Simple Message Queue (formerly MNS) supports ordered messages. This topic describes core concepts, differences from normal messages, creation and usage methods, API instructions, and related limits and best practices.
Feature definition
Simple Message Queue (formerly MNS) provides first-in-first-out (FIFO) processing for messages within the same group, ensuring sequential handling in business scenarios.
Scenarios
Ordered messages are ideal for business scenarios that require strict message sequencing:
-
Order status transitions
In E-commerce order systems, states such as order creation → payment → shipment → delivery confirmation must be processed in chronological order to maintain correct business state.
-
Financial transaction processing
In securities or stock trading systems, orders with identical bid prices must be processed strictly in the order they were placed. Downstream systems must handle these orders according to their submission sequence.
-
Database synchronization
Upstream databases perform insert, delete, or update operations. Binary operation logs are transmitted as messages to downstream systems, which replay them to refresh state data sequentially and avoid data inconsistency.
-
Business log recording
Logs of critical business operations must be stored strictly in chronological order to support auditing and troubleshooting.
Key concepts
-
MessageGroupId: A message group identifier. The system guarantees message order within each group: messages with the same
MessageGroupIdare processed strictly in sequence, while messages from different groups can be processed in parallel. -
Visibility timeout: After a message is successfully pulled, it remains invisible to other consumers until the timeout expires. This prevents multiple consumers from processing the same message simultaneously. If the message is not confirmed as deleted before the timeout, it becomes visible again.
-
Group locking mechanism: In an ordered queue, once a message with a specific
MessageGroupIdis pulled but not confirmed as deleted (i.e., remains invisible), all subsequent messages in the sameMessageGroupIdare blocked. This ensures strict consumption order. Only after the current message is confirmed as deleted or its visibility timeout expires can the next message in the same group be pulled.
Differences from normal messages
-
Ordering semantics:
-
Normal messages (standard queue/topic): Deliver at least once by best effort, with no guaranteed order.
-
Ordered messages (FIFO queue/topic): Within the same
MessageGroupId, messages are consumed strictly in the order they arrive at the server. Earlier messages must be fully processed before later ones can be consumed.
-
-
Concurrency model:
-
Normal messages: No grouping restrictions; optimized for overall throughput.
-
Ordered messages: Use
MessageGroupIdas the concurrency unit—serial within a group, parallel across groups.
-
-
Message requirements:
-
Sending an ordered message requires specifying a
MessageGroupId(applies to both queues and topics).
-
Getting Started
FIFO queue
-
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.
Other configurable parameters include Maximum message size, Long polling period, Message visibility timeout, Message retention period, Message delay time, Enable logging, Dead-letter policy, and Rate limiting policy.
-
Send messages:
-
Use
SendMessageand include aMessageGroupId. Use a stable business key such as an order ID. -
If you omit
MessageGroupId, the system returns error codeFifoMissingMessageGroupId.
-
-
Receive and confirm:
-
Use
ReceiveMessageto pull messages. The response includesMessageGroupId. After processing, callDeleteMessageto confirm.
-
FIFO topic
-
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:
Subscribe to a FIFO queue to ensure message ordering. Normal topics can only subscribe to standard queues. For details, see Step 3: Create a subscription.
-
Publish messages:
-
Use
PublishMessageand include aMessageGroupId. -
If you omit
MessageGroupId, the system returns error codeFifoMissingMessageGroupId.
-
-
Consume and confirm:
-
On the subscribed FIFO queue side, use
ReceiveMessageto pull messages andDeleteMessageto confirm, maintaining sequential processing within each group.
-
API usage
Queue-related APIs
|
API name |
Parameter description |
Return description |
|
CreateQueue-Create queue |
Input parameter
|
Creates a queue of the specified type. |
|
GetQueueAttributes-Get queue attributes |
No special parameters |
Returns queue information, including the |
|
No special parameters |
Returns a list of queues. Each queue includes the |
Topic-related APIs
|
API name |
Parameter description |
Return description |
|
CreateTopic-Create topic |
Input parameter
|
Creates a topic of the specified type. |
|
GetTopicAttributes-Get topic attributes |
No special parameters |
Returns topic information, including the |
|
No special parameters |
Returns a list of topics. Each topic includes the |
Message send/receive APIs
|
API name |
Parameter requirements |
Return Description |
Notes |
|
FIFO queues require |
Standard response. |
Sends a single message to a queue. |
|
|
FIFO queues require |
Standard response. |
Sends multiple messages to a queue in a batch. |
|
|
FIFO topics require |
Standard response. |
Publishes a message to a topic. |
|
|
No special parameters. |
The response body includes |
Receives a single message from a queue. |
|
|
Supports the |
The response body includes |
May return messages from multiple groups. Order is preserved within each group. |
Limits
-
Supported regions include China (Shenzhen), China (Shanghai), and China (Hangzhou). To request FIFO support in other regions, submit a ticket.
-
Group ID required: Sending messages to a FIFO queue or topic requires a
MessageGroupId. -
Type compatibility:
-
Dead-letter queues (DLQs): Only queues of the same type can be bound. FIFO messages can only bind to FIFO dead-letter queues, and normal messages can only bind to standard dead-letter queues.
-
Subscriptions: Standard topics can only subscribe to standard queues. For ordered delivery, subscribe FIFO topics to FIFO queues.
-
-
Peek not supported: FIFO queues do not support the message peeking interface (PeekMessage).
Best practices
-
Group design:
-
Use stable and evenly distributed
MessageGroupIdvalues (such as order IDs) to prevent a few hot-spot groups from becoming performance bottlenecks. -
For extremely hot-spot workloads, subdivide groups at the application layer to increase concurrency.
-
-
Visibility timeout: Set this based on actual processing duration to ensure your application completes processing and confirms the message before timeout, reducing duplicate deliveries.
-
Idempotency and retries: Implement idempotent processing and retry logic in consumers to ensure end-to-end consistency.
-
Batch receiving: Increase
numOfMessagesto improve throughput, as long as per-message processing delay remains acceptable.
FAQ
How is ordering defined?
Messages with the same MessageGroupId are delivered and processed strictly in send order. Messages from different groups can be processed in parallel.
Must I provide MessageGroupId?
Yes. Sending messages to a FIFO queue or topic requires MessageGroupId. Without it, ordering cannot be guaranteed.
How do I choose between FIFO and standard queues/topics?
Use FIFO queues or topics if your business requires strict ordering (such as order status transitions). Use standard queues or topics if you prioritize overall throughput and elasticity and do not require message ordering. To get started, create a FIFO Queue or FIFO Topic in the console, include MessageGroupId when sending, and read this attribute on the consumer side for ordered processing and confirmation.