KV Store Eventual Consistency

As described the KV Store is eventually consistent, with conflicts solved using last write wins. I’m curious how this is implemented and how that affects applications that might write to KV Store in bursts. How accurately are writes timestamped? If two writes occur within a couple of ms of each other in different places, are they guaranteed to be applied in the correct order?

It would be nice to have a number, eg if Cloudflare uses system time to generate the timestamp, that system time is kept accurate to within n units across data centers.

KV is eventually consistent as you mentioned so it is up to you as the developer to design your key structure in a way that mitigates your updates from being overwritten and lost. It is a bad assumption to think that updates will always be in the same order as they were given. KV does not really guarantee this but you can easily design your systems to be tolerant to this.

Using a timestamp (Date.now()) in your key structure is a great way to be mitigate issues around this. You can then find keys (and their values) using the list function prefix argument. KV · Cloudflare Workers docs

Say I wanted to count page views for users who go to my website. I can do this successfully using Workers and KV. If I have a key for the user [email protected] and update it over and over I will lose data almost immediately (bad design). Instead I should set up my key structure to have a new KV for each page view. For example a key structure like [email protected]:16250750641567 then I can do a prefix list of [email protected]: to find all of their page views. I can then store all sorts of data in the value as I see fit.

The only time I update the same key is when I know that whatever is doing the updating is scoped to a single source at any given time and won’t be making lots of changes at once (think one human in one location). If there is a potential for more than one source then data will be lost when using a single key.

Remember that KV is not a database, it is more akin to hundreds of databases that are all merging through time (and usually last merge wins but no guarantee).