Skip to content
RocketMQ Interview Deep Dive: 8 Core Topics from Architecture to Message Backlog

RocketMQ Interview Deep Dive: 8 Core Topics from Architecture to Message Backlog

This comprehensive guide covers eight critical RocketMQ interview topics including architecture design, CommitLog storage, message sending/consumption modes, transaction messages, reliability mechanisms, duplicate consumption handling, and message backlog solutions with production-grade troubleshooting steps.

Rain's Path to Java Mastery
Rain's Path to Java Mastery
Rain's Path to Java Mastery

RocketMQ Architecture: NameServer, Broker, Producer, Consumer

RocketMQ uses a decentralized, master-slave architecture with four core components:

NameServer : Stateless registry providing routing metadata (Topic → Broker addresses, queue counts). Nodes do not communicate; each Broker heartbeats to all NameServers every 30s. Producers/Consumers cache routes locally and refresh periodically. Chosen over ZooKeeper for simplicity, zero coordination cost, and eventual consistency.

Broker : Message storage and forwarding core. Uses CommitLog + ConsumeQueue + IndexFile three-layer storage. Supports sync/async flush and sync/async replication. Master handles reads/writes; Slaves replicate for HA.

Producer : Fetches routes from NameServer, load-balances across queues (round-robin, key hash, fault delay). Supports sync, async, and one-way send with retry (default 2 retries, avoiding failed Broker).

Consumer : Pulls routes, registers consumer group, maintains long-polling TCP connection. Supports clustering (each message consumed by one instance) and broadcasting (all instances receive). Rebalance redistributes queues on membership changes.

Message Storage: CommitLog + ConsumeQueue

Design solves random-write vs sequential-read conflict:

CommitLog : Single global file (default 1GB) storing all messages sequentially. File name = starting physical offset. Write via mmap to PageCache; OS flushes asynchronously. Sequential write ~600MB/s.

ConsumeQueue : Per-Topic-per-Queue index file (fixed 20-byte entries: 8B CommitLog offset, 4B message size, 8B tag hash). Built asynchronously by background thread; rebuildable from CommitLog on restart. Enables O(1) lookup: consumer reads ConsumeQueue entry, then fetches message from CommitLog via sendfile (zero-copy).

Key advantages: extreme write throughput, horizontal scalability (new Topics only need lightweight ConsumeQueue), flexible consumption models, efficient tag filtering via hash, high durability (append-only CommitLog).

Message Send & Consume Modes

Send Modes (Producer)

Sync : Blocks until Broker ACK. Highest reliability, lower TPS (~thousands). Use for payments, orders.

Async : Returns immediately; callback on success/failure. High TPS (~tens of thousands). Use for logs, analytics.

One-way : Fire-and-forget, no callback. Highest TPS (~hundreds of thousands). Use for metrics, tracing where loss is acceptable.

Consume Modes (Consumer)

Clustering (default): Load-balanced across group; each message processed once. Offset stored on Broker.

Broadcasting : Every instance receives full message stream. No retry, no ordering. Use for config refresh, cache invalidation.

Ordered : Messages with same sharding key go to same queue; single-threaded consumption per queue. Partitioned ordering recommended (global ordering = 1 queue = low throughput). Requires MessageQueueSelector on send and MessageListenerOrderly on consume. Pitfalls: hotspot queues, duplicate consumption still possible.

Transaction Messages

Implements distributed transaction via half-message + 2PC + scheduled check:

Producer sends half-message to Broker (stored in RMQ_SYS_TRANS_HALF_TOPIC, invisible to consumers).

Producer executes local transaction (e.g., create order).

Producer sends Commit/Rollback. Commit moves message to real Topic; Rollback deletes half-message.

If Broker receives no confirmation, it triggers transaction check via TransactionListener.checkLocalTransaction (max 15 retries, default 60s interval). Implementation must be idempotent.

