Redis notifications overview

Redis notifications allow clients to receive events in response to data changes in Redis. Behind the scenes, this is made possible by Redis Pub/Sub channels.

Types of notifications

The way we use notifications is by subscribing to the appropriate Pub/Sub channel, which is based on a specific naming template. There are two types of notifications we can work with:

  • Key-space notification: We can use this to get notified about changes to specific keys. For example, to get notified about all actions on the key testkey, we can use a key-space notification by subscribing to a Pub/Sub channel named __keyspace@0__:testkey. The channel will receive the name of the event (set, delete, etc.), along with the name of the key. It’s possible to further restrict this. For example, if we’re only interested in the set operations on the key, we can subscribe to the __keyspace@0__:testkey set channel instead.

  • Key-event notification: We can use this to get notified about specific actions that are being executed. For example, to get notified about set operations on any key, we can use a key-event notification by subscribing to a Pub/Sub channel named __keyevent@0__:set. The channel will receive the name of the key, along with the event of the key (set, delete, etc.).

The notifications feature needs to explicitly enabled in redis.conf, or we can do this using the CONFIG SET command. We need to set the notify-keyspace-events property, for example:

Get hands-on with 1400+ tech skills courses.