Redis publish and subscribe mechanism

1. What is Redis

Redis is an open source in-memory database that stores data in the form of key-value pairs. Since the data is stored in memory, Redis is very fast, but every time the Redis service is restarted, the data in it will also be lost. Therefore, Redis also provides a persistent storage mechanism to save the data in a file in some form. , each time you restart, you can automatically load data from the file into the memory. 
write picture description here 
The architecture of Redis consists of two parts: Redis Client and Redis Server. The Redis client is responsible for sending requests to and receiving responses from the server. The server side is responsible for handling client requests, for example, storing data, modifying data, etc. 
Redis is often used as a database, cache, and messaging system.

2. Redis publish and subscribe

2.1 Redis publish-subscribe architecture

Redis provides the publish and subscribe function, which can be used for message transmission. The publish and subscribe mechanism of Redis includes three parts, publisher, subscriber and channel. 
write picture description here 
Both publishers and subscribers are Redis clients, and Channel is the Redis server side. Publishers send messages to a certain channel, and subscribers who subscribe to this channel can receive the message. This publish and subscribe mechanism of Redis is similar to the publish and subscribe based on the topic, and the Channel is equivalent to the topic.

2.2 Redis publish and subscribe function

(1) Sending a message 
Redis uses the PUBLISH command to send a message, and the return value is the number of subscribers who received the message. 
write picture description here 
(2) Subscribing to a channel 
Redis uses the SUBSCRIBE command to subscribe to a channel. The return value includes the channel subscribed by the client, the number of channels currently subscribed, and the received messages, where subscribe indicates that a channel has been successfully subscribed. 
write picture description here 
(3) Pattern matching 
The pattern matching function allows the client to subscribe to a channel that conforms to a certain pattern. Redis uses PSUBSCRIBE to subscribe to all channels that conform to a certain pattern. The pattern is represented by " ", and " " can be replaced by any value. 
write picture description here 
Assuming that the client subscribes to a certain mode and a channel that conforms to the mode at the same time, the message sent to this channel will be received by the client twice, but the types of the two messages are different, one is the message type, and the other is the message type. is of type pmessage, but its content is the same. 
(4) Unsubscribe 
Redis uses UNSUBSCRIBE and PUNSUBSCRIBE commands to unsubscribe, and the return value is similar to subscription. 
Since Redis's subscription operation is blocking, once a client subscribes to a channel or mode, it will remain in the subscribed state until it exits. In the SUBSCRIBE, PSUBSCRIBE, UNSUBSCRIBE and PUNSUBSCRIBE commands, the return value contains the number of channels and modes currently subscribed by the client. When this number becomes 0, the client will automatically exit the subscription state.

2.3 Redis publish and subscribe implementation

Since Redis is an open source system, we can view the internal implementation details through its source code. 
(1) SUBSCRIBE 
write picture description here 
When a client subscribes to a channel, Redis needs to bind the channel to the client. First of all, in the client structure client, there is an attribute pubsub_channels, which indicates all the channels subscribed by the client. It is a dictionary type, implemented through a hash table, and each element contains a key A pair of values ​​and a pointer to the next element, each subscription inserts a node into it, the key represents the channel to subscribe to, and the value is null. Then, in the structure redisServer representing the server side, there is also an attribute called pubsub_channels, but here it represents all the channels in the server side and the clients subscribed to this channel, it is also a dictionary type, when inserting a node , the key represents the channel, and the value is a linked list of all clients subscribed to the channel. Finally Redis notifies the client that its subscription was successful. 
(2) PSUBSCRIBE 
write picture description here 
When a client subscribes to a mode, Redis also needs to bind the mode to the client. First of all, in the structure client, there is an attribute pubsub_patterns, which represents all the patterns subscribed by the client. It is a linked list type. Each node includes the subscribed pattern and a pointer to the next node. Every time you subscribe to a schema, you insert a node into it. Then, in the structure redisServer, there is an attribute called pubsub_patterns, which represents all the patterns in the server and the clients that subscribe to these patterns. It is also a linked list type. When inserting a node, each node is To include the subscribed schema, as well as clients subscribed to this schema, and a pointer to the next node. 
(3) PUBLISH 
write picture description here 
When a client sends a message to a channel, Redis first finds the node whose key is the channel in pubsub_channels in the structure redisServer, and traverses the value of the node, that is, traverses all clients that subscribe to the channel, and traverses the value of the node. messages to these clients. Then, traverse the pubsub_patterns in the structure redisServer, find the node containing the pattern of the channel, and send the message to the client subscribed to the pattern.

2.4 Application of Redis publish and subscribe in Redis

The publish and subscribe function of Redis has nothing to do with the data storage in Redis. It will not affect the key space of Redis, that is, it will not affect the data stored in Redis, but through the publish and subscribe mechanism, Redis also provides another function, namely Keyspace Notification, which allows clients to know whether there are events that change the data in Redis by subscribing to a specific channel. For example, if a client deletes the data whose key is mykey in Redis, this operation will trigger two messages, mykey del and del mykey, the former belongs to the channel keysapce, indicating the change of the keyspace, and the latter belongs to the channel keyevent, indicating that the execution operate. 
write picture description here

2.5 Comparison between Redis publish and subscribe and ActiveMQ

(1) ActiveMQ supports a variety of message protocols, including AMQP, MQTT, Stomp, etc., and supports the JMS specification, but Redis does not provide support for these protocols; 
(2) ActiveMQ provides persistence, but Redis cannot store messages persistently , once the message is sent, if no subscribers receive it, the message will be lost; 
(3) ActiveMQ provides message transmission guarantee, when the client connection timeout or transaction rollback occurs, the message will be resent to the client , Redis does not provide message transmission guarantees. 
In a word, the functions provided by ActiveMQ are far more complicated than Redis publish and subscribe. After all, Redis is not dedicated to publish and subscribe, but if Redis already exists in the system and basic publish and subscribe functions are required, there is no need to install ActiveMQ, because Most of the functions provided by ActiveMQ may not be used, and the publish and subscribe mechanism of Redis can meet the needs.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325642969&siteId=291194637
Recommended