// Listener example
public class OrderTransactionListener implements TransactionListener {
  @Override
  public LocalTransactionState executeLocalTransaction(Message msg, Object arg) {
    try { createOrder(msg); return LocalTransactionState.COMMIT_MESSAGE; }
    catch (Exception e) { return LocalTransactionState.ROLLBACK_MESSAGE; }
  }
  @Override
  public LocalTransactionState checkLocalTransaction(MessageExt msg) {
    String orderId = msg.getKeys();
    return queryOrderStatus(orderId) ? LocalTransactionState.COMMIT_MESSAGE : LocalTransactionState.ROLLBACK_MESSAGE;
  }
}

Best practices: use business unique key (not msgId) for idempotency, handle UNKNOWN state, monitor RMQ_SYS_TRANS_OP_HALF_TOPIC for stuck transactions.

Reliability: Flush & Replication Strategies

Flush Disk

SYNC_FLUSH : Message persisted to disk before ACK. High reliability, low throughput. For finance.

ASYNC_FLUSH (default): ACK after PageCache write; background thread flushes every 500ms. High throughput, risk of losing last 500ms on power loss.

Master-Slave Replication

SYNC_MASTER : Master waits for Slave ACK. Zero data loss on Master failure, higher latency.

ASYNC_MASTER (default): Master ACKs immediately; Slave replicates asynchronously. Possible loss of recent messages if Master crashes.

Recommended combinations: async flush + async replication (logs), async flush + sync replication (general HA), sync flush + async replication (payments tolerating minor replica lag), sync flush + sync replication (zero-loss finance). RocketMQ 5.x DLedger (Raft) enables automatic failover.

Duplicate Consumption & Idempotency

RocketMQ guarantees At-Least-Once; duplicates arise from producer retries, consumer timeout re-delivery, and rebalance. Business impact: double payment, duplicate orders, inventory errors.

Idempotency formula: Unique Key + State Check + Atomic Operation. Use business key (order ID, payment flow) not msgId.

Solutions (ranked)

DB Unique Index : Composite unique key on message ID + business key. Strong consistency, DB pressure.

Redis Distributed Lock : SETNX with expiry. Good performance, lock expiry risk.

Redis Atomic Ops : INCR/SETNX+EX+NX. Best performance, simple checks only.

State Machine : Business status transitions (e.g., ORDER_CREATED → PAID). Natural idempotency.

Dedup Table : Separate table logging consumed keys. Universal fallback.

Best practice: Redis front-filter (SETNX+EX+NX, TTL = message retention + 1 day) + DB unique index as ultimate guard. Delete Redis key on business failure to allow retry. Set maxReconsumeTimes (e.g., 3) then route to DLQ for manual handling.

Message Backlog Handling

Root cause: consume speed < produce speed. Two types: transient spike vs chronic backlog.

Diagnosis (3 steps)

Check consumer status (mqadmin consumerProgress): down, restarting, thread blocked.

Measure consume TPS/RT: slow DB, RPC timeout.

Check produce TPS: traffic surge (promotions, flash sales).

Remediation by Scenario

Insufficient consume capacity : Add consumers (requires queue count >= consumer count). Increase consumeThreadMin/Max (2-4x CPU cores). Enable batch consume (consumeMessageBatchMaxSize=32). Async non-core logic, reduce IO.

Consume logic errors : Skip bad messages (log + return CONSUME_SUCCESS) as emergency; configure DLQ after max retries (default 16).

Traffic spike : Temporary consumer scale-out. Producer rate limiting. Degrade non-critical features.

Broker-Level

Increase MessageQueue count (restart consumers).

Disk expansion / IO tuning.

Switch to async flush + async replication for speed.

Monitoring: rocketmq_group_diff, rocketmq_message_left, consumer_tps/producer_tps. Alert on thresholds.

Prevention: message tracing, idempotent consumers, ordered message special handling (cannot scale consumers), cold/hot data separation. Golden rule: Rate-limit first to survive, scale-out to cure, DLQ as safety net, monitoring as compass.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Distributed SystemsHigh AvailabilityMessage QueueRocketMQIdempotencyInterview PreparationMessage BacklogTransaction Messages
Reader feedback

How this landed with the community

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.