Redis keyspace notifications
Monitor changes to Redis keys and values in real time
Keyspace notifications allow clients to subscribe to Pub/Sub channels in order to receive events affecting the Redis data set in some way.
Examples of events that can be received are:
- All the commands affecting a given key.
- All the keys receiving an LPUSH operation.
- All the keys expiring in the database 0.
Note: Redis Pub/Sub is fire and forget; that is, if your Pub/Sub client disconnects, and reconnects later, all the events delivered during the time the client was disconnected are lost.
Type of events
Keyspace notifications are implemented by sending two distinct types of events
for every operation affecting the Redis data space. For instance a DEL
operation targeting the key named mykey in database 0 will trigger
the delivering of two messages, exactly equivalent to the following two
PUBLISH commands:
PUBLISH __keyspace@0__:mykey del
PUBLISH __keyevent@0__:del mykey
The first channel listens to all the events targeting
the key mykey and the other channel listens only to del operation
events on the key mykey
The first kind of event, with keyspace prefix in the channel is called
a Key-space notification, while the second, with the keyevent prefix,
is called a Key-event notification.
In the previous example a del event was generated for the key mykey resulting
in two messages:
- The Key-space channel receives as message the name of the event.
- The Key-event channel receives as message the name of the key.
It is possible to enable only one kind of notification in order to deliver just the subset of events we are interested in.
Configuration
By default keyspace event notifications are disabled because while not
very sensible the feature uses some CPU power. Notifications are enabled
using the notify-keyspace-events of redis.conf or via the CONFIG SET.
Setting the parameter to the empty string disables notifications. In order to enable the feature a non-empty string is used, composed of multiple characters, where every character has a special meaning according to the following table:
K     Keyspace events, published with __keyspace@<db>__ prefix.
E     Keyevent events, published with __keyevent@<db>__ prefix.
g     Generic commands (non-type specific) like DEL, EXPIRE, RENAME, ...
$     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 events (events generated every time a key expires)
e     Evicted events (events generated when a key is evicted for maxmemory)
m     Key miss events generated when a key that doesn't exist is accessed (Note: not included in the 'A' class)
n     New key events generated whenever a new key is created (Note: not included in the 'A' class)
o     Overwritten events generated every time a key is overwritten (Note: not included in the 'A' class)
c     Type-changed events generated every time a key's type changes (Note: not included in the 'A' class)
A     Alias for "g$lshztdxe", so that the "AKE" string means all the events except "m", "n", "o" and "c".
At least K or E should be present in the string, otherwise no event
will be delivered regardless of the rest of the string.
For instance to enable just Key-space events for lists, the configuration
parameter must be set to Kl, and so forth.
You can use the string KEA to enable most types of events.
Events generated by different commands
Different commands generate different kind of events according to the following list.
- APPENDgenerates an- appendevent.
- COPYgenerates a- copy_toevent.
- DELgenerates a- delevent for every deleted key.
- EXPIREand all its variants (- PEXPIRE,- EXPIREAT,- PEXPIREAT) generate an- expireevent when called with a positive timeout (or a future timestamp). Note that when these commands are called with a negative timeout value or timestamp in the past, the key is deleted and only a- delevent is generated instead.
- HDELgenerates a single- hdelevent, and an additional- delevent if the resulting hash is empty and the key is removed.
- HEXPIREand all its variants (- HEXPIREAT,- HPEXPIRE,- HPEXPIREAT) generate- hexpiredevents. Furthermore,- hexpiredevents are generated when fields expire.
- HINCRBYFLOATgenerates an- hincrbyfloatevent.
- HINCRBYgenerates an- hincrbyevent.
- HPERSISTgenerates an- hpersistevent.
- HSET,- HSETNXand- HMSETall generate a single- hsetevent.
- INCRBYFLOATgenerates an- incrbyfloatevents.
- INCR,- DECR,- INCRBY,- DECRBYcommands all generate- incrbyevents.
- LINSERTgenerates an- linsertevent.
- LMOVEand- BLMOVEgenerate an- lpop/- rpopevent (depending on the wherefrom argument) and an- lpush/- rpushevent (depending on the whereto argument). In both cases the order is guaranteed (the- lpush/- rpushevent will always be delivered after the- lpop/- rpopevent). Additionally a- delevent will be generated if the resulting list is zero length and the key is removed.
- LPOPgenerates an- lpopevent. Additionally a- delevent is generated if the key is removed because the last element from the list was popped.
- LPUSHand- LPUSHXgenerates a single- lpushevent, even in the variadic case.
- LREMgenerates an- lremevent, and additionally a- delevent if the resulting list is empty and the key is removed.
- LSETgenerates an- lsetevent.
- LTRIMgenerates an- ltrimevent, and additionally a- delevent if the resulting list is empty and the key is removed.
- MIGRATEgenerates a- delevent if the source key is removed.
- MOVEgenerates two events, a- move_fromevent for the source key, and a- move_toevent for the destination key.
- MSETgenerates a separate- setevent for every key.
- PERSISTgenerates a- persistevent if the expiry time associated with key has been successfully deleted.
- RENAMEgenerates two events, a- rename_fromevent for the source key, and a- rename_toevent for the destination key.
- RESTOREgenerates a- restoreevent for the key.
- RPOPLPUSHand- BRPOPLPUSHgenerate an- rpopevent and an- lpushevent. In both cases the order is guaranteed (the- lpushevent will always be delivered after the- rpopevent). Additionally a- delevent will be generated if the resulting list is zero length and the key is removed.
- RPOPgenerates an- rpopevent. Additionally a- delevent is generated if the key is removed because the last element from the list was popped.
- RPUSHand- RPUSHXgenerates a single- rpushevent, even in the variadic case.
- SADDgenerates a single- saddevent, even in the variadic case.
- SETRANGEgenerates a- setrangeevent.
- SETand all its variants (- SETEX,- SETNX,- GETSET) generate- setevents. However- SETEXwill also generate an- expireevents.
- SINTERSTORE,- SUNIONSTORE,- SDIFFSTOREgenerate- sinterstore,- sunionstore,- sdiffstoreevents respectively. In the special case the resulting set is empty, and the key where the result is stored already exists, a- delevent is generated since the key is removed.
- SMOVEgenerates an- sremevent for the source key, and an- saddevent for the destination key.
- SORTgenerates a- sortstoreevent when- STOREis used to set a new key. If the resulting list is empty, and the- STOREoption is used, and there was already an existing key with that name, the result is that the key is deleted, so a- delevent is generated in this condition.
- SPOPgenerates an- spopevent, and an additional- delevent if the resulting set is empty and the key is removed.
- SREMgenerates a single- sremevent, and an additional- delevent if the resulting set is empty and the key is removed.
- XADDgenerates an- xaddevent, possibly followed an- xtrimevent when used with the- MAXLENsubcommand.
- XDELgenerates a single- xdelevent even when multiple entries are deleted.
- XGROUP CREATECONSUMERgenerates an- xgroup-createconsumerevent.
- XGROUP CREATEgenerates an- xgroup-createevent.
- XGROUP DELCONSUMERgenerates an- xgroup-delconsumerevent.
- XGROUP DESTROYgenerates an- xgroup-destroyevent.
- XGROUP SETIDgenerates an- xgroup-setidevent.
- XSETIDgenerates an- xsetidevent.
- XTRIMgenerates an- xtrimevent.
- ZADDgenerates a single- zaddevent even when multiple elements are added.
- ZDIFFSTORE,- ZINTERSTOREand- ZUNIONSTORErespectively generate- zdiffstore,- zinterstoreand- zunionstoreevents. In the special case the resulting sorted set is empty, and the key where the result is stored already exists, a- delevent is generated since the key is removed.
- ZINCRBYgenerates a- zincrevent.
- ZREMRANGEBYRANKgenerates a single- zrembyrankevent. When the resulting sorted set is empty and the key is generated, an additional- delevent is generated.
- ZREMRANGEBYSCOREgenerates a single- zrembyscoreevent. When the resulting sorted set is empty and the key is generated, an additional- delevent is generated.
- ZREMgenerates a single- zremevent even when multiple elements are deleted. When the resulting sorted set is empty and the key is generated, an additional- delevent is generated.
- Every time a key with a time to live associated is removed from the data set because it expired, an expiredevent is generated.
- Every time a key is evicted from the data set in order to free memory as a result of the maxmemorypolicy, anevictedevent is generated.
- Every time a new key is added to the data set, a newevent is generated.
- Every time a key is being overwritten, an overwrittenevent is generated.
- Every time a key's type changes, a type_changedevent is generated.
IMPORTANT all the commands generate events only if the target key is really modified. For instance an SREM deleting a non-existing element from a Set will not actually change the value of the key, so no event will be generated.
If in doubt about how events are generated for a given command, the simplest thing to do is to watch yourself:
$ redis-cli config set notify-keyspace-events KEA
$ redis-cli --csv psubscribe '__key*__:*'
Reading messages... (press Ctrl-C to quit)
"psubscribe","__key*__:*",1
At this point use redis-cli in another terminal to send commands to the
Redis server and watch the events generated:
"pmessage","__key*__:*","__keyspace@0__:foo","set"
"pmessage","__key*__:*","__keyevent@0__:set","foo"
...
Timing of expired events
Keys with a time to live associated are expired by Redis in two ways:
- When the key is accessed by a command and is found to be expired.
- Via a background system that looks for expired keys in the background, incrementally, in order to be able to also collect keys that are never accessed.
The expired events are generated when a key is accessed and is found to be expired by one of the above systems, as a result there are no guarantees that the Redis server will be able to generate the expired event at the time the key time to live reaches the value of zero.
If no command targets the key constantly, and there are many keys with a TTL associated, there can be a significant delay between the time the key time to live drops to zero, and the time the expired event is generated.
Expired (expired) events are generated when the Redis server deletes the key and not when the time to live theoretically reaches the value of zero.
Events in a cluster
Every node of a Redis cluster generates events about its own subset of the keyspace as described above. However, unlike regular Pub/Sub communication in a cluster, events' notifications are not broadcasted to all nodes. Put differently, keyspace events are node-specific. This means that to receive all keyspace events of a cluster, clients need to subscribe to each of the nodes.
@history
- >= 6.0: Key miss events were added.
- >= 7.0: Event type- newadded