Simple Message Queue (formerly MNS) supports ordered messages. This topic describes the core concepts of ordered messages, their differences from normal messages, how to create and use them, API instructions, limits, and best practices.
Feature definition
Ordered messages in Simple Message Queue (formerly MNS) provide first-in, first-out (FIFO) delivery for messages within the same group. This ensures that messages are processed in the correct sequence for your business scenarios.
Scenarios
Ordered messages are ideal for business scenarios that require strict message ordering:
Order status transitions
In an e-commerce order system, statuses such as order creation, payment, shipping, and delivery confirmation must be processed chronologically to ensure the business state remains accurate.
Financial transaction processing
In securities and stock trading systems, if trade orders have the same bid price, they must be processed based on a first-bid, first-trade principle. The downstream system must process these orders in the sequence they were placed.
Database synchronization
An upstream database performs create, update, and delete operations. It sends binary operation logs as messages to a downstream system. The downstream system applies these operations from the messages to refresh its state and prevent data inconsistencies.
Business log recording
Logs for critical business operations must be saved in strict chronological order to facilitate future audits and troubleshooting.
Key concepts
MessageGroupId: The identifier for a message group. The system uses this group ID to guarantee message order. Within the same
MessageGroupId, messages are processed in strict order. Messages in different groups can be processed in parallel.Visibility Timeout: After a message is successfully pulled, it becomes invisible to other consumers until the timeout period expires. This prevents the message from being processed by multiple consumers simultaneously. If the message is not confirmed for deletion, it becomes visible again after the timeout.
Group locking mechanism: In an ordered queue, after a message with a specific
MessageGroupIdis pulled, it enters an invisible state and blocks subsequent messages that have the sameMessageGroupId. This ensures strict consumption order. The next message in the same group can be pulled only after the current message is deleted or its visibility timeout expires.
Differences from normal messages
Order semantics:
Normal messages (standard queues/topics): Provide at-least-once delivery. Order is not guaranteed by default.
Ordered messages (ordered queues/topics): Within the same
MessageGroupId, messages must be consumed in the strict order in which they arrive at the server. A message can be consumed only after the preceding message is processed.
Concurrency model:
Normal messages: No group limits. Emphasizes overall throughput.
Ordered messages: Use
MessageGroupIdas the unit of concurrency. Messages are processed serially within a group and in parallel between groups.
Message requirements:
When sending an ordered message, you must specify a
MessageGroupId. This applies to both queues and topics.
Getting started
Ordered queues (FIFO queues)
Create a queue:
Log on to the SMQ console.
In the left-side navigation pane, choose Queue Model > Queues.
In the top navigation bar, select a region.
On the Queues page, click Create Queue.
In the Create Queue panel, set Queue Type to FIFO Queue, configure other parameters, and then click OK.

Send a message:
Use
SendMessageand include aMessageGroupId. Use a business primary key, such as an order ID, for the group ID.If you do not include a
MessageGroupId, the error codeFifoMissingMessageGroupIdis returned.
Receive and confirm a message:
Pull the message using
ReceiveMessage. The response body contains theMessageGroupId. After processing the message, callDeleteMessageto confirm.
Ordered topics (FIFO topics)
Create a topic:
Log on to the SMQ console.
In the left-side navigation pane, choose Topic Model > Topics.
In the top navigation bar, select a region.
On the Topics page, click Create Topic.
In the Create Topic panel, set Topic Type to FIFO Topic, configure other parameters, and then click OK.

Create a subscription:
Create a subscription that pushes messages to an ordered queue to ensure the message order is maintained. Subscriptions for standard topics can only push messages to standard queues. For more information, see Step 3: Create a subscription.
Publish a message:
Use
PublishMessageand include aMessageGroupId.If you do not include a
MessageGroupId, the error codeFifoMissingMessageGroupIdis returned.
Consume and confirm a message:
On the subscribed ordered queue, pull the message using
ReceiveMessageand confirm it usingDeleteMessage. This maintains the processing order within the group.
API instructions
Queue-related APIs
API name | Parameter description | Response description |
CreateQueue-Create Queue | Request parameter
| Creates a queue of the specified type. |
GetQueueAttributes-Get Queue Attributes | No special parameters | Returns queue information, including the |
ListQueue-Lists all queues for a specified Alibaba Cloud account | No special parameters | Returns a list of queues. Each queue includes the |
Topic-related APIs
API name | Parameter description | Response description |
CreateTopic-Create topic | Request parameter
| Creates a topic of the specified type. |
GetTopicAttributes-Get topic attributes | No special parameters | Returns topic information, including the |
ListTopic-Lists topics in an Alibaba Cloud account | No special parameters | Returns a list of topics. Each topic includes the |
Message sending and receiving APIs
API name | Parameter requirements | Response description | Notes |
For an ordered queue, you must include a | Standard response. | Sends a single message to a queue. | |
For an ordered queue, you must include a | Standard response. | Sends messages to a queue in a batch. | |
For an ordered topic, you must include a | Standard response. | Publishes a message to a topic. | |
No special parameters. | The response body includes the | Receives a single message from a queue. | |
Supports the | The response body includes the | May contain messages from multiple groups. The order within each group is maintained. |
Limits
This feature is currently available in the China (Shenzhen), China (Shanghai), and China (Hangzhou) regions. To use ordered messages in other regions, submit a ticket.
Group ID required: When sending a message to an ordered queue or topic, you must include a
MessageGroupId.Type matching:
Dead-letter queue (DLQ): A DLQ must be the same type as its source queue. For example, an ordered queue can only be attached to an ordered DLQ, and a standard queue can only be attached to a standard DLQ.
Subscription: A subscription for a standard topic can only push messages to a standard queue. To ensure message order, a subscription for an ordered topic must push messages to an ordered queue.
Peek not supported: Ordered queues do not support the PeekMessage API.
Best practices
Group design:
Use a stable and evenly distributed
MessageGroupId, such as an order ID, to avoid performance bottlenecks caused by a few hot spot groups.For businesses with extreme hot spots, consider subdividing groups at the business layer to increase the degree of concurrency.
Visibility timeout: Set the timeout based on the actual processing time. This ensures that business operations are completed and confirmed before the timeout, reducing the probability of redelivery.
Idempotence and retries: Implement idempotent processing and a retry mechanism for consumers to ensure end-to-end consistency.
Batch pulling: Increase the
numOfMessagesvalue to improve throughput, while ensuring that the processing delay for a single message is acceptable.
FAQ
How is order defined?
Messages within the same MessageGroupId are processed in the strict first-in, first-out (FIFO) order in which they arrive at the server. Messages in different groups can be processed in parallel.
Is a MessageGroupId required?
Yes. You must include a MessageGroupId when you send a message to an ordered queue or topic. Otherwise, the processing order cannot be guaranteed.
How do I choose between an ordered and a standard queue/topic?
If your business relies on strict ordering, such as for order status transitions, choose an ordered queue or topic. If you are more concerned with overall throughput and elasticity and do not require strict ordering, use a standard queue or topic. To get started, create an Ordered Queue or Ordered Topic in the console, include a MessageGroupId when you send messages, and then use this property on the consumer side to process and confirm the message.