0

Cloudflare Workers KV is an eventually consistent data store. You can write values assigned to keys, and you can read values by key from it. But is there any possibility to listen to some key's value?

In a regular relational DB you can subscribe to changes of an individual row, but are there any similar options for KV?

Neurotransmitter
  • 6,289
  • 2
  • 51
  • 38

1 Answers1

1

There is currently no built-in listen.

You could poll keys for changes, which would probably only make sense if you had a small number of keys. (You can list keys to iterate over them.)

But, since you're the owner of your KV namespace, the best option is probably to wrap your write operations so that you notify some other service/queue that a change has been made.

Brett
  • 798
  • 1
  • 4
  • 11
  • Polling is not really a great idea, since you will waste workers quota/energy on that... Some other "always on" service is a solution, however it defeats the serverless purpose. You can subscribe a client to such service and listen for messages from it, no problems with that, but it would be much simpler if you just could subscribe a client right to a KV store... – Neurotransmitter Dec 14 '20 at 10:03
  • 1
    @Neurotransmitter You could build a pubsub mechanism pretty easily with Durable Objects, FWIW. https://blog.cloudflare.com/introducing-workers-durable-objects/ (Currently in closed beta, though.) – Kenton Varda Dec 14 '20 at 15:45
  • Interesting, looks like a perfect fit for a "real-time" app! – Neurotransmitter Dec 14 '20 at 17:56