KV API - Need either keys() or count() and key(index)

I do similar things with .gitignore, but there was the day I accidentally deleted .gitignore! Perhaps Cloudflare could provide some separate keys for the KV API. Or perhaps they could make them available from within the Worker. Although, once they provide the .keys() function natively the point may be moot.

This is a bit old, but I see no one explicitly addressed it, so I’ll reply. :slight_smile:

This is very racy, and is going to drop keys, e.g.

  1. Add key1 to object_v0 occurs in IAD -> object_v1
  2. Add key2 to object_v0 occurs in Japan -> object_v2
  3. After global sync, object_v1 was written, then object_v2 was written, without any merge
  4. subsequent read gets object_v2, missing key1

unit testing won’t catch this unless you explicitly induce the race by forcing connections to route to different CF data centers. Which I’m not sure how possible that is as I’ve not tried it myself.

I wouldn’t even recommend using it for development / testing as it fails silently.

Thanks … as I feared. Really could do with a distributed conflict resolution mechanism like Gun. Actually moving away from this key storage approach anyway for some other reasons anyway.

Cloudflare could probably add a transactional write-layer to the KV if they wanted to, but it would probably cost to use it. Like an API gateway that waits for the key to be written on all locations before returning a response.

How do Gun handle it? By time-stamps? If so, then if any write race to come first, does the next write (which is stamped earlier) overwrite the current value? In that case, there’s a window where the first value could be read.

Here is the link to the Gun algorithm:
https://github.com/amark/gun/wiki/Hypothetical-Amnesia-Machine