How RocketMQ Guarantees No Message Loss, Duplication, or Disorder
This article explains RocketMQ’s architecture, the roles of NameServer, Broker, Producer, Consumer, and how each component ensures reliable message delivery—covering synchronous, asynchronous, and one‑way sending, storage mechanisms, consumer retries, dead‑letter queues, installation steps, and Java client integration with code examples.
1. Introduction
RocketMQ is an open‑source, pure‑Java distributed messaging middleware from Alibaba, known for high throughput, low latency, high availability, and strong consistency. It is widely used in finance, e‑commerce, IoT, and many other large‑scale systems.
2. RocketMQ Model Overview
2.1 Core Components
NameServer : a lightweight service registry that stores routing information for Brokers; each NameServer is independent.
Broker : the core node that receives, stores, and forwards messages. A broker group contains a master (read/write) and slaves (read‑only).
Producer : sends messages to a Broker.
ProducerGroup : logical grouping of producers for load‑balancing and fault‑tolerance.
Consumer : pulls messages from a Broker.
ConsumerGroup : logical grouping of consumers; groups operate independently.
Topic : the basic unit for publishing and subscribing.
Tag : sub‑topic used to differentiate business types within a Topic.
Queue (MessageQueue) : a partition of a Topic where messages are stored.
Subscription : the consumer’s interest expression for a Topic.
Message : the payload with metadata such as ID, topic, tag, and timestamp.
2.2 Producer Message Reliability
The producer obtains Broker addresses from NameServer and sends messages according to the configured Topic. RocketMQ provides three sending modes:
Synchronous send : the producer blocks until the Broker returns a status. Successful sends receive a status code; failures trigger two automatic retries, which may cause duplicate delivery.
Asynchronous send : the producer returns immediately and receives the result via a callback. It also retries twice on failure, offering higher throughput but slightly lower reliability.
One‑way send : the producer does not wait for any response and performs no retries, giving the highest throughput but the lowest reliability.
2.3 Storage Reliability
Messages are persisted on local disks using several files:
CommitLog : the primary physical storage file (default 1 GB per file).
ConsumeQueue : a logical index for fast consumption (default ~5 MB per file).
IndexFile : supports keyword or time‑range queries.
Checkpoint : records the last flush timestamps for recovery.
When a producer sends a message, the Broker writes it to CommitLog, then updates ConsumeQueue and IndexFile. Consumers read offsets from ConsumeQueue and retrieve the actual payload from CommitLog. RocketMQ writes to page‑cache first and flushes to disk, optimizing both write and read paths. Even if ConsumeQueue is lost, the full CommitLog can reconstruct it, ensuring data integrity.
2.4 Consumer Reliability
RocketMQ guarantees at‑least‑once delivery. After a consumer successfully processes a message, it commits the offset; on failure, the broker retries up to 16 times with exponential back‑off (seconds to hours). If all retries fail, the message is moved to a dead‑letter queue (DLQ) whose name is prefixed with %DLQ%. Consumers can also request message backtracking by retaining messages in storage and pulling them based on time.
3. Installation
Download the binary package (e.g., version 4.6.0) from https://rocketmq.apache.org/download/ and unzip it.
Start the NameServer: nohup sh bin/mqnamesrv & Verify the log contains The Name Server boot success. serializeType=JSON.
Configure and start a Broker (edit conf/broker.conf to set namesrvAddr=localhost:9876), then run: nohup sh bin/mqbroker -c conf/broker.conf & Check the broker log for a success message such as
The broker[broker-a, 192.168.0.102:10911] boot success. serializeType=JSON and name server is localhost:9876.
Optionally, launch the RocketMQ Dashboard ( https://github.com/apache/rocketmq-dashboard) to monitor topics and messages.
4. Client Integration (Java)
4.1 Dependency
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
<version>4.7.1</version>
</dependency>4.2 Producer Example
public class ProducerMain {
public static void main(String[] args) throws Exception {
// Create a producer belonging to "myProducerGroup"
DefaultMQProducer producer = new DefaultMQProducer("myProducerGroup");
producer.setNamesrvAddr("127.0.0.1:9876");
producer.setSendMsgTimeout(60000);
producer.start();
Message msg = new Message("myTopic", "TagA", "我是一个同步消息 ".getBytes(RemotingHelper.DEFAULT_CHARSET));
SendResult sendResult = producer.send(msg);
System.out.printf("%s%n", sendResult);
producer.shutdown();
}
}Successful send logs a SendResult with status SEND_OK.
4.3 Consumer Example
public class ConsumerOne {
public static void main(String[] args) throws Exception {
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("myConsumerGroup");
consumer.setNamesrvAddr("127.0.0.1:9876");
consumer.subscribe("myTopic", "*");
consumer.registerMessageListener(new MessageListenerConcurrently() {
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
for (MessageExt msg : msgs) {
System.out.printf("消费消息:%s
", new String(msg.getBody()));
}
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
});
consumer.start();
System.out.printf("ConsumerOne Started.%n");
}
}When a message is consumed, the console prints the payload and the dashboard shows the consumer details.
5. Summary
RocketMQ delivers high performance and high availability for distributed systems. Its architecture—NameServer, Broker, Producer, Consumer—combined with synchronous/asynchronous/one‑way sending, durable storage (CommitLog, ConsumeQueue, IndexFile), and consumer retry/DLQ mechanisms ensures messages are not lost, duplicated, or delivered out of order. The article provides a step‑by‑step guide to install the server, configure NameServer and Broker, and integrate Java clients with concrete code snippets.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Pan Zhi's Tech Notes
Sharing frontline internet R&D technology, dedicated to premium original content.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
