What are keyspace notifications in Redis, and what do they let your application do?
Short answer
Keyspace notifications let Redis publish events whenever certain operations happen to keys, such as a key being set, deleted, or expiring, letting other parts of your application subscribe to and react to these changes in real time, rather than needing to constantly poll Redis…
Advertisement
AModel answer
Keyspace notifications let Redis publish events whenever certain operations happen to keys, such as a key being set, deleted, or expiring, letting other parts of your application subscribe to and react to these changes in real time, rather than needing to constantly poll Redis to check if something has changed.
Example
CONFIG SET notify-keyspace-events 'KEA' SUBSCRIBE __keyevent@0__:expired
Real-world example
A shopping cart application listens for expired key events, automatically cleaning up related order data whenever a customer's abandoned cart session expires, without needing a separate polling process.
Likely follow-ups
- Why are keyspace notifications disabled by default in Redis?
- What is the performance impact of enabling keyspace notifications on a busy server?
Related topics