当前位置:网站首页>MQ message queue is used to design the high concurrency of the order placing process, the generation scenarios and solutions of message squeeze, message loss and message repetition
MQ message queue is used to design the high concurrency of the order placing process, the generation scenarios and solutions of message squeeze, message loss and message repetition
2022-07-27 05:04:00 【WuSong1999】
Use message queue to design the order process with high concurrency
Use message queue to design the order process with high concurrency
Using message queues requires solving a core problem , That is how to ensure the reliability of the message , There are three solutions , In microservice architecture projects, businesses with high concurrency cannot be used seata To handle distributed transactions , So use message queuing to ensure the final consistency of data . Flexible transactions using message transactions : Reliable information + The final consistency plan ( Asynchronous assured )
Lost message
Send to mq The message of will be lost in those scenarios :
1、 Because of network fluctuations , The network flash outage caused the message not to arrive at the server , Didn't send out , It can be done at the place where the message is sent try catch, Make sure that the message will be sent , Log messages that fail to be sent , Whether the status of each message is received by the server should be recorded , And this message should be stored in a persistent database, such as mysql Database etc. . And regularly resend , If the message is not sent successfully , Scan the database regularly and resend unsuccessful messages .
Create one for each database mq_message Message table , Used to store sent messages , Save the details of each message to the database , After saving, as long as the sending fails , You can scan the database regularly and send the failed message again .
CREATE TABLE `mq_message` (
`message_id` char(32) NOT NULL, # The only news id
`content` text, # The content of the message convert to json
`to_exchane` varchar(255) DEFAULT NULL, # The switch sent to
`routing_key` varchar(255) DEFAULT NULL, # What is the routing key sent
`class_type` varchar(255) DEFAULT NULL, # Mark the type of message
`message_status` int(1) DEFAULT '0' COMMENT '0- newly build 1- Has been sent 2- Wrong arrival 3- Arrived ',
`create_time` datetime DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
PRIMARY KEY (`message_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
2、 The message arrived Broker,Broker To write a message to disk ( Persistence ) Success is considered. , here Broker Persistence has not been completed , Downtime .mq After the server receives the message, it must be persisted , Then send the message to the switch , The switch is handed over to the designated queue .
In addition to consumer information, there is a confirmation mechanism , There is also an acknowledgement mechanism for sending messages , Reliable arrival -ConfirmCallback
Message arrival Queue after , Only then can the message arrive successfully ,publisher The confirmation callback mechanism must also be added , Confirm the successful message , Then modify the status of the message corresponding to the database .
3、 Automatically ACK Under the state of , Consumers receive messages , But there was no time to consume the news , The consumer server goes down , Cause message loss , So be sure to turn on manual ACK, Only after successful consumption , Fail or fail before you have time to deal with it noAck Rejoin the team .
The above three points can actually belong to one point , That is to do a good job in the message confirmation mechanism ( Both the sender and the consumer should confirm the arrival and consumption of the message ), Every message sent is recorded in the database , Send the failed message again regularly .

Message arrival Queue after , Only then can the message arrive successfully ,publisher The confirmation callback mechanism must also be added , Confirm the successful message , Then modify the status of the message corresponding to the database .
3、 Automatically ACK Under the state of , Consumers receive messages , But there was no time to consume the news , The consumer server goes down , Cause message loss , So be sure to turn on manual ACK, Only after successful consumption , Fail or fail before you have time to deal with it noAck Rejoin the team .
The above three points can actually belong to one point , That is to do a good job in the message confirmation mechanism ( Both the sender and the consumer should confirm the arrival and consumption of the message ), Every message sent is recorded in the database , Send the failed message again regularly .
Duplicate message
Message repetition is a message sent to consumers twice , It is equivalent to that consumers have consumed twice
scene :
1、 After the consumer executes the core code , The transaction has been committed ,ack when , Server down , Result in not giving mq Respond to , No, ack success , Because it is opened manually ACK, So the message queue will think it failed ,Broker The message was re sent by unack Turn into ready, And send it to other consumers , Send the message again , But the consumer has finished executing the core code , This leads to repeated consumption
2、 Message consumption failed , Due to the retry mechanism , Automatically send the message again , This is a normal scene 
Message successful consumption , however ack Time goes down , Message by unack Turn into ready,Broker Re send to consumers , This leads to repeated message consumption , The solution is to design idempotent with the core business , Use the uniqueness of each message ID To record whether the core business has been executed ( After committing the transaction ), Make the message unique ID The status flag of is consumed , Consume again according to the unique ID To determine whether it can be consumed .
There's a backlog of news
Message backlog is , There are too many messages in the message queue , It will definitely affect mq Performance of , bring mq Performance degradation of
scene :
1、 Consumer downtime ,mq Not connected to any consumer , Or even too little , There are no consumers to consume news , Producers are also constantly generating messages
2、 The traffic sent is too large ,mq There are too many messages saved
solve :
1、 More consumers online , For normal consumption
2、 If the business volume is too large , Too much data , Then you can launch a consumer dedicated to processing messages , This consumer will mq Take out all the information inside , And then put it in the database , Write an offline processing business by yourself , Process messages in the database one by one
summary
Message loss is the most important thing to avoid , This one needs closed loop
Finally, the last message queue high concurrency design flowchart for the order placing process , as follows 
边栏推荐
- STM32_ HAL_ SUMMARY_ NOTE
- 【无标题】按照一定的条件,对 i 进行循环累加。条件通常为循环数组的长度,当超过长度就停止循环。因为对象无法判断长度,所以通常搭配 Object.keys() 使用。\nforEach 一般认为是 普
- 测试基础5
- Transaction database and its four characteristics, principle, isolation level, dirty read, unreal read, non repeatable read?
- 如何将Photoshop图层复制到其他文档
- Unit test Chapter6
- C中文件I/O的使用
- 不需手动安装cuda和cudnn,通过一行程序即可安装tensorflow-gpu,以tensorflow-gpu2.0.0,cuda10.0,cudnn7.6.5为例
- SVN使用详解
- [untitled] I is circularly accumulated under certain conditions. The condition is usually the length of the loop array. When it exceeds the length, the loop will stop. Because the object cannot judge
猜你喜欢

Comprehensive experiment of static routing

strlen和sizeof的区别

How to create an applet project

集成开发环境Pycharm的安装及模板设置

Hiding skills of Photoshop clipping tool

Photoshop提示暂存盘已满怎么办?ps暂存盘已满如何解决?

Cache read / write policies: cacheside, read/writethrough and writeback policies

Mysql表的约束

树莓派输出PWM波驱动舵机

ps样式如何导入?Photoshop样式导入教程
随机推荐
测试基础5
使用Photoshop出现提示“脚本错误-50出现一般Photoshop错误“
What if Photoshop prompts that the temporary storage disk is full? How to solve the problem that PS temporary storage disk is full?
Approval of meeting OA
标准对话框 QMessageBox
kali系统arp介绍(断网嗅探密码抓包)
事件过滤器
Introduction to Web Framework
老子云携手福昕鲲鹏,首次实现3D OFD三维版式文档的重大突破
Solution: read the files with different names in the two folders and deal with the files with different mappings
JS tips
二维数组求和 练习
Plane conversion (displacement, rotation, scaling)
How to import PS style? Photoshop style import tutorial
How do I reset Photoshop preferences? PS method of resetting preferences
[search] two way search + A*
节流函数的demo——正则表达式匹配
.htaccess learning
Advantages of smart exhibition hall design and applicable industry analysis
Network protocol details: IP