当前位置:网站首页>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 ...]]
边栏推荐
- Once you change the test steps, write all the code. Why not try yaml to realize data-driven?
- 【文本生成】论文合集推荐丨 斯坦福研究者引入时间控制方法 长文本生成更流畅
- [shell script] - archive file script
- Blue Bridge Cup_ Single chip microcomputer_ PWM output
- Five layer network architecture
- Le modèle sentinelle de redis
- The carousel component of ant design calls prev and next methods in TS (typescript) environment
- Pytest's collection use case rules and running specified use cases
- Redis之Geospatial
- Kratos ares microservice framework (II)
猜你喜欢
基于B/S的影视创作论坛的设计与实现(附:源码 论文 sql文件 项目部署教程)
Servlet learning diary 7 -- servlet forwarding and redirection
Pytest's collection use case rules and running specified use cases
Advance Computer Network Review(1)——FatTree
[text generation] recommended in the collection of papers - Stanford researchers introduce time control methods to make long text generation more smooth
Redis之哨兵模式
KDD 2022 paper collection (under continuous update)
Digital people anchor 618 sign language with goods, convenient for 27.8 million people with hearing impairment
Le modèle sentinelle de redis
基于B/S的网上零食销售系统的设计与实现(附:源码 论文 Sql文件)
随机推荐
Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges
Leetcode: Jianzhi offer 04 Search in two-dimensional array
Pytest参数化你不知道的一些使用技巧 /你不知道的pytest
[OC-Foundation框架]--<Copy对象复制>
Advance Computer Network Review(1)——FatTree
Selenium+pytest automated test framework practice (Part 2)
Blue Bridge Cup_ Single chip microcomputer_ PWM output
Parameterization of postman
Global and Chinese market of linear regulators 2022-2028: Research Report on technology, participants, trends, market size and share
Connexion d'initialisation pour go redis
Intel distiller Toolkit - Quantitative implementation 1
Global and Chinese market of metallized flexible packaging 2022-2028: Research Report on technology, participants, trends, market size and share
I-BERT
A convolution substitution of attention mechanism
Leetcode problem solving 2.1.1
LeetCode:162. Looking for peak
数字人主播618手语带货,便捷2780万名听障人士
【shell脚本】——归档文件脚本
IJCAI2022论文合集(持续更新中)
Improved deep embedded clustering with local structure preservation (Idec)