nats-server icon indicating copy to clipboard operation
nats-server copied to clipboard

Receive watch events for TTL'd KV objects

Open segfaultdoc opened this issue 2 years ago • 23 comments

Feature Request

Use Case:

We currently use JetStream KV to build different in memory cache views of the objects stored. We'd like to leverage the KV stores TTL mechanism to delete stale objects in our derived views.

Proposed Change:

Emit watch events from the server signaling when a KV object has been TTL'd.

Who Benefits From The Change(s)?

Jito Labs and others

Alternative Approaches

Build cleanup logic in app code.

segfaultdoc avatar Jul 15 '22 14:07 segfaultdoc

This has been requested and we are looking into the best way to provide this.

derekcollison avatar Jul 17 '22 15:07 derekcollison

Would love to see this too

esemeniuc avatar Jan 05 '23 14:01 esemeniuc

Yes on our list for 2.10..

derekcollison avatar Jan 05 '23 15:01 derekcollison

This would be quite useful for building large distributed caching system with nats as the origin store.

the secondary caches could be cdn style servers that speak http downstream and nats upstream.

Use case then is:

where an external cache line can be invalidated when the nats kv or Object item TTL expires..

gedw99 avatar Jun 29 '23 09:06 gedw99

Any updates on this making it into v2.10?

alexandreLamarre avatar Aug 30 '23 18:08 alexandreLamarre

Will not make it in 2.10.0. Still on our list.

derekcollison avatar Aug 30 '23 18:08 derekcollison

it's much needed, we're waiting

stpnov avatar Jan 16 '24 14:01 stpnov

It will be coming..

derekcollison avatar Jan 16 '24 14:01 derekcollison

@derekcollison Any eta on a target version/date?

alexj212 avatar Jan 29 '24 20:01 alexj212

2.11 April timeframe is target.

derekcollison avatar Jan 29 '24 22:01 derekcollison

If the key expiration events could have an option to be distributed to a stream that would be great. This would be great for a multi-client environment to use different stream policy to consume the expiration events.

mxcoppell avatar Feb 12 '24 23:02 mxcoppell

They will be on the stream that caused the expiration. As system delete markers.

derekcollison avatar Feb 13 '24 02:02 derekcollison

Is there any limitation for the underlying stream for the KV store? One issue we have with Redis is that all pods need to listen to the key space for expiration events and it requires dedup for the duplicate events. If the underlying stream could be WorkQueuePolicy and a number of queue subscribers can listen to the stream that would be perfect.

mxcoppell avatar Feb 13 '24 04:02 mxcoppell

@mxcoppell I am not following. Could you provide a bit more information?

derekcollison avatar Feb 13 '24 15:02 derekcollison

Sure, using the following example from https://shijuvar.medium.com/using-nats-jetstream-key-value-store-in-go-85f88b0848ce:

url := os.Getenv("NATS_URL")
 if url == "" {
  url = nats.DefaultURL
 }

 nc, _ := nats.Connect(url)
 defer nc.Drain()

 js, _ := nc.JetStream()
if stream, _ := js.StreamInfo("KV_discovery"); stream == nil {
  // A key-value (KV) bucket is created by specifying a bucket name.
  kv, _ = js.CreateKeyValue(&nats.KeyValueConfig{
   Bucket: "discovery",
  })
 } else {
  kv, _ = js.KeyValue("discovery")
 }

The KV bucket "discovery" is in the stream "KV_discovery".

My understanding to the key expiration events (could be totally wrong) is that the event will be distributed to the stream consumers of "KV_discovery". My last question about this if user could set the retention policy like "WorkGroupQueue" for "KV_discovery" so a group of queue subscribers could listen to stream "KV_discover" for the expiration events - and each expiration event will be distributed to one subscriber at a time, not broadcasting to all the listeners.

Thanks!

mxcoppell avatar Feb 14 '24 02:02 mxcoppell

Yes system level expiration or removal will be distributed to the watchers of that KV in 2.11.

derekcollison avatar Feb 14 '24 05:02 derekcollison

Derek, could you use an example to illustrate how this expiration event subscription work? And if the events are broadcasts to all watchers or there could be more options like queue subscriber?

mxcoppell avatar Feb 14 '24 05:02 mxcoppell

They will be for all watchers. This is similar to what happens today if you use the KV library to delete a key.

derekcollison avatar Feb 14 '24 05:02 derekcollison