In the process of using RabbitMQ, some business scenarios require us to ensure sequential consumption. For example, in a business scenario where three messages are generated to perform data addition, modification, and deletion operations, if the order of consumption is not guaranteed, the execution sequence may become delete, modify, add, thus resulting in disorder.

As shown below:

The issue of message sequencing in RabbitMQ needs to be considered in three aspects: the order of sending messages, the order of messages in the queue, and the order of message consumption.

Order of sending messages

The order of message transmission at the message sender end is not strictly required for most businesses. It doesn't matter who sends the message first. If a business requires that messages be sent in order, it means that only one message can be sent at a time with global locking, and messages cannot be sent concurrently.

Order of messages in the queue

In RabbitMQ, messages will ultimately be stored in a queue. Within the same queue, messages are ordered based on the first in, first out principle, which is guaranteed by RabbitMQ and usually does not need to be a concern for developers.

Note: The order of messages in different queues is not guaranteed. For example, when entering a subway station, if you join one of three queues, the order of entry into the station cannot be guaranteed between different queues.

Order of message consumption

When discussing how to ensure message sequencing, we are generally referring to the order in which consumers consume messages. In scenarios where multiple consumers consume messages from the same message queue, it is usually not possible to guarantee message sequencing. As indicated in the opening diagram, although the messages in the message queue are ordered, multiple consumers concurrently consuming messages, the speed at which messages are retrieved, the speed of executing business logic, and possible execution exceptions can all lead to inconsistent message sequencing.

For example: Messages A, B, and C enter the queue in sequence. Consumer A1 gets message A, and consumer B1 gets message B. However, if consumer B1 executes faster, it may complete before consumer A1, or if consumer A1 encounters an error, both scenarios can lead to inconsistent message sequencing.

The typical solution to the problem of consuming in sequence is for a queue to have only one consumer In this way, messages can be processed one by one in order. The downside is that the concurrent capability decreases, and concurrent message consumption is not possible. This is a trade-off.

Note: If the business requires sequential consumption and increased concurrency, a common approach is to enable multiple queues. The business can distribute messages to different queues based on rules, thus increasing the concurrency level. For example, in an e-commerce order scenario, it is only necessary to ensure the sequencing of order messages for the same user. Messages belonging to different users can be directed to different queues.