Understanding RocketMQ Message Tracing: Concepts, Configuration, and Custom TraceTopic Implementation
This article explains RocketMQ's message tracing feature, covering its definition, key data attributes, core benefits, configuration steps for both normal and IO‑isolated modes, the underlying implementation mechanism, supported trace topics, and provides complete Java code examples for custom TraceTopic usage and verification.
Introduction
Hello, I am the author. A friend was interviewed at JD and was asked about RocketMQ message tracing. The interview question was "What is message tracing?" and the interviewer added that it is configured on the broker node.
1. What is Message Tracing
RocketMQ's message tracing is a feature that tracks the full‑link status of a message from production, storage, to consumption, recording the complete lifecycle to help quickly locate issues such as loss, duplicate consumption, or delay.
Message tracing data key attributes
Producer side
Consumer side
Broker side
Producer instance info
Consumer instance info
Message Topic
Send time
Delivery time, delivery attempts
Message storage location
Send success flag
Consume success flag
Message Key
Send latency
Consume latency
Message Tag
Core purposes of message tracing
Problem diagnosis – quickly determine whether a message was sent, stored, consumed, and why it failed.
Link tracing – view the path and timing of a message across producer, broker, and consumer.
Status monitoring – aggregate metrics such as send/consume latency and success rates.
Data audit – retain the full lifecycle for compliance.
2. Message Tracing Configuration
To enable tracing, add the following property: traceTopicEnable=true Broker‑side properties (excerpt from the official documentation) include:
brokerClusterName=DefaultCluster<br/>brokerName=broker-a<br/>brokerId=0<br/>deleteWhen=04<br/>fileReservedTime=48<br/>brokerRole=ASYNC_MASTER<br/>flushDiskType=ASYNC_FLUSH<br/>storePathRootDir=/data/rocketmq/rootdir-a-m<br/>storePathCommitLog=/data/rocketmq/commitlog-a-m<br/>autoCreateSubscriptionGroup=true<br/>## if msg tracing is open, the flag will be true<br/>traceTopicEnable=true<br/>listenPort=10911<br/>brokerIP1=XX.XX.XX.XX1<br/>namesrvAddr=XX.XX.XX.XX:9876RocketMQ supports two deployment modes for tracing:
Normal mode – tracing data shares the same broker nodes as regular business messages; simple to deploy but may cause IO contention under high throughput.
Physical IO isolation mode – tracing data is stored on dedicated broker nodes, avoiding resource competition and improving stability for large‑scale tracing.
3. Implementation Principle
Tracing works via:
Built‑in trace Topic (default RMQ_SYS_TRACE_TOPIC) for storing trace records.
Hook mechanism – producers and consumers intercept send/consume events, generate trace data, and forward it to the trace Topic.
Broker stores the trace Topic; users can query via the RocketMQ console or API.
Workflow summary:
Producer: send business message → hook records trace → async report → store in trace Topic.
Consumer: consume message → hook records trace → async report → store in trace Topic.
All trace records are consolidated in RMQ_SYS_TRACE_TOPIC for later inspection.
4. Trace Topic Support Modes
Two ways to store trace data:
Default built‑in trace Topic ( RMQ_SYS_TRACE_TOPIC), automatically created by RocketMQ.
Custom trace Topic defined by the user (e.g., MyApp_TraceTopic) for special storage or security requirements.
4.1 Default Built‑in Trace Topic
Name is RMQ_SYS_TRACE_TOPIC, created automatically.
Enabled by default when producers/consumers set setEnableMsgTrace(true).
Broker manages queues and storage automatically.
Suitable for quick validation or test environments.
4.2 Custom Trace Topic
Users can specify a custom Topic name such as MyApp_TraceTopic to store trace data.
Useful when trace data needs isolated clusters, sharding, or stricter security.
5. Custom TraceTopic Code Practice
5.1 Producer with Custom TraceTopic
import org.apache.rocketmq.client.producer.DefaultMQProducer;<br/>import org.apache.rocketmq.common.message.Message;<br/>import org.apache.rocketmq.remoting.common.RemotingHelper;<br/><br/>public class CustomTraceProducer {<br/> public static void main(String[] args) throws Exception {<br/> // 1. Create producer, enable tracing, specify custom trace topic<br/> DefaultMQProducer producer = new DefaultMQProducer("OrderProducerGroup", true, "MyApp_TraceTopic");<br/> producer.setNamesrvAddr("localhost:9876");<br/> producer.start();<br/><br/> try {<br/> // 2. Build business message<br/> Message msg = new Message("OrderTopic", "PayOrder", "Order_202307280001", "订单支付成功".getBytes(RemotingHelper.DEFAULT_CHARSET));<br/> // 3. Send message<br/> SendResult sendResult = producer.send(msg);<br/> System.out.println("Message send result: " + sendResult);<br/> } finally {<br/> // 4. Shut down producer<br/> producer.shutdown();<br/> }<br/> }<br/>}5.2 Consumer with Custom TraceTopic
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;<br/>import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;<br/>import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;<br/>import org.apache.rocketmq.common.consumer.ConsumeFromWhere;<br/><br/>public class CustomTraceConsumer {<br/> public static void main(String[] args) throws Exception {<br/> // 1. Create consumer, enable tracing, specify custom trace topic<br/> DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("OrderConsumerGroup", true, "MyApp_TraceTopic");<br/> consumer.setNamesrvAddr("localhost:9876");<br/> // 2. Subscribe business topic<br/> consumer.subscribe("OrderTopic", "PayOrder");<br/> consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);<br/> // 3. Register listener<br/> consumer.registerMessageListener((MessageListenerConcurrently) (msgs, context) -> {<br/> System.out.println("Received order message: " + new String(msgs.get(0).getBody()));<br/> return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;<br/> });<br/> consumer.start();<br/> System.out.println("Consumer started, waiting for order messages...");<br/> }<br/>}5.3 Trace Data Verification
Ensure the custom trace Topic exists (create manually if auto‑create is disabled):
sh mqadmin updateTopic -n localhost:9876 -t MyApp_TraceTopic -c DefaultClusterRun the producer to send a test message to OrderTopic; trace data will be written to MyApp_TraceTopic.
Query the trace using RocketMQ console or CLI:
sh mqadmin QueryMsgTraceById -n localhost:9876 -i "0A1234567890ABCDEF" # replace with actual MsgIDSigned-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.
IT Services Circle
Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.
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.
