5

Is it possible to set a listener in Redis which is triggered when an item (value/key) is inserted in Redis or when an value is changed? I researched library Jedis and Redisson, but found nothing.

tddmonkey
  • 20,798
  • 10
  • 58
  • 67
user3541830
  • 75
  • 2
  • 8

2 Answers2

6

Yes, you can do that with Redis' Keyspace Notifications and subscribing to the relevant channels from your Jedis/Redisson clients.

Itamar Haber
  • 47,336
  • 7
  • 91
  • 117
  • From the doc: "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." Is there any technique/architecture we can use to compensate this? – Anthony Kong Jun 09 '20 at 01:55
  • Currently, the only option I'm aware of is using RedisGears - see here for details https://github.com/antirez/redis/issues/5766#issuecomment-620623878 – Itamar Haber Jun 09 '20 at 14:32
-2

Redis is not having a feature to support this. The client would simply have to query the data again and again to get the information to make it possible to make the notification you are searching for.

I guess you should utilize the channel feature of Redis (You then need to make a change in client that actually push the data to your redis database)

Where you subsribe SUBSCRIBE hashtablekeychannel

Ie. inserting data should be changed to HSET hashtablekey key "value" PUBLISH hashtablekeychannel key

(Actually in most cases you could actually then just remove the hashtable and to publish the value - but that's and other story)