This is the 24th day of my participation in the August Text Challenge.More challenges in August

【Redis series 】 Redis learn nine, Redis publish and subscribe how to play

Redis publishes subscriptions

Redis publish subscription (PUB/SUB) is a message communication mode

  • The sender sends the message PUB
  • The receiver subscribes to the message sub

For example, wechat, Weibo and other attention systems

Redis clients can subscribe to any number of channels without limit

So let’s look at the diagram

  • Message publisher
  • Message subscribers
  • channel

In this case, the message publisher and the message subscriber are both Redis clients, the subscriber subscribs to a channel, the publisher publishes relevant information in the channel, such as articles, such as boiling points, etc., and the message subscriber can receive the content just sent by the publisher in real time

In the image below, 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

This message is sent to the three clients that subscribe 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 …]

View the subscription and publication system status.
3 PUBLISH channel message

Sends the message 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 unsubscribe from a given channel.

Actual testing and validation

  • subscribe channel [channel …]

Subscribe to one or more channels

  • PUBLISH channel message

Send a message to a channel

The receiver:

Subscribe xiaomotong channel, as long as the sender has a publish message to the channel, the receiver can immediately receive it

127.0.0.1:6379> subscribe xiaomotong
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "xiaomotong"
3) (integer) 1
1) "message"
2) "xiaomotong"
3) "hellowrold"
1) "message"
2) "xiaomotong"
3) "hello_redis"
1) "message"
2) "xiaomotong"
3) "xiaozhupeiqi"
Copy the code

The sender:

The sender sends message, Hellowrold, hello_redis, xiaozhupeiqi to Xiaomotong channel in turn

root@iZuf66y3tuzn4wp3h02t7pZ:~# redis-cli
127.0.0.1:6379> publish xiaomotong hellowrold
(integer) 1
127.0.0.1:6379> publish xiaomotong hello_redis
(integer) 1
127.0.0.1:6379> publish xiaomotong xiaozhupeiqi
(integer) 1

Copy the code

So how does this implementation work?

Realize the principle of

Redis is a single-process open source component implemented in C language

By analyzing the publish.c file in redis source code, we can understand the underlying implementation of Redis publish subscription, which can deepen our understanding of Redis

Redis implements publish and subscribe through commands such as publish, subscribe, and psubscribe

For example, wechat, which we all use:

subscribe

After subscribing to a channel, redis-server maintains a dictionary. The key of the dictionary is the name of the channel, and the value of the dictionary is a linked list of all clients that subscribe to the channel

A subscribe instruction adds a client to a channel’s subscription list

publish

Redis publishes messages to the channel. Redis server uses the given key as the channel name, keeps a list of all clients subscribing to the channel in the channel dictionary it maintains, traverses the list, and sends messages to all subscribers

pub / sub

Pub/sub: Publish and subscribe

In Redis, we can publish and subscribe messages to a certain key value. When a message is published on a key value, all clients subscribing to it will receive the message it just published. The most obvious use of this function is as a real-time messaging system

For example, we usually use chat system, instant messaging system and so on

But we need to be careful here

In order to realize the Redis cluster, all subscribed clients can receive published messages, but different clients may be connected to different master nodes, so Redis will broadcast all published messages to all nodes. If the published messages are large, the network overhead will be high, so it is necessary to avoid sending large messages

Redis publish/subscribe application scenario

1. Real-time messaging system

2, instant messaging, channel as a chat room, the message back to all subscribe to the channel

3, subscription system, concerned about the system is OK

For complex scenarios, forget about Redis and use professional MQ open source components such as rabbitMQ or Kafka

Points to note when using Redis to publish/subscribe

Publishing/subscribing using Redis is flawed

1. The reliability requirement for message processing is not strong

2. Consumption power does not need to be enhanced by increasing consumers

References:

redis_doc

Welcome to like, follow and favorites

Friends, your support and encouragement, I insist on sharing, improve the quality of the power

All right, that’s it for this time

Technology is open, our mentality, should be more open. Embrace change, live in the sun, and strive to move forward.

I am Nezha, welcome to like, see you next time ~