Redis (5) Publishing and Subscribing


1. What is publishing and subscribing?

Redis 发布订阅 (pub/sub) 是一种消息通信模式:发送者 (pub) 发送消息,订阅者 (sub) 接收消息。
Redis 的 subscribe 命令可以让客户端订阅任意数量的频道, 
每当有新信息发送到被订阅的频道时, 信息就会被发送给所有订阅指定频道的客户端。

insert image description here
Three "subscribers" subscribe to the "Channel1" channel:

insert image description here
When the background of Channel1 sends a data to the channel of Channel1, the three clients subscribed to Channel1 will receive this data at the same time:
insert image description here

2. Instructions and descriptions

Order describe
PSUBSCRIBE pattern [pattern …] Subscribe to one or more channels matching a given pattern
PUBSUB subcommand [argument [argument …]] View subscription and publishing system status
PUBLISH channel message Send information to specified channel
PUNSUBSCRIBE [pattern [pattern …]] Unsubscribe from all channels of a given pattern
SUBSCRIBE channel [channel …] Subscribe to a given channel or channels
UNSUBSCRIBE [channel [channel …]] Unsubscribe from the given channel. Note: If no channel is specified, all channels will be unsubscribed by default.

3. Instruction test

insert image description here
View active channels.
insert image description here
After subscribing to a channel through the SUBSCRIBE command, a dictionary is maintained in redis-server. The keys of the dictionary are each channel, and the value of the dictionary is a linked list. The linked list stores all clients that subscribe to this channel. The key to the SUBSCRIBE command is to add the client to the subscription list of a given channel.
insert image description hereWhen the client subscribes, it is linked to the end of the linked list of the corresponding channel. To unsubscribe, the client node is removed from the linked list.
shortcoming:

  • After the client subscribes to the channel, if the messages are not received in time, it may cause message accumulation. When the message accumulation threshold is reached (default value is 32MB), or reaches a certain level (default value is 8MB), after a period of time (default value is 1 minute), the server The client will automatically disconnect the client to avoid internal memory exhaustion.
  • When the connection is disconnected, the client needs to use subscribe or psubscribe to re-subscribe, otherwise it cannot continue to receive messages.
  • Redis' pubsub is not a reliable messaging system. When the client connection exits, or in extreme cases a master/backup switch occurs on the server, unconsumed messages will be discarded.

Application scenario:

1. Real-time communication message system
2. Personal blog system (fans follow the blogger, the blogger sends a blog, and subscribed users can listen to it)
3. In e-commerce, after the user successfully places an order, he sends a message to the designated channel, downstream Business subscription payment results This channel handles its own related business logic
Redis pubsub is not a reliable messaging system. If the scenario is complex, you can use message middleware, kafka RabbitMQ ActiveMQ RocketMQ...etc.

Guess you like

Origin blog.csdn.net/qq_45637894/article/details/130903886