"Redis key space Notification "
Key-space notifications allow clients to subscribe to channels or patterns to receive events that change the Redis dataset in some way.
Here are some examples of events sent by a key-space notification:
- All the commands that modify the key.
- All keys that receive the Lpush command.
0
All expired keys in the number database.
Events are distributed through Redis's subscription and publishing features (pub/sub), so all clients that support the subscription and publishing capabilities can use the key-space notification function directly without any modifications.
Because the current subscription and publishing features of Redis are the send-and-forget (fire and forget) policy, if your program requires reliable event notification (reliable notification of events), the current key-space notification may not be suitable for you: When a client that subscribes to an event is disconnected, it loses all the events that were distributed to it during the disconnection.
Type of Event
For each operation that modifies the database, the key-space notification sends two different types of events.
For example, 0
when you execute the DEL command on the key of a database, the mykey
system distributes two messages, equivalent to executing the following two PUBLISH commands:
PUBLISH [email protected]__:mykey delpublish [email Protected]__:del mykey
Subscribing to the first channel [email protected]__:mykey
can receive 0
events for all modifier keys in the number database mykey
, while subscribing to the second channel [email protected]__:del
can receive 0
keys for all execution commands in the number database del
.
The keyspace
channel that is prefixed is called the Key Space notification (KEY-SPACE notification), and the keyevent
channel prefixed is called the key event notification (key-event notification).
When the del mykey
command is executed:
- The subscriber of the key space channel will receive the name of the event being executed, in this case, that is
del
.
- The subscriber of the key event channel will receive the name of the key of the event being executed, in this case
mykey
.
Configuration
Because the Enable key space notification feature consumes some CPU, the feature is turned off in the default configuration.
You can redis.conf
CONFIG SET
turn the key space notification feature on or off by modifying the file or by using the command directly:
- When
notify-keyspace-events
the parameter of an option is an empty string, the function is closed.
- On the other hand, when the parameter is not an empty string, the function is turned on.
notify-keyspace-events
parameter can be any combination of the following characters, which specifies which types of notifications the server sends:
character |
Notifications sent |
K |
Key space notifications, all notifications as [email protected]<db>__ prefixes |
E |
Key event notification, all notifications to [email protected]<db>__ be prefixed |
g |
DEL , EXPIRE , and RENAME Other types of General command-independent notification |
$ |
Notification of STRING commands |
l |
Notification of LIST commands |
s |
Notification of COLLECTION commands |
h |
Notification of the hash command |
z |
Notification of ordered collection commands |
x |
Expired event: Sent whenever an expiration key is deleted |
e |
Eviction (evict) event: Sent whenever a key is maxmemory deleted because of a policy |
A |
g$lshzxe Aliases for parameters |
The input parameter must have at least one K
or E
, otherwise, no notification will be distributed, regardless of the rest of the parameters.
For example, if you just want to subscribe to a list-related notification in a key space, the parameters should be set Kl
, and so on.
Setting a parameter to a string "AKE"
indicates that all types of notifications are sent.
Reference: http://redisdoc.com/topic/notification.html
Redis Key Space Notification