Writing in the front

A review guide is summarized on Github, the content is detailed, illustrated, there is a need to learn friends can Star! Making address:Github.com/ThinkingHan…

1.Redis publish and subscribe architecture

Redis provides publish and subscribe functions, which can be used for message transmission. The publish and subscribe mechanism of Redis consists of three parts, publisher, subscriber and Channel.

2.Redis publish and subscribe

(1) Send messages

Redis sends a message using a PUBLISH command, which returns the number of subscribers that received the message.

(2) Subscribe to a channel

Redis uses the SUBSCRIBE command to SUBSCRIBE to a channel, and its return value includes the channel to which the client subscribed, the number of channels to which it has subscribed, and the received messages. SUBSCRIBE indicates that the client has successfully subscribed to a channel.

(3) Pattern matching

The pattern matching function allows clients to subscribe to channels that match a certain pattern. Redis uses PSUBSCRIBE to subscribe to all channels that match a certain pattern. “” is used to represent the pattern, and” “can be replaced by any value.

(4) Unsubscribe

Redis uses UNSUBSCRIBE and PUNSUBSCRIBE commands to cancel subscriptions, which return values similar to subscriptions. Because Redis’s subscription operation is blocking, once a client subscribes to a channel or mode, it will remain subscribed until it exits. In SUBSCRIBE, PSUBSCRIBE, UNSUBSCRIBE, and PUNSUBSCRIBE, the return value contains the number of channels and modes that the client is currently subscribed to. When this number becomes zero, the client automatically unsubscribes.

3.Redis publishing and subscription implementation

Because Redis is an open source system, we can look at the internal implementation details through its source code.

(1) the SUBSCRIBE

(2) PSUBSCRIBE

(3) the PUBLISH

4.Redis publishes subscriptions in Redis

The Redis publish and subscribe function is irrelevant to the data storage in Redis. It does not affect the key space of Redis, that is, it does not affect the data stored in Redis. However, through the publish and subscribe mechanism, Redis also provides another function, namely Keyspace Notification, Allows clients to know if there are events that change data in Redis by subscribing to specific channels. For example, if a client deletes data with the key mykey in Redis, this operation will trigger two messages, mykey del, belonging to channel keysapce, indicating changes to the keyspace, and del Mykey, belonging to channel keyevent, indicating operations to be performed.

5. Comparison between Redis publishing subscription and ActiveMQ

(1) ActiveMQ supports a variety of messaging protocols, including AMQP, MQTT, Stomp, etc., and supports JMS specifications, but Redis does not provide support for these protocols; (2) ActiveMQ provides persistence function, but Redis cannot store messages persistently. Once a message is sent, if no subscriber receives it, the message will be lost; (3) ActiveMQ provides message transmission guarantee. When the client connection times out or transaction rollback occurs, the message will be re-sent to the client, while Redis does not provide message transmission guarantee. In a word, the functions provided by ActiveMQ are far more complex than Redis publish and subscription. After all, Redis is not dedicated to publish and subscription. However, if Redis is already in the system and basic publish and subscription functions are required, there is no need to install ActiveMQ. Because most of the functions provided by ActiveMQ are probably not needed, while Redis’ publish and subscribe mechanism can meet the needs.