当前位置:网站首页>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
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-QYPwOc9W-1656468585459)(C:/Users/86158/AppData/Roaming/Typora/typora-user-images/image-20220629092442701.png)]](/img/74/e1cb383231f0a0de7f84a8f2d5831a.png)
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-GpVSVwBL-1656468585460)(C:/Users/86158/AppData/Roaming/Typora/typora-user-images/image-20220629092454345.png)]](/img/9b/458ec64d9257631922785b29218e8f.png)
When new news passes PUBLISH Command to channel channel1 when , This message will be sent to the three clients who subscribe to it :
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-BmRYSWOk-1656468585460)(C:/Users/86158/AppData/Roaming/Typora/typora-user-images/image-20220629092514496.png)]](/img/94/b05b1389171977ff454b7612079b0a.png)
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 .
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-B4RrhzqZ-1656468585461)(C:/Users/86158/AppData/Roaming/Typora/typora-user-images/image-20220629092747075.png)]](/img/a2/3367b90413121cb90b570f2cc1edcc.png)
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 .
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-WEWireSG-1656468585461)(C:/Users/86158/AppData/Roaming/Typora/typora-user-images/image-20220629095040384.png)]](/img/bc/aafa89128ada2f28d0b2ccf991f234.png)
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"
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-liCreDDl-1656468585462)(C:/Users/86158/AppData/Roaming/Typora/typora-user-images/image-20220629095644923.png)]](/img/16/7a2ef3a80ec3e920eca97eb367e56a.png)
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!

![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-OibXwJKi-1656468585462)(C:/Users/86158/AppData/Roaming/Typora/typora-user-images/image-20220629095811652.png)]](/img/4c/6ca5d00f2b77f74f73ebcccd93c3a2.png)
Put the left client ctrl+c After closing , When searching again on the right , Found that it was empty
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-MWIEDufB-1656468585462)(C:/Users/86158/AppData/Roaming/Typora/typora-user-images/image-20220629100055201.png)]](/img/b4/9e3f0c3504945af456323023e0c90a.png)
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 ...]
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-a1EsdK2y-1656468585463)(C:/Users/86158/AppData/Roaming/Typora/typora-user-images/image-20220629100645723.png)]](/img/91/909503bb5e9062dcec287eea491c46.png)
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 ...]]
边栏推荐
- Withdrawal of wechat applet (enterprise payment to change)
- Redis之五大基础数据结构深入、应用场景
- [OC foundation framework] - string and date and time >
- Selenium+pytest automated test framework practice (Part 2)
- Redis之Bitmap
- Connexion d'initialisation pour go redis
- Kratos战神微服务框架(三)
- Pytest参数化你不知道的一些使用技巧 /你不知道的pytest
- QML type: overlay
- Show slave status \ read in G_ Master_ Log_ POS and relay_ Log_ The (size) relationship of POS
猜你喜欢

The five basic data structures of redis are in-depth and application scenarios
![[three storage methods of graph] just use adjacency matrix to go out](/img/79/337ee452d12ad477e6b7cb6b359027.png)
[three storage methods of graph] just use adjacency matrix to go out

QML type: locale, date

Kratos ares microservice framework (I)
![[OC foundation framework] - [set array]](/img/b5/5e49ab9d026c60816f90f0c47b2ad8.png)
[OC foundation framework] - [set array]

Blue Bridge Cup_ Single chip microcomputer_ PWM output

Improved deep embedded clustering with local structure preservation (Idec)

KDD 2022论文合集(持续更新中)

SimCLR:NLP中的对比学习

Intel Distiller工具包-量化实现3
随机推荐
CSP salary calculation
The order of include header files and the difference between double quotation marks "and angle brackets < >
KDD 2022论文合集(持续更新中)
【图的三大存储方式】只会用邻接矩阵就out了
KDD 2022 paper collection (under continuous update)
CUDA realizes focal_ loss
Master slave replication of redis
Advance Computer Network Review(1)——FatTree
AcWing 2456. Notepad
Appears when importing MySQL
Global and Chinese market of airport kiosks 2022-2028: Research Report on technology, participants, trends, market size and share
BN folding and its quantification
An article takes you to understand the working principle of selenium in detail
在QWidget上实现窗口阻塞
Heap (priority queue) topic
BN折叠及其量化
Go redis initialization connection
一改测试步骤代码就全写 为什么不试试用 Yaml实现数据驱动?
Seven layer network architecture
数字人主播618手语带货,便捷2780万名听障人士