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 binlog
synchronizing 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 consumer
consumptions, which will cause a sequence error. Reading data consumer
from MQ
it is orderly, but consumer
the execution time of each is not fixed. It cannot be guaranteed that the one who reads the message consumer
first must complete the operation first. In this way, the messages will not be executed in order, resulting in the wrong data order.
1.2.2 Confusion scene 2
One queue
corresponds to one consumer
, but consumer
multi-threaded consumption is carried out in it, which will also cause the wrong order of message consumption.
1.3 Guarantee the order of consumption of messages
1.3.1 Solution 1
Splitting it into multiple pieces queue
, each queue
of consumer
which 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.
1.3.2 Solution 2
Or just one queue
but corresponding to one consumer
, and then this consumer
is internally queued with a memory queue, and then distributed to the bottom layer worker
for processing