Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement publish and subscribe Model in Redis

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to achieve the publish and subscribe model in Redis. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.

Publish subscription (pub/sub) is a mode of message communication: the sender (pub) sends messages on a certain channel and the subscriber (sub) receives messages. The publish and subscription model is similar to Weibo follow. For example, if the blogger mango is followed by Zhang San, Li Si and Wang Wu, then when mango posts a Weibo post, Zhang and Li Wang will all see the Weibo in their followings.

So what are the similarities and differences between publishing and subscription and production and consumption? Production and consumption is mainly to generate a message that can only be consumed by one client, while publish subscription can be understood as publishing a message, which will be received by all clients in the channel, so sometimes our publish subscription is similar to broadcast.

Note that pubsub is a data structure.

Pub/Sub (publish and subscribe)

It was mentioned earlier that the client should subscribe to the channel first if it can receive the message, then the subscription command subscribe channel [channel.] in redis You can subscribe to multiple channels here. After subscribing, we need to post messages to this channel, publish channel message.

# subscribe to test Channel > subscribe testReading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "test" 3) (integer) 1 release information > publish test 'hello' (integer) 1

It can be broadcast, so let's set up another client.

Of course, redis also provides unsubscribe unsubscribe in the same way as subscribe.

PUBSUB command

PUBSUB is an introspective command to view the status of the subscription and publishing system, which consists of several subcommands in different formats.

PUBSUB CHANNELS [pattern] lists the current active channels. Active channels refer to those channels that have at least one subscriber, excluding clients in subscription mode.

The pattern parameter is optional:

If the pattern parameter is not given, all active channels in the subscription and publishing system are listed.

If the pattern parameter is given, only those active channels that match the given mode pattern are listed.

> pubsub channels1) "test"

PUBSUB NUMSUB [channel-1... Channel-N] lists the current active channels and the number of connections returned

> pubsub numsub test1) "test" # Channel 2) (integer) 2 # connections > pubsub numsub test11) "test1" 2) (integer) the number of subscription modes returned by 0PUBSUB NUMPAT.

Note that this command does not return the number of clients subscribing to the pattern, but the sum of all the patterns subscribed by the client, which can be understood as pattern matching, for example, subscribing to a test*, client that can receive test, test1, test2, and so on, see the following example.

# client > psubscribe test*Reading messages... (press Ctrl-C to quit) 1) "psubscribe" 2) "test*" 3) (integer) 1 > pubsub numpat (integer) 1

Pattern matching subscription

After introducing the general mode of publishing and subscribing, our partner asked, can I subscribe to someone who looks like mango? Of course, redis supports this kind of fuzzy subscription, and its command is psubscribe, which is consistent with the way subscribe is used.

> psubscribe test*Reading messages... (press Ctrl-C to quit) 1) "psubscribe" 2) "test*" 3) (integer) 1

PubSub principle

Until redis is a dictionary of key-value key-value pairs, and PubSub mentioned earlier as a data structure, how is it stored in memory? Look at the old man's drawing to answer.

From the figure, we can see that each channel is put into the dictionary array, and the subscribers of the corresponding channel are put into the linked list. When we send a publish command, the dictionary array first traverses to find the corresponding channel, then finds the corresponding subscription chain, and sends messages in turn.

So we can see that every time we send a message, we need to traverse this dictionary, that is to say, its execution time efficiency is O (n), but the purpose of our redis is to fast, reduce the execution of O (n) commands, which goes against our original intention.

Shortcomings of PubSub

The publisher of PubSub passes a message, and Redis will directly find the corresponding subscriber and pass it on. If there are no subscribers, the message will be discarded directly. If there are three subscribers at first and the third subscriber suddenly dies, the publisher will continue to send messages, and the other two subscribers can continue to receive messages, but when the dead subscribers reconnect, the message sent by the publisher during the disconnection is completely lost to this publisher.

If Redis stops and restarts, PubSub messages will not be persisted. After all, starting Redis is equivalent to not having a single subscriber, and all messages will be discarded directly. It is precisely because of these shortcomings that PubSub can hardly find suitable application scenarios in the field of message queuing.

So the author of Redis opened a separate project, Disque, to do multicast message queues, but the project is not yet mature and is in the Beta version.

Fill in the hole-Why Redis is not suitable for message queuing

1. It is difficult to guarantee the ACK of the message queue. There is no feedback process after the message is sent, and the message cannot be persisted.

two。 If the persistence of the message is guaranteed, then the performance must be lost. First of all, we need to store the message on disk, and then read the data from the disk to memory to operate. This process is very time-consuming.

The execution efficiency of 3.PubSub is low, and the execution efficiency is O (n), which violates the original intention of redis design.

4. It is difficult to implement complex message patterns

This is how to implement the publish and subscribe model in the Redis shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 265

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report