当前位置:网站首页>Message queue learning -- Concepts
Message queue learning -- Concepts
2022-07-27 03:54:00 【Scarecrow 0.0】
come from : Message queue core knowledge point e-book . The author's personal official account 「yes Training strategy 」
1、 What is message queuing
In the field of Computer Science , Message queues and mailboxes are software engineering components , It is usually used for thread communication between processes or within the same process . They deliver messages through queues - Transmit control information or content , Group communication system provides similar functions .
Message queue ( Message middleware ) It is a component that uses queues to communicate .
2、 What message queuing does
In essence, it is because of the rapid development of the Internet , Business is expanding , Enabling technology architecture to evolve .
It's often used to implement : Asynchronous processing 、 Service decoupling 、 flow control .
2.1、 Asynchronous processing
The longer the call link , The slower the response . Compared with inventory deduction and order generation , Point service and SMS service are unnecessary “ In time ”. Therefore, only after the order generation process , Throw a message into the message queue and you can directly return the response . Moreover, the point service and SMS service can consume this message in parallel .
Message queuing can Reduce waiting for requests , It also allows services to be processed asynchronously and concurrently , Improve the overall performance of the system .

2.2、 Service decoupling
The downstream system of orders is constantly expanding , In order to cater to these downstream systems, order services need to be modified frequently , Any change of downstream system interface may affect the order service . Therefore, message queuing is generally used to solve the problem of coupling between systems , The order service inserts order related messages into the message queue , The downstream system subscribes to this topic whoever wants it . In this way, order service is liberated !

2.3、 flow control
The request of the gateway is put into the message queue first , The back-end service does its best to consume requests in the message queue . Timeout requests can return errors directly .
Some background tasks , No need to respond in time , And the business processing is complex and the process is long , Then the incoming requests are put into the message queue first , Back end services are handled at their own pace .
The above two situations respectively correspond to the situation that producers produce too fast and consumers consume too slowly , Message queues can play a good buffer effect .

2.4、 Be careful
The stability of a middleware system will be reduced by one more layer , The difficulty of operation and maintenance is raised to a higher level . So weigh the pros and cons , The system is evolving .
3、 Message queuing model
There are two models of message queuing : The queue model and Release / Subscription model . RabbitMQ Queue model is adopted , RocketMQ and Kafka Adopt release / Subscription model .
3.1、 The queue model
The producer sends a message to a queue , A queue can store messages from multiple producers , A queue can also have multiple consumers , But there is a competitive relationship between consumers , That is, each message can only be consumed by one consumer .

3.2、 Release / Subscription model
Release / The subscription model sends messages to a Topic That is, in the subject , All subscribed to this Topic All subscribers can consume this message . Release / The subscription model is compatible with the queue model , That is, when there is only one consumer, it is basically consistent with the queue model .

The following content is based on Publishing / Subscription model
4、 Common terms
Producer: The party that sends the production message is called the producerConsumer: The party receiving the consumption information is called the consumerBroker: Message queuing server
Message from Producer Sent to Broker , Broker Store messages locally , then Consumer from Broker Pull the news , perhaps Broker Push message to Consumer , Last consumption .

To improve concurrency , Often publish / The subscription model also introduces the concept of queues or partitions . That is, the message is sent to a queue or partition under a topic . RocketMQ Call the queue , Kafka It's called zoning , The essence is the same .
For example, there is... Under a topic 5 A queue , Then the concurrency of this topic is increased to 5 , At the same time, there can be 5 Multiple consumers consume messages on this topic in parallel . Generally, polling or key hash Strategies such as remainder are used to allocate messages of the same subject to different queues .
The corresponding consumers generally have the concept of group Consumer Group , That is, consumers belong to a certain consumption group . A message will be sent to multiple consumer groups that subscribe to this topic .
Suppose there are two consumption groups, namely Group 1 and Group 2 , They're all subscribed to Topic-a . At this time, a message is sent to Topic-a , Then both consumer groups can receive this message .
Then the message is actually written to Topic In a queue , A consumer in the consumption group consumes a queue of messages .
Physically, in addition to copies , A message in Broker There will only be one in , Each consumer group will have its own offset That is, the consumption point is used to identify the consumption location . The news before the consumption point indicates that it has been consumed . Of course, this offset It's queue level . Each consumer group maintains a subscription Topic Of each queue under offset .
Take a look at the picture, it should be very clear .

5、 How to ensure that messages are not lost
There are three stages , Namely Production news 、 Store and consume messages . We start from these three stages to ensure that messages will not be lost . Ensuring the reliability of messages requires Three parties cooperate .

