Redis publishes and subscribes-like message queues, with senders sending messages to channels and channel subscribers receiving messages.
1. Publish and subscribe examples
First, start the first Redis client on the machine and subscribe to blog. Redis channel by executing the following command:
SUBSCRIBE "blog.redis"
Copy the code
Then, open a second Redis client on the machine and subscribe to the blog.redis channel with the same command:
Then, open a third Redis client and send a message to the blog.redis channel by executing the following command:
PUBLISH blog.redis "redis-in-action-01"
Copy the code
View client 1 and client 2, and the following information is displayed:
The relationship between the three clients and channels is shown in the figure below:
You can view the number of connected clients using the INFO clients command:
2. Subscribe/unsubscribe channels
2.1 Subscription Channels
The SUBSCRIBE command of Redis is used to SUBSCRIBE channels as follows:
SUBSCRIBE "blog.redis"
Copy the code
If you subscribe to multiple channels, you can use the following command:
SUBSCRIBE "blog.redis" "blog.rocketmq"
Copy the code
Redis stores the subscription relationship of all channels in the pubsub_Channels dictionary of the server state. The key of the dictionary is a channel that is subscribed to, and the value of the key is a linked list of all the clients that subscribed to this channel.
For example, client 1 and client 2 are subscribing to the channel blog.redis, and client 3 and client 4 are subscribing to the channel blog. rocketMQ.
If there is one client 5, run the following command:
SUBSCRIBE "blog.rocketmq" "blog.java"
Copy the code
Then the channel subscription relationship saved by the server state will become as shown below:
2.2 Unsubscribe channel
The UNSUBSCRIBE command of Redis is used to UNSUBSCRIBE a channel as follows:
UNSUBSCRIBE "blog.redis"
Copy the code
To unsubscribe multiple channels, you can use the following command:
UNSUBSCRIBE "blog.redis" "blog.rocketmq"
Copy the code
Assume that the channel subscription relationship saved by the server state is as shown below:
If client 5 runs the following command:
UNSUBSCRIBE "blog.rocketmq" "blog.java"
Copy the code
Then the channel subscription relationship saved by the server state will become as shown below:
3. Subscribe/unsubscribe mode
3.1 the sample
First, start a Redis client and subscribe to the mode “blog.r*” with the following command:
PSUBSCRIBE "blog.r*"
Copy the code
Next, start another Redis client and PUBLISH to the channel:
PUBLISH "blog.redis" "redis-in-action-01"
PUBLISH "blog.rocketmq" "rocketmq-in-action-01"
PUBLISH "blog.java" "java-in-action-01"
Copy the code
As you can see, the first client can receive the first two messages because the channels “blog.redis”, “blog.rocketMQ” match the pattern “blog.r * “:
But the channel “blog.java” does not match this pattern, so the last message sent was not received by the client.
3.2 Subscription Mode
The Redis PSUBSCRIBE command is used to subscribe to the schema as follows:
PSUBSCRIBE "blog.r*"
Copy the code
If you subscribe to multiple schemas, you can use commands like the following:
PSUBSCRIBE "blog.r*" "blog.j? va" "blog.j[ae]va"Copy the code
Redis stores all schema subscriptions in the pubSub_Patterns property of the server state.
The PUBsub_Patterns attribute is a linked list. Each node in the linked list has a pubsub_Pattern structure. The pattern attribute of this structure records the subscribed pattern and the client attribute records the clients that subscribed to the pattern.
For example, client 1 is subscribing to blog.r* and client 2 is subscribing to blog.j? Va.”
If there is one client (3), run the following command:
PSUBSCRIBE "blog.j[ae]va"
Copy the code
The schema subscription for server state saving will look like the following:
3.3 Unsubscribe mode
The Redis PUNSUBSCRIBE command is used to unsubscribe the mode as follows:
PUNSUBSCRIBE "blog.r*"
Copy the code
To unsubscribe multiple modes, you can use the following command:
PUNSUBSCRIBE "blog.j? va" "blog.j[ae]va"Copy the code
Suppose the schema subscription relationship for server state is now as follows:
If client 3 runs the following command:
PUNSUBSCRIBE "blog.j[ae]va"
Copy the code
The schema subscription for server state saving will look like the following:
4. Send the MESSAGE
If, the channel subscription relationship saved by the server state is as follows:
The schema subscription relationship for server state saving is shown below:
If a Redis client executes the following PUBLISH command:
PUBLISH blog.redis "redis-in-action-01"
Copy the code
The server then performs the following two actions:
- Send the message “redis-in-action-01” to all subscribers of the channel “blog.redis”
- Send the message “redis-in-action-01” to a subscriber that matches the pattern of channel “blog.redis”
That is, the message “redisin-action-01” is sent not only to the subscriber client 1 and client 2 of the channel “blog.redis”, but also to the subscriber client 5 of the pattern “blog.r*” matching the channel “blog.redis”.
5. View subscription information
You can use Redis’ PUBSUB command to view information about channels or modes.
5.1 Viewing Subscribed Channels
If you want to view the subscribed CHANNELS, you can use the PUBSUB CHANNELS [pattern] command, where the pattern parameter is optional:
- If pattern is not specified, all channels currently subscribed to the server are returned
- If pattern is specified, channels matching the pattern are returned from the subscribed channels of the server
This command is implemented by iterating through the pubsub_Channels dictionary of the server state.
For a concrete example, if the server state saved pubsub_channels dictionary looks like this:
PUBSUB CHANNELS returns something like this:
PUBSUB CHANNELS r* returns the following result:
5.2 Check the number of subscribers of the channel
To check the number of subscribers for a channel, use the PUBSUB NUMSUB [channel1 channel2… channeln] command.
This command is implemented by iterating through the pubsub_channels dictionary saved by the server state. The length of the list of subscribers corresponding to a channel is the number of subscribers for that channel.
For a concrete example, if the server state saved pubsub_channels dictionary looks like this:
The command PUBSUB NUMSUB blog.redis blog.rocketmq blog.java returns the following result:
5.3 Viewing the Number of Subscribed Modes
To view the number of subscribed schemas, use the PUBSUB NUMPAT command.
This command is implemented by returning the length of the pubsub_Patterns list that the server state holds.
For a concrete example, if the server state holds a pubsub_Patterns list like this:
The command PUBSUB NUMPAT returns something like this:
6. Summary
Redis publish-and-subscribe is somewhat similar to message queue publish-and-subscribe and consists of the following seven commands:
- SUBSCRIBE
- UNSUBSCRIBE
- PSUBSCRIBE
- PUNSUBSCRIBE
- PUBSUB CHANNELS
- PUBSUB NUMSUB
- PUBSUB NUMPAT
The core of each of these seven commands is based on the pubsub_Channels dictionary and pubsub_Patterns linked list stored in the server state.
Reference 7.
Redis Design and Implementation by Huang Jianhong
Note: If you think this blog has any mistakes or better suggestions, please leave a comment, I will follow up and correct the blog content in time!
This article continues to be updated, please pay attention to the wechat public number “Shencheng Strangers” for the first time to read!