'Does REDIS support event listeners?

My USE Case:

I want to have a key in redis and want a callback to some method (Using Jedis Java client) when the keys value changes or exceeds some threshold. Is it possible?



Solution 1:[1]

Yes it is possible.

In a first redis-cli:

$ redis-cli
127.0.0.1:6379> psubscribe __key*
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "__key*"
3) (integer) 1

Open another terminal with a second redis-cli

$ redis-cli
127.0.0.1:6379> config set notify-keyspace-events KEg$
OK
127.0.0.1:6379> config get notify-keyspace-events
1) "notify-keyspace-events"
2) "g$KE"
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> del foo
(integer) 1
127.0.0.1:6379>

You should see in the redis-cli first terminal:

1) "pmessage"
2) "__key*"
3) "__keyspace@0__:foo"
4) "set"
1) "pmessage"
2) "__key*"
3) "__keyevent@0__:set"
4) "foo"
1) "pmessage"
2) "__key*"
3) "__keyspace@0__:foo"
4) "del"
1) "pmessage"
2) "__key*"
3) "__keyevent@0__:del"
4) "foo"

Just be aware that this is not a free functionality and the performances are necessarily impacted because REDIS must trigger the PUBLISH command on each of the events.

See the documentation here: https://redis.io/docs/manual/keyspace-notifications/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jlguenego