producer Need to deal with
BrokerResponse , Use retry in case of error 、 Alarm and other means .BrokerNeed to control the timing of response , In the case of a single machine, the response is returned after the message swipes the disk , Cluster multiple replicas , That is, if it is sent to two or more copies, the response will be returned .consumerYou need to return the response to after executing the real business logicBroker.
Be careful : Message reliability is enhanced , Performance drops , Wait for the message to brush the disk 、 Returning after multiple copies are synchronized will affect performance . So it depends on the business , For example, it doesn't matter if one or two logs are lost during log transmission , Therefore, there is no need to wait for the message to brush the disk before responding .
6、 How to handle duplicate messages
In normal business Message repetition is inevitable , So we can only from Another way to solve the problem of duplicate messages .
Idempotent handles duplicate messages : Idempotent is a mathematical concept , We understand that calling the same interface multiple times with the same parameters is the same as calling the same interface once .
For example, this article SQL
update t1 set money = 150 where id = 1 and money = 100;
How many times money All are 150, This is called idempotent . Therefore, it is necessary to transform the business processing logic , So that the final result will not be affected in the case of repeated messages .
Database data version control :version Version number control .
update t1 set money = 150 where id = 1 and version = 1; # After the update version + 1
7、 How to ensure the order of the message
7.1、 Overall order
If you want to ensure the global order of messages , First, only one producer can go to Topic Send a message , And one Topic There can only be one queue inside ( Partition ). The consumer must also consume the queue in a single thread . Such messages are globally ordered !
But generally, we don't need global order , Even synchronization MySQL Binlog You only need to ensure that the single table messages are in order .

7.2、 Partial order
Therefore, most of the ordered requirements are partially ordered , Partially ordered, we can put Topic Internally divided into the number of queues we need , Send messages to a fixed queue through a specific policy , Then each queue corresponds to a single threaded consumer . In this way, some orderly requirements are completed , The message processing efficiency can be improved by the concurrency of the number of queues .

There are several producers in the picture , A producer can also , As long as similar messages are sent to the specified queue .
边栏推荐
- Binary tree (day 82)
- 回归测试:意义、挑战、最佳实践和工具
- Characteristics and determination scheme of Worthington pectinase
- Redis源码学习(33),命令执行过程
- [Android synopsis] kotlin multithreaded programming (I)
- Plato Farm有望通过Elephant Swap,进一步向外拓展生态
- 百融榕树数据分析拆解方法
- A new paradigm of distributed deep learning programming: Global tensor
- FastBoot brush machine
- Do you really understand code rollback?
猜你喜欢

Characteristics and determination scheme of Worthington pectinase

On the first day of Shenzhen furniture exhibition, the three highlights of Jin Ke'er booth were unlocked!

Contour detection based on OpenCV (1)

Connman introduction

Spark Learning Notes (V) -- spark core core programming RDD conversion operator

Solution to Chinese garbled code in console header after idea connects to database to query data

redis入门练习

A new paradigm of distributed deep learning programming: Global tensor

Meta Quest内容生态总监谈App Lab设计初衷

The function and application of lpci-252 universal PCI interface can card
随机推荐
Spark: ranking statistics of regional advertising hits (small case)
03.获取网页源代码
easyui中textbox在光标位置插入内容
Plato farm brings a new experience to community users through the LAAS protocol elephant swap
Characteristics and experimental suggestions of abbkine abfluor 488 cell apoptosis detection kit
How can you access the domestic server and overseas server quickly with one database?
04.在谷歌浏览器中安装模拟浏览器ChromeDriver的详细步骤
Duplicate disc: what are the basic attributes of an image? What do you know about images? What are the parameters of the image
Vector to SVG method
若依的环境的部署以及系统的运行
Explain工具实际操作
Deployment of ruoyi's environment and operation of the system
Will this flinkcdc monitor all tables in the database? Or the designated table? I look at the background log. It monitors all tables. If it monitors
[tree chain dissection] 2022 Hangzhou Electric Multi school 21001 static query on tree
面试题:String类中三种实例化对象的区别
C language force deduction question 43 string multiplication. Optimized vertical
C语言入门实战(12):求自然常数e的值
Ming min investment Qiu Huiming: behind the long-term excellence and excess, the test is the team's investment and research ability and the integrity of strategy
Spark: calculate the average value of the same key in different partitions (entry level - simple implementation)
Insert pictures and videos in typera