Redis can publish an event whenever a key changes, but the feature ships disabled. How do you turn Redis keyspace notifications on, and what do the individual flag characters in the notify-keyspace-events configuration value mean?
answer
- notify-keyspace-events, empty = disabled
- K = keyspace channel, E = keyevent channel
- need K and/or E plus a class flag
- A = g$lshzxetd, excludes m and n
- x = expired, e = evicted
basics
~20 sSet notify-keyspace-events to a string of flags; empty means off. You must include K (keyspace channels) and/or E (keyevent channels) plus at least one event class such as g generic, $ string, l list, x expired, e evicted, or A for all classes. Example: Ex.
solid answer
~40 sNotifications are controlled by one config string, `notify-keyspace-events`, empty by default. It is a set of single-character flags in two groups. Delivery-channel flags: `K` publishes to `__keyspace@<db>__:<key>` channels, `E` publishes to `__keyevent@<db>__:<event>` channels. At least one is required - a value with only class flags delivers nothing, which is the classic configuration mistake. Class flags select which commands generate events: `g` generic (DEL, EXPIRE, RENAME), `$` strings, `l` lists, `s` sets, `h` hashes, `z` sorted sets, `t` streams, `d` module key types, `x` expired, `e` evicted, `m` key-miss, `n` new key. `A` is an alias for all classes except the key-miss and new-key ones. So `notify-keyspace-events "Ex"` gives only expired events on keyevent channels; `"KEA"` gives everything on both families and is the loudest, most expensive setting.
code
text · 10 lines# only expired events, keyevent channels only
redis-cli CONFIG SET notify-keyspace-events Ex
redis-cli CONFIG GET notify-keyspace-events
# in one terminal
redis-cli PSUBSCRIBE '__keyevent@0__:expired'
# in another
redis-cli SET sess:9 x PX 500
# subscriber receives: pmessage __keyevent@0__:expired sess:9go deeper
Know that notifications are off by default, that notify-keyspace-events turns them on, and that you need both a channel flag and an event class such as Ex.
Enumerate the main class flags, explain the K/E distinction, and state that A excludes the key-miss and new-key classes.
Talk about scoping the flags to what is consumed, the fanout cost on the command thread, and cluster-wide propagation of non-sharded Pub/Sub messages.
Weigh whether event-driven coupling to the cache is the right design at all, given the delivery guarantees, and set a policy for which classes may be enabled on shared instances.
## What the feature does When enabled, Redis publishes a Pub/Sub message every time a key is affected by a command or removed by expiration or eviction. Clients subscribe with `SUBSCRIBE` or `PSUBSCRIBE` on the notification channels and receive those messages like any other Pub/Sub traffic. The events describe *that* something happened to a key; they never carry the value. ## The configuration string Everything is controlled by `notify-keyspace-events`, settable in redis.conf or at runtime with `CONFIG SET`. Its value is an unordered set of characters, and the empty string - the default - disables the feature entirely so it costs nothing on an instance that does not use it. The characters fall into two groups that must both be represented for anything to arrive. **Where events are published** - `K` - keyspace notifications, published to `__keyspace@<db>__:<keyname>`, with the event name as the message payload. - `E` - keyevent notifications, published to `__keyevent@<db>__:<eventname>`, with the key name as the message payload. **Which events are generated** - `g` generic commands that are not type specific: DEL, EXPIRE, RENAME, PERSIST and similar. - `$` string commands, `l` list commands, `s` set commands, `h` hash commands, `z` sorted-set commands, `t` stream commands, `d` module key-type events. - `x` expired - fired when a key is removed because its TTL elapsed. - `e` evicted - fired when a key is removed by maxmemory eviction. - `m` key-miss - fired when a read finds no key (added in Redis 6.0); deliberately excluded from `A` because it is very noisy. - `n` new-key events (Redis 7.0), also excluded from `A`. - `A` - shorthand for the whole set `g$lshzxetd`. A value such as `"gxE"` means: generate generic and expired events, deliver them on keyevent channels. A value of `"gx"` alone generates nothing observable, because neither `K` nor `E` is present - Redis accepts it silently and the subscriber sees nothing, which is the single most common support question about this feature. ## Cost and blast radius Enabling classes is not free. Every matching command does extra work to build and publish a message, and Pub/Sub fanout is performed on the same thread that executes commands. On a busy instance `"KEA"` means two published messages for every write, and with pattern subscribers matching every channel the fanout cost grows with the number of subscribers. In Redis Cluster, keyspace notifications use ordinary (non-sharded) Pub/Sub, so messages propagate across the cluster bus to all nodes rather than staying local to the shard - a genuine bandwidth consideration on chatty instances. The practical rule is to enable the narrowest set that satisfies the use case. A cache-invalidation listener that only cares about expiry needs `"Ex"`, not `"KEA"`. ## Operational notes - The setting is dynamic: `CONFIG SET notify-keyspace-events Ex` takes effect immediately, and `CONFIG GET` reads back a normalised string. - Because the flags are a set, order does not matter, but the string is case sensitive: `K` and `E` are not `k` and `e`. - The database number is baked into the channel name, so a subscriber must know which database it is watching, or use a pattern such as `__keyevent@*__:expired`. - Events are published even if no client is subscribed; the flags, not the audience, decide whether the work is done.
- A team sets notify-keyspace-events to "gx" and no client receives anything. What is wrong?The value selects which event classes are generated but no delivery channel, because neither K nor E is present. Redis accepts the configuration silently and simply publishes nothing. Adding E (or K, or both) fixes it - for example "gxE".
- What is the cost of enabling KEA on a high-throughput instance?Every matching command builds and publishes up to two Pub/Sub messages on the same thread that executes commands, and fanout scales with the number of subscribers and pattern subscriptions. In Cluster the messages also travel the cluster bus to other nodes because keyspace notifications use non-sharded Pub/Sub. Enable only the classes you consume.
- Do notifications carry the value of the affected key?No. A keyspace message carries the event name and a keyevent message carries the key name; neither carries data. For expired and evicted events the value is already gone by the time the message is published, so a subscriber cannot read it back. Designs that need the old value must store it separately, for example under a companion key.
saying these in an interview costs you the question
- Setting only class flags and expecting delivery without K or E
- Believing the feature is on by default
- Assuming A includes every class, including key-miss and new-key events
- Expecting the event message to include the key's value
- Treating KEA as a harmless default on a high-throughput instance