“This is the 21st day of my participation in the August Text Challenge.

1. Publish and subscribe overview

Redis publish subscription (PUB/SUB) is a message communication model: the sender (PUB) sends the message and the subscriber (sub) receives the message.

The Redis client can subscribe to any number of channels. ,

Subscribe to publish message graph:

Instead of sending the message directly to a specific receiver (the client that receives the message), the sender (the client that sends the message) sends the message to a channel, which then forwards the message to all subscribers interested in the channel.

The sender doesn’t need to know anything about the subscriber, and the subscriber doesn’t need to know which client is sending it the message, it just follows the channel it’s interested in.

The decoupling of publisher and subscriber can greatly improve the scalability of the system and result in a more dynamic network topology.

The following figure shows channel Channel1 and the relationship between the three clients that subscribe to this channel — Client2, Client5, and Client1:

When a new message is sent to channel Channel1 via PUBLISH, the message is sent to the three clients that subscribed to it:

Common commands

The following table lists the common redis publish and subscribe commands:

The serial number Commands and Description
1 PSUBSCRIBE pattern [pattern …] Subscribe to one or more channels that conform to a given pattern.
2 PUBSUB subcommand [argument [argument…] Displays the status of the subscription and publishing system.
3 PUBLISH Channel Message Sends information to the specified channel.
4 PUNSUBSCRIBE pattern [pattern …] Unsubscribe all channels for a given mode.
5 SUBSCRIBE channel [channel …] Subscribe to a given channel or channels of information.
6 UNSUBSCRIBE channel [channel …] To subscribe to a specified channel.

Psubscribe command

The Redis Psubscribe command subscribes to one or more channels that conform to a given pattern.

Each pattern uses * as its card, such as IT * to match all channels that start with IT (it. News, it. Blog, it. Tweets, etc.). News.* Matches all channels that start with news. (news.it, news.global.today, etc.), and so on.

Return value: The received message.

UNSUBSCRIBE command

If no parameter is added to the UNSUBSCRIBE command, all channels are unsubscribed.

Publish subscribe test

1, subscribers subscribe to a channel, subscription will automatically monitor the channel

127.0.0.1:6379> subscribe game
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "game"(3)integer1)Copy the code

2. The sender sends a message to the channel

127.0.0.1:6379> publish wangzherongyaointeger) 1
127.0.0.1:6379> publish game chiji
(integer1)Copy the code

3. Subscribers receive messages

127.0.0.1:6379> subscribe game
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "game"(3)integer) 1, 1)"message"
2) "game"  # Which channel does the message come from
3) "wangzherongyao"  # Message content
1) "message"
2) "game"
3) "chiji"
Copy the code

4. Open another client where subscribers subscribe to a channel model

127.0.0.1:6379> SUBSCRIBE CCTV.* Reading messages... (press Ctrl-C to quit) 1)"psubscribe"
2) "cctv.*"(3)integer1)Copy the code

5. The sender sends a message to the channel

127.0.0.1:6379> publish cctl. 1 xinwen (integer) 1
127.0.0.1:6379> publish cctv.5 tiyu
(integer1)Copy the code

6. The subscriber can receive the message as long as it conforms to the CCTV.* format

127.0.0.1:6379> SUBSCRIBE CCTV.* Reading messages... (press Ctrl-C to quit) 1)"psubscribe"
2) "cctv.*"(3)integer) 1, 1)"pmessage"
2) "cctv.*"
3) "cctv.1"
4) "xinwen"
1) "pmessage"
2) "cctv.*"
3) "cctv.5"
4) "tiyu"
Copy the code

2. Principle of publishing and subscription

SUBSCRIBE channel subscription principle

When a client executes a SUBSCRIBE command to SUBSCRIBE to a channel or channels, the client establishes a subscription relationship with the channel to which it is subscribed.

Redis stores the subscription relationships of all channels in the pubsub_Channels dictionary of the server state. The key of this dictionary is a channel that is subscribed to, and the value is a linked list that records all the clients that subscribed to this channel.

The principle of PSUBSCRIBE

The server stores all of its subscriptions in the pubsub_channe property of the server state, and all of its schema subscriptions in the Pubsub_Patterns property of the server state.

The PUBsub_Patterns attribute is a linked list. Each node in the linked list contains a pubsubPattern structure whose pattern attribute records the subscribed pattern and whose client attribute records the clients that subscribed to the pattern.

Examples of the pubsub_Patterns property:

The linked list records the following information:

  • Client -7 is subscribing to CCTV.*
  • Client client-8 is subscribing to test_x.*
  • Test. y*”

When a client executes the PSUBSCRIBE command to subscribe to one or more patterns, the server performs two operations for each subscribed pattern:

  1. Create a new pubsubPattern structure, set the pattern attribute to the subscribed pattern (such as CCTV.*), and set the client attribute value to the subscribed client.
  2. Add a pubsubPattern structure to the end of the PUBSUB_Patterns list.

Principle of PUBLISH message publishing

When a client sends a message to a channel through a PUBLISH command, Redis first finds the node whose key is the channel in the pubsub_Channels dictionary in the structure redisServer, traverses the value of the node, finds all the clients, and sends the message to these clients.

The pubsub_Patterns in the structure redisServer is then traversed to find the node that contains the pattern for that channel and the message is sent to the client that subscribed to that pattern.

3. Application scenarios

1, real-time chat (chat room as a channel, subscribers send messages to the sender, the sender of the message back to everyone)

2. Real-time messaging system

3. Subscribe, follow the system…