All Products
Search
Document Center

ApsaraMQ for RocketMQ:Ordered messages

Last Updated:Apr 16, 2025

Ordered messages are consumed in the same order in which they are sent. The earliest message that is sent is consumed as the first message, and the message that is sent next is consumed later. In most cases, ordered messages are used in scenarios that require messages to be consumed in the order in which they are produced. For example, ordered messages are frequently used in financial securities and e-commerce fields. This topic describes the terms, scenarios, working mechanism, and usage notes of ordered messages in ApsaraMQ for RocketMQ.

What are ordered messages?

Ordered messages are a type of messages provided by ApsaraMQ for RocketMQ. Messages of this type are published and consumed in a first in, first out (FIFO) order in a topic. The earliest published message is consumed as the first message.

Partitionally ordered messages

Messages in a specific topic are partitioned based on sharding keys. Messages in each partition are published and consumed in FIFO order. FIFO order applies only to messages in the same partition. Messages across partitions do not need to be consumed in FIFO order.

  • Scenarios

    Partitionally ordered messages are suitable for scenarios in which high performance is required and messages in each partition (distinguished using sharding keys) must be published and consumed in FIFO order.

  • Examples

    • If verification codes are required for user registration and user IDs are used as sharding keys, messages sent by the same user are published and consumed in FIFO order.

    • If you use order IDs as sharding keys when creating e-commerce orders, messages for the creation, payment, and refund 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 stable performance and sequential processing of online orders.

Working mechanism

This section describes how to ensure the order of messages in ApsaraMQ for RocketMQ.顺序消息1.0

To ensure that messages are sent and consumed in FIFO order in ApsaraMQ for RocketMQ, pay attention to the following phases:

  • Message publishing

    In the preceding figure, messages for Orders A and B are produced in the following sequence: A1, B1, A2, A3, B2, and B3. Messages for the same order must be sent and consumed in the same sequence in which they are produced. For example, messages for Order A are sent and consumed in the following sequence: A1, A2, and A3. Normal messages for Order A may be sent to different queues in polling mode. The original order of the normal messages cannot be retained in the queues. Ordered messages that use the same sharding key, such as the same order ID, are sequentially routed to the same queue in ApsaraMQ for RocketMQ.

    An ApsaraMQ for RocketMQ broker determines the order in which messages are generated based on the order in which the producer sends the messages. An ApsaraMQ for RocketMQ broker cannot determine the order of messages sent in parallel by different producers or threads.

  • Message storage

    Each logical queue in a topic of ordered messages corresponds to a physical queue, as shown in the preceding figure. When messages are sent to a logical queue in order, they are stored in the corresponding physical queue in the same order.

  • Message consumption

    ApsaraMQ for RocketMQ delivers messages to a consumer in the order in which they are stored. Then, the consumers consume the messages from the earliest received one.

    When consumers consume messages, messages that contain the same sharding key are consumed using a single thread. This helps ensure the consistency between the message consumption order and the message storage order, and the consistency between the message consumption order and the message publishing order.

Usage notes

  • Each group corresponds to a topic type. A group cannot be used to send or receive both ordered messages and unordered messages.

  • An ApsaraMQ for RocketMQ broker determines the order in which messages are generated based on the order in which the sender uses a single producer or thread to send messages. If the sender uses multiple producers or threads to concurrently send messages, the message order is determined by the order in which the messages are received by the ApsaraMQ for RocketMQ broker. This order may be different from the sending order on the business side.

FAQ

  • 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 message types.

  • In which regions can I use ordered messages?

    Ordered messages are supported in all Alibaba Cloud regions and Finance Cloud regions where ApsaraMQ for RocketMQ is available.

  • What transmission modes do ordered messages support?

    Ordered messages support only the reliable synchronous transmission mode that ensures strict message order. Asynchronous transmission is not supported.

  • Do ordered messages support clustering consumption and broadcasting consumption?

    Ordered messages support clustering consumption but not broadcasting consumption.

Sample code for TCP SDKs

For information about sample code for messaging over TCP, see the following documents:

Sample code for HTTP SDKs