Handling Stale Data with Cloudflare KV API After Updating Like Count in a Next.js Application
I'm currently working on a Next.js application where user 'likes' are handled through Cloudflare KV API. To enhance security, these updates are routed through Next.js API routes before directly updating the KV store. Although the updates are reflected correctly in the Cloudflare dashboard, fetching like counts immediately after an update seems to return outdated information. This leads to a less responsive user experience, as the UI doesn't show the updated like counts immediately.
Has anyone experienced similar issues with Cloudflare KV's cache consistency? What strategies did you employ to ensure that your users see the most recent data after an update? Any insights or suggestions on how to handle this stale data problem would be greatly appreciated.
Saving is done with this code.
Acquisition is done with this code
2 Replies
We generally recommend using something like Durable Objects for stuff that requires Atomicity, like tracking Likes
As you have experienced with KV, there is not a guarantee that the value returned is the latest value
Thanks! I see what you mean...kv makes it difficult...got it!