Home >Database >Redis >Redis study notes publish and subscribe

Redis study notes publish and subscribe

WBOY
WBOYforward
2022-06-17 13:56:282018browse

This article brings you relevant knowledge about Redis, which mainly introduces issues related to publish and subscribe. Redis publish and subscribe (pub/sub) is a message communication mode: send The publisher (pub) sends the message, and the subscriber (sub) receives the message. Let's take a look at it. I hope it will be helpful to everyone.

Redis study notes publish and subscribe

Recommended learning: Redis video tutorial

Redis publish and subscribe (pub/sub) is a message communication model : The sender (pub) sends the message, and the subscriber (sub) receives the message.

Redis clients can subscribe to any number of channels.

Subscribe/publish message graph:

The first object: the message sender. Second object: channel. The third object: message subscriber.

Redis study notes publish and subscribe

The following figure shows the relationship between channel channel1 and the three clients that subscribe to this channel - client2, client5 and client1:

Redis study notes publish and subscribe

When a new message is sent to channel channel1 through the PUBLISH command, this message will be sent to the three clients that subscribe to it:

Redis study notes publish and subscribe

Command Description
Publish channel message Command Send the message to specified channel.
SUBSCRIBE channel [channel …] Subscribe to the information of one or more given channels.
UNSUBSCRIBE channel [channel …] refers to unsubscribing from a given channel.
[PUNSUBSCRIBE pattern [pattern …]] Unsubscribe from all channels of the given pattern.
[PUBSUB argument [argument …] View the subscription and publishing system status.
PSUBSCRIBE pattern [pattern …] Subscribe to one or more channels that match the given pattern.

Test

Subscriber:

127.0.0.1:6379> subscribe mianbao 			# 订阅频道 mianbao
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "mianbao"
3) (integer) 1
# 等待读取推送的消息
1) "message"		# 消息
2) "mianbao"		# 频道
3) "hello"			# 消息内容
1) "message"
2) "mianbao"
3) "hello redis"

Sender:

127.0.0.1:6379> publish mianbao "hello"			# 发送消息到频道		
(integer) 1
127.0.0.1:6379> publish mianbao "hello redis"
(integer) 1

Principle:

Redis is implemented using C. By analyzing the pubsub.c file in the Redis source code, we can understand the underlying implementation of the publishing and subscription mechanism, and deepen our understanding of Redis.

Redis implements publishing and subscribing functions through commands such as PUBLISH, SUBSCRIBE and PSUBSCRIBE.

After subscribing to a channel through the SUBSCRIBE command, a dictionary is maintained in redis-server. The keys of the dictionary are channels, and the value of the dictionary is a linked list. All subscriptions are stored in the linked list. The client of the channel. The key to the SUBSCRIBE command is to add the client to the subscription list of a given channel.

Send a message to the subscriber through the PUBLISH command. The redis-server will use the given channel as the key, search the linked list of all clients that subscribe to this channel in the channel dictionary it maintains, and traverse the linked list. , publish the message to all subscribers.

Pub/Sub literally means publishing (Publish) and subscription (Subscribe). In Redis, you can set up message publishing and message subscription for a certain key value. When a key value is used, After a message is published, all clients that subscribe to it will receive the corresponding message. The most obvious use of this function is as a real-time messaging system, such as ordinary instant chat, group chat and other functions.


For slightly more complex scenarios we will use message middleware: RabbitMQ, RocketMQ, ActiveMQ, kafka

Recommended learning: Redis video tutorial

The above is the detailed content of Redis study notes publish and subscribe. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete