Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can redis key space notifications be pushed to the redis stream instead of pub/sub channel

We have a requirement that we need to get a notification on changes to a Redis data structure. Based on my research I found out that I can use Redis key space notifications for doing the same. However, Redis key space notifications send the events to Redis pub/sub channel which is fire and forget i.e once the clients lose the connections all the events till the connection is up again are lost.

Redis streams solve this problem. Also I want to use consumer group feature of Redis streams. So is there any way that Redis key space notifications can be pushed to Redis streams instead of Redis pub/sub channel?

like image 608
Rishabh Avatar asked Mar 04 '23 18:03

Rishabh


2 Answers

The only way this can be done afaik with the current - Redis v5.0.3 - is to use the Modules API to develop a module that registers for keyspace notifications, processes them and adds the relevant messages into the Stream.

like image 183
Itamar Haber Avatar answered Apr 06 '23 12:04

Itamar Haber


With RedisGears it's pretty simple to register a listener that will automatically write each event to a Stream.

e.g. the following register.py script will write for each HSET or HMSET call on person:* key prefix an event to mystream Stream.

register.py:

GearsBuilder() \
.foreach(lambda x: execute('XADD', "mystream", '*', *sum([[k,v] for k,v in x.items()],[]))) \
.register(prefix="person:*", eventTypes=['HSET', 'HMSET'])

To run it all you need to do is call:

$ gears-cli run register.py
like image 43
Guy Korland Avatar answered Apr 06 '23 12:04

Guy Korland