Backend Development 8 min read

Ensuring Ordered Message Consumption in RocketMQ: Principles and Practices

This article explains why ordered message processing is crucial for business workflows, outlines how to maintain order during production, storage, and consumption in RocketMQ, and describes the locking mechanisms required to achieve reliable sequential consumption.

Architecture & Thinking
Architecture & Thinking
Architecture & Thinking
Ensuring Ordered Message Consumption in RocketMQ: Principles and Practices

1 Introduction

Message ordering is essential in many business scenarios, such as e‑commerce order creation → payment → completion, or ride‑hailing order acceptance → arrival → payment → completion. Maintaining strict order prevents issues and allows independent, parallel processing of different orders, making ordered processing in MQ systems an unavoidable topic.

2 Explanation of Message Ordering

Ordered execution is not a single component capability; the entire pipeline—from production, queuing, storage to consumption—must preserve order. This requirement applies to MQ systems like Kafka, RocketMQ, or RabbitMQ.

Production order: producers must send messages sequentially.

Queue order: messages must be enqueued in FIFO order.

Storage order: persisted messages should be stored sequentially to enable ordered consumption.

Consumption order: messages must be consumed sequentially, either globally (all messages in a topic) or partially (within a group).

Achieving global ordered consumption would require a single consumer, which severely impacts performance; therefore, partitioned ordered consumption is commonly used.

2.1 Production Order

To ensure overall order, producers must send messages in order. In RocketMQ, a Topic groups related messages, and a Topic can have multiple producers and consumers. Producers can direct related messages to the same queue by using consistent keys, such as order ID modulo the number of queues, or by assigning the same tag to a group of messages.

Business code should use synchronous sending to preserve order; asynchronous or multithreaded sending may break FIFO guarantees. If multithreading is needed, a lock combined with Spring's publish‑event mechanism can serialize events before they are persisted and sent to the MQ.

Even with these measures, failures in the broker or message loss can still cause out‑of‑order consumption.

2.2 Storage Order

RocketMQ stores messages using a file‑based mechanism similar to Kafka. Under ROCKETMQ_HOME , three files are used:

CommitLog – stores message metadata sequentially.

ConsumeQueue – logical queue indexing CommitLog entries per MessageQueue.

IndexFile – index for fast lookup.

All operations are sequential, and isolation by MessageQueue prevents disorder.

2.3 Consumption Order

RocketMQ provides two listener modes:

Ordered consumption – MessageListenerOrderly

Concurrent consumption – MessageListenerConcurrently

The ordered mode ensures messages are processed sequentially by using three locks:

Distributed lock on the broker side.

Local synchronized lock on the MessageQueue.

Local consumeLock on the ProcessQueue.

3 Summary

To achieve ordered message consumption, place messages that must be processed sequentially into the same queue (or partition), and when consuming with a thread pool, employ distributed and local locks so that only one thread processes a given queue at a time.

Backenddistributed systemsMessage Queuerocketmqordered messages
Architecture & Thinking
Written by

Architecture & Thinking

🍭 Frontline tech director and chief architect at top-tier companies 🥝 Years of deep experience in internet, e‑commerce, social, and finance sectors 🌾 Committed to publishing high‑quality articles covering core technologies of leading internet firms, application architecture, and AI breakthroughs.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.