Starting from Redis 2.8.0, Redis has added a publish/subscribe mode and keyspace notification function. The key space message reminder provides the ability to allow the client to obtain Redis data changes by subscribing to a specified channel.
It should be noted that the keyspace message reminder is not reliable, and it will not confirm whether the subscriber has received the message. For example, a subscribed client is temporarily disconnected, and events that occurred during the period until the connection is restored will not be available again.
Configuration
By default, Redis does not enable the keyspace message reminder function. In order to enable this function, it needs to notify-keyspace-events
be set through configuration, for example:
redis> CONFIG GET notify-keyspace-events
1) "notify-keyspace-events"
2) ""
redis> CONFIG SET notify-keyspace-events KEA
OK
redis> CONFIG GET notify-keyspace-events
1) "notify-keyspace-events"
2) "AKE"
In the above example, it will be notify-keyspace-events
configured to KEA
represent all events except misses. Among them, K
and E
represents two types of events- Keyspace
and Keyevent
. Keyspace
Represents messages related to event names, such as subscribing to an operation event performed on a specified key; Keyevent
represents messages related to key names, such as subscribing to key names related to key expiration events.
For more notify-keyspace-events
configuration, please refer to the following description:
K
: Keyspace event, will be__keyspace@<db>__
prefixed with the eventE
: Keyevent event, will be__keyevent@<db>__
prefixed with the eventg
: Non-specific type of general commands, for exampleDEL
,EXPIRE
,RENAME
like$
: String commands, for exampleSET
,INCR
etc.l
: List commands, for exampleLPUSH
,LPOP
etc.s
: Set commands, for exampleSADD
,SREM
etc.h
: Hash table commands, for exampleHSET
,HINCRBY
etc.z
: Ordered collection commands, for exampleZSET
,ZREM
etc.t
: Stream commands, for exampleXADD
,XDEL
etc.x
: Expiration event (generated every time a key expires)e
: Elimination event (generated when each key is eliminated)m
: Missing event (generated when accessing a non-existent key)A
: The configuredg$lshztxe
alias, but does not include the miss eventm
Subscribe to specified events
After completing the configuration, you can SUBSCRIBE
subscribe to one or more specified events by ordering to subscribe to the specified channel. For example, subscribing __keyevent@0__:expired
to the key expiration event in database 0 is realized by subscribing. Example 1: Subscription key expiration event .
The format of the subscribed channel is composed of three parts: __<type>@<db>__:<event>
event type ( keyspace
or keyevent
), database (for example, database 0
), and event (for example expired
). For the name of the corresponding event, please refer to the command event chapter below .
In addition, you can also PSUBSCRIBE
subscribe to one or more channels matched by compound regular expressions through commands. For example, by subscribing __key*@*__:*
to all events in all databases in Redis.
Command event
Redis provides different events for many commands. In this article, we will select some of these commands and their corresponding events to introduce:
DEL
: Andel
event occurs when a key is deletedEXPIRE
,PEXPIRE
,EXPIREAT
AndPEXPIREAT
: When setting a positive number or expiration timestamp future time, then generateexpire
events, otherwise generatedel
an event (will be immediately deleted)SET
And similarSETEX
,SETNX
,GETSET
: generatingset
event, the use ofSETEX
it will produceexpire
eventsMSET
: Will generate anset
event for each keyLPUSH
,LPUSHX
AndRPUSH
,RPUSHX
: According to generate the direction of insertionlpush
orrpush
eventsRPOP
,LPOP
: Generaterpop
andlpop
event separately, if the last element in the list is removed, thedel
event will be generated at the same timeLSET
: Generate anlset
eventLREM
: Generate anlrem
event, also if the removed element is the last element in the list, andel
event will be generated at the same timeHSET
,HSETNX
As well asHMSET
: a generatinghset
eventHDEL
: Generate ahdel
event, and in the removal of Hou Haxi table is empty case generateddel
eventSADD
: Generate ansadd
eventSREM
: Generate asrem
event, and collection case is empty after removing generateddel
eventSMOVE
: The original key generationsrem
event and to generate a target keysadd
eventsSINTERSTORE
, ,SUNIONSTORE
:SDIFFSTORE
Generatesinterstore
,sunionstore
as well assdiffstore
event and result in the case of an empty set and the target key exists, it will generatedel
an eventZADD
: No matter how many elements are added, only onezadd
event is generatedZREM
: Whether to remove several elements only generate azrem
event, generated when the removal of an ordered set is emptydel
eventXADD
: Generatingxadd
event, the use ofMAXLEN
sub-command may simultaneously generatextrim
eventXDEL
: Generate anxdel
eventPERSIST
: If the expiration event associated with the corresponding key is successfully removed, anpersist
event is generated- Generate an
expired
event when the key expires maxmemory
Anevicted
event is generated when the key is eliminated after reaching the set memory value- ……
For more command related events, please refer to keyspace notification related documents
Example
Subscription key expiration event
redis1> SUBSCRIBE __keyevent@0__:expired
1) "subscribe"
2) "__keyevent@0__:expired"
3) (integer) 1
# redis2> SETEX greeting 1 "hello world"
# 等待1秒后:
1) "message"
2) "__keyevent@0__:expired"
3) "greeting"
Subscribe to all events
redis1> PSUBSCRIBE __key*@*__:*
1) "psubscribe"
2) "__key*@*__:*"
3) (integer) 1
# redis2> SET greeting "hello world"
1) "pmessage"
2) "__key*@*__:*"
3) "__keyspace@0__:greeting"
4) "set"
1) "pmessage"
2) "__key*@*__:*"
3) "__keyevent@0__:set"
4) "greeting"