[database] redis (5) — redis publish and subscribe

Time:2021-12-31

Redis publish and subscribe

Redis publish subscribe model is a simple model provided by redisMessage subscriptionandMessage consumptionThe model can be implemented simplyMQ analog communicationThe function of. That is, redis can be used asMessage subscription and publishingIt is not only a cache, but also a non relational database.

1.1. Introduction to redis publishing and subscription

Redis publish / subscribe is a message communication mode: the publisher (public) sends messages and the subscriber (sub) receives messages.

Redis client can subscribe to any number of channels.

 

The following figure shows the relationship between channel 1 and the three clients subscribing to this channel – client2, client5 and cliet1:

When a new message is sent to channel 1 through the publish command, the message will be sent to the three clients subscribing to it:

1.2. Redis publish subscribe common commands

  • subscribe chanel[,chanel2]

Indicates the channel information corresponding to the subscription. The corresponding client is the subscriber (consumer):

127.0.0.1:6379> SUBSCRIBE kuangshenshuo
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "kuangshenshuo"
3) (integer) 1
  • public message

Indicates that the corresponding message message is published. The sender (producer) of the corresponding message:

127.0.0.1:6379> PUBLISH kuangshenshuo hello,kuangshen
(integer) 1
127.0.0.1:6379> PUBLISH kuangshenshuo hello,maria
(integer) 1
127.0.0.1:6379> PUBLISH kuangshenshuo "hello,redis"
(integer) 1

You can see that the message receiver under the corresponding channel has received the message from the publisher:

 More command References:Redis publish subscribe command

1.3. Redis publishing and subscription principle

principle

Redis implements publish and subscribe functions through publish, subscribe, psubscribe and other commands.

After subscribing to a channel through the subscribe command,A dictionary is maintained in redis serverThe keys of the dictionary are channelsThe value of the dictionary is a linked listAll clients subscribing to this channel are saved in the linked list。 The key to the subscribe command is toThe client is added to the subscription linked list of the given channel

Send a message to subscribers through the publish command. Redis server will use the given channel as the key, find the linked list of all clients subscribing to the channel in the channel dictionary maintained by it, traverse the linked list, and publish the message to all subscribers.

Pub / sub literally means publish and subscribe. In redis, you can set a key value for message publishing and message subscription. When a key value is published, all clients subscribing to it will receive corresponding messages. The most obvious use of this function is as a real-time message system, such as ordinary instant chat, group chat and other functions.

Usage scenario

  • Real time message system

  • Real time chat (the channel can be used as a chat room and the information can be echoed to everyone)

  • Subscribe, pay attention to the system

 

Blog writing reference:

Rookie tutorial – redis publish and subscribe

Crazy God says redis