当前位置:网站首页>MQ prevent message loss and repeated consumption
MQ prevent message loss and repeated consumption
2022-07-01 11:48:00 【Belief_ two hundred and seventy-three million nine hundred and 】
RabbitMQ How to prevent message loss and repeated consumption
One 、 Lost message
As shown in the figure ,RabbitMQ The lost message can be sent to any node . Remember the execution flow of the following figure

1.1、 The producer failed to send the message to MQ
The reason for the loss : Because of the instability of network transmission , When the producer is talking to MQ In the process of sending a message ,MQ Failed to receive message , But producers think MQ Successfully received message , The message will not be sent again , This leads to the loss of messages .
terms of settlement : There are two solutions : Affairs and mechanisms confirm Mechanism , The most common is confirm Mechanism ( Release confirmation mechanism )
Be careful :
1、RabbitMQ The transaction mechanism is synchronous , Very energy consuming , It will reduce RabbitMQ Throughput .
2、confirm The mechanism is asynchronous , After the generator sends a message , No need to wait RabbitMQ The callback , You can send the next message , When RabbitMQ After successfully receiving the message, it will automatically and asynchronously call back to an interface of the producer to return the success message .
The two mechanisms are described below :
confirm( Release confirmation ) Mechanism : RabbitMQ Can be opened confirm Pattern , Set on at the producer confirm After the pattern , Every time a producer writes a message, it assigns a unique id, If the message is successfully written RabbitMQ in ,RabbitMQ Will send back a message to the producer ack news , Tell you the news ok 了 . If RabbitMQ It didn't work , Will call back one of you nack Interface , Tell you the message failed to receive , Producers can once again report to RabbitMQ Send a message .
RabbitMQ Provides transaction functions , Open before producer sends data RabbitMQ Business channel.txSelect, And then send a message , If the message doesn't succeed RabbitMQ Received , Then the producer will receive an exception , At this point, you can roll back the transaction channel.txRollback, Then try sending the message again ; If you receive a message , Then you can commit the transaction channel.txCommit. The pseudocode is as follows :
//1、 Open transaction
channel.txSelect
try {
//2、 Send a message here
} catch (Exception e) {
channel.txRollback
//3、 Here is the message again
}
//4、 Commit transaction
channel.txCommit
1.2、RabbitMQ After receiving the message, the message is lost
The reason for the loss :RabbitMQ Received a message from the producer , It's in memory , If it is not sent to consumers , here RabbitMQ It's down. , So when you start again , The messages in the original memory are lost .
terms of settlement
1、 Turn on RabbitMQ The persistence of . When the producer successfully writes the message to RabbitMQ after ,RabbitMQ Just persist the message to disk . Combined with the above confirm Mechanism , If message persistence is enabled , Only after the message is successfully persisted to the disk , Will call back the producer's interface and return ack news , Otherwise, it's a failure
2、 After reboot , He will read messages from the disk , It will not lead to the loss of messages .
Persistent configuration :
1、queue Queue persistence , establish queue Set it to persistent , Even if you restart at this time rabbitmq queue , It still exists , But it won't last queue Of the data in .
2、Message Message persistence , When sending a message deliveryMode Set to 2, Set to persistent , here RabbitMQ It will persist the message to disk .
3、 Be careful : For persistence to work, you must set both persistence settings at the same time
1.3、 The consumer lost the message
The reason for the loss : If RabbitMQ Successfully sent the message to the consumer , that RabbitMQ Of ack The mechanism will automatically return success , Indicates that the message was sent successfully , This message will not be sent next time . But if at this time , The consumer hasn't finished processing the message yet , Then it went down , Then the message is lost .
The solution : Simply speaking , It must be closed RabbitMQ Automatic ack, Use manual ack( Message answering mechanism ). In this case , If you haven't dealt with it , No, No. ack 了 ! that RabbitMQ Think you're not done with it , When they arrive in Timeout time Not yet submitted , This is the time RabbitMQ Think you failed to deal with , Messages automatically rejoin the queue , Will allocate this consumption to others consumer To deal with . When the processing is successful rabbitmq Will delete the message
Message answering mechanism , Divided into two : Auto answer 、 Manual response .
Two 、 How to prevent repeated consumption
Under normal circumstances , When consumers are consuming news , After consumption , A confirmation message will be sent to RabbitMQ( If RabbitMQ I didn't receive it ), The message queue knows that the message has been consumed , The message is removed from the message queue .
Under special circumstances :
Because of network transmission and other failures , If this confirmation message is not sent back to RabbitMQ,RabbitMQ It may be thought that the consumer failed to handle , The message will be distributed to other consumers again .
The solution is : Keep the message unique , Even if it's multiple transmissions , Don't let the multiple consumption of news have an impact ; Ensure the idempotency of the message ;
1、 At the time of message production ,MQ Internally, a message is generated for each producer inner-msg-id, As a basis for de duplication and idempotency , Avoid duplicate messages entering the queue
2、 When the message is consumed , It is required that there must be a bizId( Globally unique : If paid ID、 Order ID etc. ) As a basis for de duplication and idempotency , Avoid repeated consumption of the same message .
So under the condition of ensuring stability : Better up 2 Do all of them , Then when you spend , Also put msgId Put in storage , In consumption, it is to query whether it has been consumed , If there is one, don't deal with it
Introduction to this article :
https://blog.csdn.net/wang_luwei/article/details/123613091
边栏推荐
- 博途V15添加GSD文件
- Wechat applet development - user authorization to log in to "suggestions collection"
- Learning summary on June 28, 2022
- 2022/6/29学习总结
- Mechanism and type of CPU context switch
- Comment Cao définit la décimale de dimension
- Getting started with Paxos
- Question: what professional qualities should test engineers have?
- Custom grpc plug-in
- 如何看懂开发的查询语句
猜你喜欢

Explore the contour detection function findcontours() of OpenCV in detail with practical examples, and thoroughly understand the real role and meaning of each parameter and mode

Botu V15 add GSD file

How to understand the developed query statements

Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS

图的理论基础

Neo4j Chinese developer monthly - issue 202206

Learning summary on June 30, 2022

MQ-防止消息丢失及重复消费

Learning summary on June 28, 2022

C summary of knowledge points 1
随机推荐
sshd_ Discussion on permitrotlogin in config
如何看懂开发的查询语句
Value/string in redis
TMUX usage
activity工作流引擎
ACLY与代谢性疾病
Getting started with Paxos
Mechanism and type of CPU context switch
JS日期格式化转换方法
Theoretical basis of graph
分享psd格式怎么预览的方法和psd文件缩略图插件[通俗易懂]
Continuous delivery -pipeline getting started
Wonderful! MarkBERT
redis中value/SortedSet
构建外部模块(Building External Modules)
2022/6/30学习总结
redis配置环境变量
redis常识
Learning summary on June 29, 2022
基于IMDB评论数据集的情感分析