Keyspace Notifications
What are keyspace notifications?
Section titled “What are keyspace notifications?”Redis can publish a Pub/Sub message whenever certain events happen — a key expires, a SET is issued, a DEL runs, and more. Your application subscribes to these channels to react without polling: for example, invalidate a local cache when a key expires, or trigger a background job when a specific key is written.
Redis exposes two channel types:
- Keyevent channels — one channel per event type; the message payload is the key name. Pattern:
__keyevent@<db>__:<event>(example:__keyevent@0__:expired) - Keyspace channels — one channel per key; the message payload is the event type. Pattern:
__keyspace@<db>__:<keyname>
Enabling keyspace notifications
Section titled “Enabling keyspace notifications”Keyspace notifications are disabled by default because publishing every event adds CPU overhead. Enable them with CONFIG SET at runtime or via redis.conf on startup.
The value is a string of flag characters that select which event classes to publish:
| Flag | Events enabled |
|---|---|
K | Keyspace events (__keyspace@... channel) |
E | Keyevent events (__keyevent@... channel) |
x | Expired events (key TTL reached zero) |
g | Generic commands: DEL, EXPIRE, RENAME, … |
s | Set commands (SADD, SREM, …) |
l | List commands (LPUSH, RPOP, …) |
For production expiry notifications the common setting is Ex — keyevent channel (E) for expired events only (x):
notify-keyspace-events "Ex"Or at runtime:
127.0.0.1:6379> CONFIG SET notify-keyspace-events ExOKSubscribing to expiry events
Section titled “Subscribing to expiry events”Keyspace notifications use a two-terminal workflow. Open Terminal 1 and subscribe before the key expires:
127.0.0.1:6379> PSUBSCRIBE __keyevent@0__:expiredReading messages... (press Ctrl-C to quit)In Terminal 2, set a key with a short TTL to trigger the event:
127.0.0.1:6379> SET demo:token "abc" EX 3OKAfter 3 seconds, Terminal 1 receives the notification:
1) "pmessage"2) "__keyevent@0__:expired"3) "__keyevent@0__:expired"4) "demo:token"The fourth line is the key name — your subscriber now knows exactly which key expired.
Runnable commands
Section titled “Runnable commands”The commands below enable notifications and set a key with a TTL so you can observe the configuration in action. To see the expired event, open a second redis-cli, run PSUBSCRIBE __keyevent@0__:expired, then in the first terminal run SET demo:token "hello" EX 5 and wait 5 seconds.
CONFIG SET notify-keyspace-events Ex
SET demo:token "hello" EX 5
TTL demo:token