Asynchronous Workers === Inconsistent KV ?!

I love Cloudflare! Thank you all so much!

IF a KV key is users
AND the value is [ { id: 1, isActive: false }, { id: 2, isActive: false } ]

IF worker1 and worker2 both get the above value simultaneously
AND worker1 sets users[0].isActive to true AND puts that into KV
AND THEN worker2 sets users[1].isActive to true AND puts that into KV

Could the value in KV now be

WRONG: [ { id: 1, isActive: false }, { id: 1, isActive: true } ]

Because worker2 wrote after worker1, BUT we would love the KV value to be

CORRECT: [ { id: 1, isActive: true }, { id: 1, isActive: true } ]

  • Is WRONG possible please?!
  • Is CORRECT possible please?!

That is correct, yes - your WRONG example could happen, due to the nature of how KV is designed to be eventually consistent.

You could do something like have a user-1, user-2 key, or alternatively if you need stronger consistency, Durable Objects is likely to be a better fit for this kind of user data.

1 Like

Thank you for guiding me @cherryjimbo! It sounds like today is learn all about Durable Objects day! Thank you so much!

1 Like