当前位置:网站首页>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 ...]]
边栏推荐
猜你喜欢
Selenium+pytest automated test framework practice (Part 2)
注意力机制的一种卷积替代方式
Redis之五大基础数据结构深入、应用场景
【文本生成】论文合集推荐丨 斯坦福研究者引入时间控制方法 长文本生成更流畅
CUDA realizes focal_ loss
Nacos installation and service registration
A convolution substitution of attention mechanism
Redis cluster
[shell script] - archive file script
KDD 2022论文合集(持续更新中)
随机推荐
Implement window blocking on QWidget
Booking of tourism products in Gansu quadrupled: "green horse" became popular, and one room of B & B around Gansu museum was hard to find
甘肃旅游产品预订增四倍:“绿马”走红,甘肃博物馆周边民宿一房难求
【文本生成】论文合集推荐丨 斯坦福研究者引入时间控制方法 长文本生成更流畅
Selenium+pytest automated test framework practice (Part 2)
Improved deep embedded clustering with local structure preservation (Idec)
[daily question] Porter (DFS / DP)
Kratos ares microservice framework (III)
Seven layer network architecture
Different data-driven code executes the same test scenario
Redis geospatial
Advanced Computer Network Review(5)——COPE
运维,放过监控-也放过自己吧
QML type: overlay
Parameterization of postman
基于B/S的网上零食销售系统的设计与实现(附:源码 论文 Sql文件)
Servlet learning diary 8 - servlet life cycle and thread safety
注意力机制的一种卷积替代方式
Global and Chinese market of cup masks 2022-2028: Research Report on technology, participants, trends, market size and share
使用标签模板解决用户恶意输入的问题