How RabbitMQ guarantees the order of messages [Key points]

1.1 The meaning of guaranteeing order

If several messages in the message queue operate on the same data, these operations have a front and back relationship and must be executed in the order of the front and back, otherwise data exceptions will occur .

For example: For
  example, by mysql binlogsynchronizing data between two databases, since the data operations on the databases are sequential, if the order of operations is reversed, it will cause immeasurable errors. For example, if the database 插入->更新->删除operates , the order must be in this way. If the order of messages changes during the synchronization process, 删除->插入->更新the data that should have been deleted will not be deleted, resulting in data inconsistency.

1.2 Scenarios that appear out of order

We still take RabbitMQ as an example, and we will update more MQ solutions in the future.

1.2.1 Confusion scene one

① One queue, there are multiple consumerconsumptions, which will cause a sequence error. Reading data consumerfrom MQit is orderly, but consumerthe execution time of each is not fixed. It cannot be guaranteed that the one who reads the message consumerfirst must complete the operation first. In this way, the messages will not be executed in order, resulting in the wrong data order.

insert image description here

1.2.2 Confusion scene 2

One queuecorresponds to one consumer, but consumermulti-threaded consumption is carried out in it, which will also cause the wrong order of message consumption.
insert image description here

1.3 Guarantee the order of consumption of messages

1.3.1 Solution 1

Splitting it into multiple pieces queue, each queueof consumerwhich is just more queue, is indeed a troublesome point; this will also cause a decrease in throughput, and you can use multi-threading within the consumer to cancel the cost.
insert image description here

1.3.2 Solution 2

Or just one queuebut corresponding to one consumer, and then this consumeris internally queued with a memory queue, and then distributed to the bottom layer workerfor processing

insert image description here

Guess you like

Origin blog.csdn.net/weixin_42039228/article/details/123526391