当前位置:网站首页>Publish and subscribe to redis
Publish and subscribe to redis
2022-07-06 09:21:00 【~Pompeii】
Catalog
Redis Publish and subscribe to
1. Introduction to publication and subscription
Redis Publish subscribe (pub/sub) It's a message communication mode : sender (pub) Send a message , subscriber (sub) receive messages .
Redis Clients can subscribe to any number of channels .
Redis Provides the basis for 「 Release / subscribe 」 Message mechanism of pattern , In this mode , There is no direct communication between the publisher and the subscriber .
The picture below shows the channel channel1 , And three clients that subscribe to this channel —— client2 、 client5 and client1 The relationship between
When new news passes PUBLISH Command to channel channel1 when , This message will be sent to the three clients who subscribe to it :
2. Publish and subscribe application scenarios
Suppose we have such a business scenario , After placing an order on the website , Need to inform inventory service for shipment processing .
It's not difficult to implement the above business , We just need to make the inventory service available to the relevant customers , After the order is paid, just call the inventory service .
If there is a new business later , For example, the points service , He needs to get the results of the order payment , Then increase the user's points .
It's not hard to implement , Let the points service also provide an interface , After the order is paid, just call the inventory service .
If two businesses need to obtain the results of the order payment , That's OK , Program transformation is also fast . But with the continuous development of business , More and more new business is said to be the result of order payment .
At this time, we will find that there are many problems with the above system architecture :
First of all , The order payment business is heavily coupled with other businesses , Whenever a new business needs to pay for the result , You need to change the order payment business .
second , If there are too many calls , The response time of the order payment interface will be longer . in addition , If any downstream interface slows down , The synchronization will cause the response of the single payment interface to become longer .
Third , If any downstream interface fails , Conditions that may lead to data inconsistency . For example, the figure below , First call A, Call after success B, Last call C.
If you're calling B Interface exception , At this point, the order payment interface may fail to return , But at this time A The interface has actually been called successfully , This means that it has internally processed the result of successful order payment .
This will lead to A,B,C Three downstream interfaces ,A Get success get payment results , however B,C Didn't get , The situation that leads to the data inconsistency of the three systems .
Actually, let's think about it , For order payment business , It doesn't really need to care about the downstream call results , As long as there is some mechanism to inform them .
Here we are. , This requires the introduction of the publish subscribe mechanism today .
Use Redis Publish subscribe mechanism , For the above business , The order payment business only needs to send a message to the payment result channel , Other downstream businesses subscribe to the channel of payment results , You can get the message , Then make business processing .
In this way, the call relationship between upstream and downstream of the system can be decoupled .
3. example
The following example demonstrates how a publish subscription works . In our example, we created a subscription channel named redisChat:
SUBSCRIBE redisChat
Now? , Let's turn on a new one redis client , And then on the same channel redisChat Two news releases , Subscribers can receive messages .
The subscriber's client will display the following message
PUBLISH redisChat "Redis is a great caching technique"
PUBLISH redisChat "Learn redis by w3cschool.cc"
Pubsub The command is used to view subscription and publishing system status , You can see one
Be careful : Don't write as pubsub redisChat
PUBSUB CHANNELS!
Put the left client ctrl+c After closing , When searching again on the right , Found that it was empty
4.publish
Publish The command is used to send information to the specified channel .
Return value : Number of subscribers who received information .
PUBLISH channel message
5.subscribe
Subscribe The command is used to subscribe to the information of a given channel or channels .
We're using subscription commands , The main points need to be :
First of all , After the client executes the subscription instruction , You'll be in a subscription state , After that, you can only receive subscribe、psubscribe、unsubscribe、punsubscribe These four orders .
second , New subscription clients , I can't receive messages before this channel , This is because Redis It doesn't persist on published messages .
Return value : Information received
Compared with many majors MQ, such as kafka、rocketmq Come on , redis The publish and subscribe function is a little crude . however redis Publish and subscribe is better than simple , If the current scenario can tolerate these shortcomings , You can still choose to use .
SUBSCRIBE channel [channel ...]
6.psubscribe
Psubscribe Command to subscribe to one or more channels that conform to a given pattern .
Each pattern is represented by * As a match , such as it* Match all to it The first channel ( it.news 、 it.blog 、 it.tweets wait ). news.* Match all to news. The first channel ( news.it 、 news.global.today wait ), And so on .
Return the received information
PSUBSCRIBE pattern [pattern ...]
7.pubsub
Pubsub The command is used to view subscription and publishing system status , It consists of several subcommands in different formats .
Return value : A list of active channels
PUBSUB <subcommand> [argument [argument ...]]
8.unsubscribe
The command is used to unsubscribe information of a given channel or channels .
Return value : This command behaves differently in different clients .
UNSUBSCRIBE channel [channel ...]
9.punsubscribe
Punsubscribe The command is used to unsubscribe from all channels in a given mode .
Return value : This command behaves differently in different clients
PUNSUBSCRIBE [pattern [pattern ...]]
边栏推荐
- Redis core configuration
- 什么是MySQL?MySql的学习之路是怎样的
- LeetCode41——First Missing Positive——hashing in place & swap
- Advance Computer Network Review(1)——FatTree
- Redis' bitmap
- 【文本生成】论文合集推荐丨 斯坦福研究者引入时间控制方法 长文本生成更流畅
- AcWing 2456. Notepad
- QDialog
- Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges
- Using label template to solve the problem of malicious input by users
猜你喜欢
How to intercept the string correctly (for example, intercepting the stock in operation by applying the error information)
[oc]- < getting started with UI> -- learning common controls
CUDA实现focal_loss
Kratos ares microservice framework (I)
BMINF的後訓練量化實現
Intel Distiller工具包-量化实现3
In depth analysis and encapsulation call of requests
一篇文章带你了解-selenium工作原理详解
Kratos ares microservice framework (II)
Redis之哨兵模式
随机推荐
Solve the problem of inconsistency between database field name and entity class attribute name (resultmap result set mapping)
Pytest parameterization some tips you don't know / pytest you don't know
【图的三大存储方式】只会用邻接矩阵就out了
Global and Chinese market of capacitive displacement sensors 2022-2028: Research Report on technology, participants, trends, market size and share
注意力机制的一种卷积替代方式
LeetCode:387. The first unique character in the string
在QWidget上实现窗口阻塞
I-BERT
[oc]- < getting started with UI> -- common controls - prompt dialog box and wait for the prompt (circle)
Intel distiller Toolkit - Quantitative implementation 3
CUDA implementation of self defined convolution attention operator
Redis之连接redis服务命令
Advance Computer Network Review(1)——FatTree
KDD 2022论文合集(持续更新中)
Selenium+pytest automated test framework practice
What is MySQL? What is the learning path of MySQL
AcWing 2456. Notepad
Once you change the test steps, write all the code. Why not try yaml to realize data-driven?
Using label template to solve the problem of malicious input by users
[text generation] recommended in the collection of papers - Stanford researchers introduce time control methods to make long text generation more smooth