This topic describes the concepts, scenarios, and usage notes of ordered messages in Message Queue for Apache RocketMQ. In addition, the benefits of Ordered Message 2.0 are introduced.
What are ordered messages?
In Message Queue for Apache RocketMQ, ordered messages, also known as first-in-first-out (FIFO) messages, are messages that are delivered and consumed in a strict order. A producer publishes the messages of a topic in an order, and a consumer subscribes to these messages in the same order. The consumer receives the earliest message first.
Ordered Message 2.0
Message Queue for Apache RocketMQ provides Ordered Message 2.0. This version has the following benefits:
- High concurrency: The leaderless architecture greatly improves the ordered message throughput on brokers.
- High availability: The leaderless architecture supports improved load balancing policies for brokers and self-adaptive recovery of faulty brokers.
- Balanced distribution of messages: Improved load balancing policies eliminate the problem of unbalanced message distribution on brokers.
- Ordered Message 2.0 is available only for Message Queue for Apache RocketMQ Enterprise Platinum Edition instances.
- Ordered Message 2.0 requires version Release notes or later of the TCP Java SDK.
Globally ordered messages
All messages under a topic are published and consumed in strict FIFO order.
Globally ordered messages are typically used in scenarios where the performance requirements are not high and all messages must be published and consumed in strict FIFO order.
Consider a securities processing scenario where the topic is RMB to USD exchange transactions. If the bid price is the same for multiple transactions, earlier transactions are processed first. In this scenario, the system must publish and consume messages under the topic in a global FIFO order.
Partitionally ordered messages
All messages under a specified topic are partitioned by a shard key. The messages in each partition are published and consumed in strict FIFO order. A shard key is a key field that is used in ordered messages to distinguish different partitions. It is completely different from the key used in normal messages.
Partitionally ordered messages are typically used in scenarios where the performance requirements are high and messages in each partition must be published and consumed in strict FIFO order.
- Consider a scenario where a verification code is required for user registration and the user ID is used as the shard key. The messages sent by the same user are published and consumed in FIFO order.
- In an e-commerce scenario, the order ID is used as the shard key. Then, messages about the creation, payment, delivery, and refund status of the same order are published and consumed in FIFO order.
All internal e-commerce systems in Alibaba Group use partitionally ordered messages to ensure service order and performance.
Comparison between globally ordered messages and partitionally ordered messages
The following tables compare the topic types that are used for ordered messages.
|Message type||Transactional messages||Scheduled and delayed messages||Performance|
|Unordered messages (normal, transactional, scheduled, and delayed messages)||Supported||Supported||Highest|
|Partitionally ordered messages||Not supported||Not supported||High|
|Globally ordered messages||Not supported||Not supported||Medium|
|Message type||Reliable synchronous transmission||Reliable asynchronous transmission||One-way transmission|
|Unordered messages (normal, transactional, scheduled, and delayed messages)||Supported||Supported||Supported|
|Partitionally ordered messages||Supported||Not supported||Not supported|
|Globally ordered messages||Supported||Not supported||Not supported|
Note the following points when you use ordered messages:
- Each group ID is used for only one type of topic. A single group ID cannot be used to send or subscribe to both ordered messages and unordered messages.
- We recommend that you avoid congestion for globally ordered messages. You can run multiple instances simultaneously to provide service continuity. If the active instance fails, another instance can immediately take over, ensuring that services are uninterrupted. Only one instance is active at a time.
- Can a single message be an ordered message, a scheduled message, and a transactional
message at the same time?
No. Ordered messages, scheduled messages, and transactional messages are different and mutually exclusive.
- In which regions can I use ordered messages?
Ordered messages are supported in all Alibaba Cloud regions and AntCloud regions in which Message Queue for Apache RocketMQ is available.
- Why is the performance of globally ordered messages unsatisfactory?
Globally ordered messages are processed in strict FIFO order. If a message is not consumed, the next message will remain in the topic queue and cannot be processed. To improve the performance of globally ordered messages, upgrade your instance configuration and ensure that the application on the message client processes local service logic as quickly as possible.
- What methods are supported for sending ordered messages?
Ordered messages support only the reliable synchronous transmission method, which ensures strict message order. Asynchronous transmission is not supported.
- Do ordered messages support clustering consumption and broadcasting consumption?
Ordered messages support clustering consumption but do not support broadcasting consumption.