Redis Keyspace Notifications

Keyspace notifications let Redis announce events about your data. When a key expires, changes, or leaves memory, Redis publishes a message that any subscribed client can read. Applications use these messages to clean up resources, refresh caches, and react to changes without constant checking.

The Doorbell Analogy

A doorbell rings the moment a visitor arrives, so you never need to peek through the window every minute. Keyspace notifications ring a bell inside your application whenever something happens to a key.

  Key "session:7" expires
          │
          ▼
  ┌────────────────────────┐          ┌────────────────────┐
  │ Redis publishes event  │ ───────► │ Subscriber app     │
  │ on a Pub/Sub channel   │          │ cleans up session  │
  └────────────────────────┘          └────────────────────┘

Turning the Feature On

Redis ships with notifications switched off, because they use CPU. Enable them with the notify-keyspace-events setting.

127.0.0.1:6379> CONFIG SET notify-keyspace-events KEA
OK

The value is a string of letter flags. Each flag turns one group of events on.

Flag   Meaning
─────  ───────────────────────────────────────────
K      Publish on keyspace channels (named by key)
E      Publish on keyevent channels (named by event)
g      Generic commands such as DEL, EXPIRE, RENAME
$      String commands
l      List commands
s      Set commands
h      Hash commands
z      Sorted set commands
x      Expired events
e      Evicted events
A      Shortcut for most event groups

Pick only the flags you need. A setting of Ex listens only to expired events and keeps the extra load low.

Two Channel Styles

Channel pattern                    Message content   Question it answers
─────────────────────────────────  ────────────────  ────────────────────────────
__keyspace@0__:session:7           Event name        What happened to this key?
__keyevent@0__:expired             Key name          Which keys expired?

The number after the at sign is the database index. Database 0 uses @0.

Listening for Events

Open one connection and subscribe to a pattern.

127.0.0.1:6379> PSUBSCRIBE __keyevent@0__:*
1) "psubscribe"
2) "__keyevent@0__:*"
3) (integer) 1

Open a second connection and change some data.

127.0.0.1:6379> SET coupon:55 "SAVE10" EX 5
OK

Five seconds later the first connection prints the event.

1) "pmessage"
2) "__keyevent@0__:*"
3) "__keyevent@0__:expired"
4) "coupon:55"

Expiry Timing

Redis publishes the expired event when it actually deletes the key, not at the exact second the timer ends. Redis deletes expired keys when a client touches them and through a background sweep. A key may linger a short while after its time ends, and the event waits until deletion.

Delivery Is Fire-and-Forget

Notifications use Pub/Sub, so Redis sends them only to clients subscribed at that moment. A disconnected subscriber misses events forever. Treat notifications as helpful hints. Design critical work, such as billing, with Streams or a database that keeps a durable record.

Practical Example – Releasing a Held Seat

A ticket site holds a seat for 5 minutes during checkout.

Step 1:  SET hold:seat:A12 "user:88" EX 300
Step 2:  A worker subscribes to __keyevent@0__:expired
Step 3:  The user abandons checkout.
Step 4:  After 5 minutes Redis removes "hold:seat:A12" and publishes the event.
Step 5:  The worker reads the key name and marks seat A12 as free in the database.

A periodic database sweep that frees stale holds acts as the safety net if the worker misses a message.

Key Points

  • Enable notifications with notify-keyspace-events and pick only the needed flags.
  • Keyspace channels report events per key, and keyevent channels report keys per event.
  • The expired event fires when Redis deletes the key.
  • Subscribers that disconnect lose events, so add a backup check for critical tasks.