Module P-8·16 min read

notify-keyspace-events configuration, the event type matrix (key expiry, deletion, Set/List/Hash writes), subscribing to expiry for cache warming, and the critical limitation: keyspace notifications are at-most-once.

JJS
Written by Jatin Jain Saraf · Senior Software Engineer

P-8 — Keyspace Notifications and Event-Driven Architectures

Who this module is for: You want to trigger application logic when a Redis key expires, is deleted, or is modified — without polling. Keyspace notifications let you subscribe to Redis's internal events via Pub/Sub. This module covers the configuration, the event type matrix, practical patterns, and the critical limitation that most documentation buries at the bottom.


What Keyspace Notifications Are

Keyspace notifications allow Redis to publish messages to Pub/Sub channels when specific events occur — a key expires, a key is written, a key is deleted. Your application subscribes to these channels and reacts to events.

This enables:

  • Cache warming: re-populate a key when it expires
  • Audit logging: record every mutation to sensitive keys
  • Event-driven workflows: trigger downstream logic when a key is set
  • Session expiry hooks: notify your app when a session token expires

Enabling Keyspace Notifications

By default, keyspace notifications are disabled (they add CPU overhead to every write operation).

CONFIG SET notify-keyspace-events "KEA"

Or in redis.conf:

notify-keyspace-events "KEA"

The Event Flag String

The value is a combination of flag characters:

Event type (what happened):

  • g — generic commands (DEL, EXPIRE, RENAME, COPY)
  • $ — String commands (SET, GETSET, SETRANGE, APPEND, INCR, etc.)
  • l — List commands (LPUSH, RPUSH, LPOP, RPOP, LMOVE, etc.)
  • s — Set commands (SADD, SREM, SPOP, etc.)
  • h — Hash commands (HSET, HDEL, etc.)
  • z — Sorted Set commands (ZADD, ZINCRBY, ZREM, etc.)
  • x — Expired events (key expired by TTL)
  • e — Evicted events (key evicted by maxmemory-policy)
  • t — Stream commands
  • d — Module key type events
  • m — Key miss events (when a command targets a non-existent key — generates many events)
  • A — Alias for g$lshztdxea (every event class except key-miss events)

Channel type (where to publish):

  • K — Keyspace events: channel is __keyspace@{db}__:{key}; message is the event name
  • E — Keyevent events: channel is __keyevent@{db}__:{event}; message is the key name

You must specify at least one channel type (K or E) and at least one event type.

Common configurations:

text

The Two Channel Types

Keyspace Channels (K)

Channel: __keyspace@{db}__:{key}
Message: the event name (e.g., "expired", "set", "del")

Subscribe to a specific key to receive all events affecting it:

text

Keyevent Channels (E)

Channel: __keyevent@{db}__:{event}
Message: the key name

Subscribe to a specific event type to receive it for all keys:

text

For most use cases (cache expiry hooks, session expiry), __keyevent@0__:expired is the right channel.


Practical Pattern: Cache Re-Population on Expiry

When a cached value expires, automatically re-populate it:

typescript

Sign in to keep reading

The rest of this module is free — sign in with Google to unlock it and track your progress.

Sign in & Register

Discussion

0

Join the discussion

Loading comments...

© 2026 Jatin Jain Saraf (JJS). All rights reserved.