This topic introduces the concepts, scenarios, and usage precautions of RocketMQ ordered messages.
Ordered messages (FIFO messages) are a type of messages provided by RocketMQ, which are delivered and consumed in a strict order. Ordered messages are both delivered and consumed in order.
Ordered publishing: A client sends messages of a specified topic in order.
Ordered consumption: A client receives messages of a specified topic in FIFO order.
All messages under a specified topic are published and consumed in the strict first-in-first-out (FIFO) order.
Globally ordered messages are typically used in scenarios where the performance requirements are not high, and all messages are delivered and consumed in strict FIFO order.
In securities processing, for RMB to USD exchange transactions of the same quote, earlier placed ones are processed first. In this scenario, you can deliver and consume messages of the topic in a globally ordered manner in FIFO mode.
All messages under a specified topic are partitioned by the sharding key. Messages in one shard are published and consumed strictly in the FIFO order. A sharding key is a key field used in ordered messages to distinguish different partitions. It is completely different from the key used in normal messages.
Typically used in scenarios where the performance requirements are high, the sharding key is used as the sharding field, and messages in one shard are delivered and consumed in strict FIFO order.
[Example 1] A verification code needs to be sent for user registration, so user ID is used as the sharding key. Messages sent by the same user will be delivered and subscribed in FIFO order.
[Example 2] Order IDs are used as the sharding key in the creation of e-commerce orders. Then, order creation messages, order payment messages, order refund messages, and order logistics messages of the same order are published and subscribed in FIFO order.
All internal e-commerce systems in Alibaba Group use partitionally ordered messages, which can ensure both service order and performance.
Different topic types are used for creating different types of messages in the RocketMQ console. The following table compares various types of topics.
|Topic type||Support transactional messages||Support scheduled and delayed messages||Performance|
|Unordered messages (normal, transactional, and scheduled and delayed messages)||Yes||Yes||Highest|
|Partitionally ordered messages||No||No||High|
|Globally ordered message||No||No||Moderate|
|Message types||Support reliable synchronous transmission||Support reliable asynchronous transmission||Support one-way transmission|
|Unordered messages (normal, transactional, and scheduled and delayed messages)||Yes||Yes||Yes|
|Partitionally ordered messages||Yes||No||No|
|Globally ordered message||Yes||No||No|
- Ordered messages do not support the broadcasting consumption mode.
- It is recommended that one group ID corresponds to only one type of topic, that is, one group ID is used for sending or receiving either ordered messages or unordered messages.
- Ordered messages do not support asynchronous transmission. Otherwise, the strict order is not ensured.
- For globally ordered messages, we recommend that the number of created instances is at least two. Running multiple instances simultaneously prevents service interruption when one work instance exits unexpectedly. When one work instance exits, another instance takes over the work immediately, ensuring that services are not interrupted. Only one instance works at a time.
Use Java SDK 1.2.7 or later.
The sample code can be found in the following documentation: