Reputation: 85
Is there a way to have a reliable notifications on key expiry in Redis. As I understood from the Redis documentation that key expiry notification is Redis is not reliable.
As per the documentation, Because Redis Pub/Sub is fire and forget currently there is no way to use this feature if your application demands reliable notification of events, that is, if your Pub/Sub client disconnects, and reconnects later, all the events delivered during the time the client was disconnected are lost.
Can someone suggest a way to have reliable key expiry notification through Redis or please suggest some other database or mechanism in Java to have data expiry and it's notification in order to perform additional operations on data on expiry.
Upvotes: 8
Views: 4506
Reputation: 6754
You can improve the reliability of the notifications by having multiple clients subscribe to the keyspace expire notifications.
You can decouple the expire notification from the actual processing of the expired key, so you can manage to process them just once and also increase reliability by having the keyspace expire notification clients doing a simple queueing task. For this, you could use other data structures from Redis like sets or sorted sets (unique values), or even Redis Streams with Consumer Groups.
Another option is to expire another key instead of the actual key. See Event on key expire issue. This is also helpful if you need the value of the expired key.
Assuming your to-be-expired key is called 'foo', when you create it don't expire it but rather create another key called 'foo:expire' and set the TTL on it.
... Upon getting an expiry event, fire the logic to store and remove the "old" data ('foo')
On this approach, you can include logic to recover from missed notifications.
Upvotes: 4