This topic describes the concepts, scenarios, and use precautions of Message Queue for Apache RocketMQ ordered messages.

Ordered messages, also known as first-in-first-out (FIFO) messages, are a type of message provided by Message Queue for Apache RocketMQ, which are delivered and consumed in a strict order. Ordered messages are both published and consumed in order. Ordered publishing means that a producer publishes messages of a specified topic in order. Ordered consumption means that a consumer subscribes to the messages of a specific topic in the order that they are published, that is, the consumer receives the earliest message first.

Ordered messages are divided into Globally ordered message and Partitionally ordered message.

Globally ordered message

All messages under a specified topic are published and consumed in strict FIFO order.

  • Scenarios

    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.

  • Example

    In securities processing, for RMB to USD exchange transactions of the same quote, earlier placed ones are processed first. In this scenario, you can publish and consume messages of the topic in a globally ordered manner in FIFO mode.

Partitionally ordered message

All messages under a specified topic are partitioned by the shard key. Messages in one shard are published and consumed strictly in FIFO order. A shard key is a key field that is used in ordered messages to distinguish different shards. It is completely different from the key used in normal messages.

  • Scenarios

    Typically used in scenarios where the performance requirements are high, the shard key is used as the sharding field, and messages in one shard are delivered and consumed in strict FIFO order.

  • Examples
    • A verification code is required for user registration, and the user ID is used as the shard key. Messages sent by the same user are published and consumed in FIFO order.
    • Order IDs are used as the sharding keys when e-commerce orders are created. Then, order creation messages, order payment messages, order refund messages, and order logistics messages of the same order are published and consumed in FIFO order.

All internal e-commerce systems in Alibaba Group use partitionally ordered messages, which can ensure both service order and performance.

Comparison between globally ordered messages and partitionally ordered messages

Different topic types are used for creating different types of messages in the Message Queue for Apache RocketMQ console. The following table compares various types of topics.

Table 1. Message types
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
Table 2. Sending methods
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 message Yes No No
Globally ordered message Yes No No

Precautions

When using ordered messages, pay attention to the following:

  • Ordered messages do not support the broadcasting consumption pattern.
  • One group ID maps only one topic, that is, one group ID cannot be used for sending or receiving both ordered messages and unordered messages.
  • Ordered messages do not support asynchronous transmission. Otherwise, the strict order is not ensured.
  • For globally ordered messages, we recommend that you create at least two SDK instances. Running multiple instances simultaneously prevents service interruption when one instance exits unexpectedly. When one instance exits, another instance takes over the work immediately, ensuring that services are uninterrupted. Only one instance works at a time.

TCP SDK sample code

For the sample code for TCP, see the following documents: