Common Message Queues: RabbitMQ, RocketMQ, and Kafka – Components, Features, and Best Practices
This article introduces the core concepts, components, exchange types, reliability mechanisms, ordering, delay, transaction, high‑availability, and load‑balancing strategies of three popular message‑queue systems—RabbitMQ, RocketMQ, and Kafka—while also discussing common challenges such as message ordering, delayed consumption, reliability, idempotence, and backlog handling.
RabbitMQ
RabbitMQ is a broker‑based messaging system. Its main components are:
Broker : a RabbitMQ instance.
Virtual Host (vhost) : similar to a MySQL database; isolates queues, exchanges, bindings, and permissions.
Exchange : routes messages from producers to queues.
Queue : stores messages until they are consumed.
Binding : defines the relationship between an exchange and a queue using a routing key.
Channel : a lightweight TCP multiplexing layer to avoid the cost of opening/closing sockets.
Connection : the TCP link between client and broker.
Publisher and Consumer : message producer and consumer.
Message : consists of a header (routing‑key, priority, etc.) and a body.
Exchange Types
Direct : routes when the routing key exactly matches the binding key.
Fanout : broadcasts to all bound queues; ignores routing keys.
Topic : pattern‑matches routing keys using "#" (zero or more words) and "*" (exactly one word).
TTL (Time‑To‑Live)
Set on the message when publishing (via message properties).
Set on the queue/exchange; messages expire after the configured interval.
Publisher Confirm Mechanism
Producers can enable confirm mode on a channel ( channel.confirmSelect()) and add a ConfirmListener to receive ACK/NACK callbacks, ensuring that the broker has persisted the message.
Return Listener
When the mandatory flag is true, the broker returns unroutable messages to the producer via a ReturnListener.
Consumer ACK/NACK
Consumers can manually acknowledge messages ( basicAck) or negatively acknowledge them ( basicNack) with options for multiple and requeue.
public void basicNack(long deliveryTag, boolean multiple, boolean requeue)Dead‑Letter Exchange (DLX)
A DLX is a normal exchange that receives messages that become dead letters (e.g., TTL expiration, reject, or max‑retry). The dead‑letter queue is defined by the x-dead-letter-exchange argument on the original queue.
RocketMQ
RocketMQ, Alibaba’s flagship message product, provides high‑availability and high‑reliability messaging.
Core Concepts
Broker : stores and forwards messages; registers topics with NameServer.
Topic : logical grouping of messages.
Tag : secondary classification within a topic.
MessageQueue : physical queue under a topic; enables horizontal scaling.
NameServer : lightweight metadata service (similar to ZooKeeper) without inter‑node communication.
Producer : supports synchronous, asynchronous, and one‑way sending.
Consumer : supports PUSH/PULL, cluster consumption, and broadcast consumption.
Group : a set of producers or consumers sharing the same logical role.
Offset : a 64‑bit index locating a message within a queue.
Delay Messages
RocketMQ supports predefined delay levels (e.g., 1 s, 5 s, 1 min, …). Level 0 means no delay.
Ordered Messages
RocketMQ guarantees strict ordering either per‑partition (queue) or globally by using hash(key) % number_of_queues.
Transactional Messages
Transactional messages follow a half‑message → local‑transaction → commit/rollback flow, with a compensation process for pending transactions.
High‑Availability
Master‑Slave replication (synchronous or asynchronous).
Producer load‑balancing: round‑robin across all queues.
Consumer load‑balancing: each consumer instance is assigned one or more queues; excess consumers receive no queues.
Dead‑Letter Queue (DLQ)
When a message exceeds the max retry count, RocketMQ routes it to a special queue named %DLQ%<ConsumerGroup>.
Kafka
Kafka is a distributed, partitioned, replicated log‑based messaging system originally coordinated by ZooKeeper (newer versions are ZooKeeper‑free).
Core Concepts
Broker : a Kafka server; a cluster consists of multiple brokers.
Topic : logical category of messages.
Partition : ordered sub‑log of a topic; each partition has a leader and replicas.
Producer and Consumer : clients that write to and read from brokers.
ConsumerGroup : a set of consumers where each partition is consumed by only one member.
Leader / Follower : leader handles reads/writes; followers replicate.
Offset : position of a record within a partition.
Controller
One broker is elected as the controller (via ZooKeeper) to manage partition leadership and metadata changes.
Rebalance Mechanism
When consumer group membership or topic partitions change, Kafka reassigns partitions using range, round‑robin, or sticky strategies.
Producer Publishing Flow
Producer discovers the leader for the target partition via ZooKeeper.
Message is sent to the leader.
Leader appends the record to its local log.
Followers pull the record, write to their logs, and ACK the leader.
Leader updates the high‑watermark (HW) after all in‑sync replicas ACK and replies to the producer.
hash(key) % partitionCountHW and LEO
HW (high‑watermark) is the smallest LEO (log‑end‑offset) among in‑sync replicas; consumers can only read up to HW, guaranteeing that a record is replicated before being consumed.
Log Segment Storage
Each partition stores data in segment files (default max 1 GB). Index, data, and time‑index files enable fast offset and timestamp lookups.
Common MQ Challenges and Solutions
Ensuring Ordered Consumption
RabbitMQ: one consumer per queue.
RocketMQ: hash(key) % number_of_queues.
Kafka: hash(key) % number_of_partitions.
Implementing Delayed Consumption
RabbitMQ: dead‑letter queue + TTL or the delayed‑message plugin.
RocketMQ: native delay‑message support.
Kafka: create a dedicated delay topic, persist messages, and have a background thread move them to the target topic after the delay.
Guaranteeing Reliable Delivery
RabbitMQ: producer confirms + manual consumer ACK; optionally persist messages in a database.
RocketMQ: enable message persistence (deliveryMode=2) and use confirm/ack mechanisms.
Kafka: set acks=all, configure replication.factor, min.insync.replicas, and unlimited retries.
Achieving Idempotence
Duplicate messages can arise during send, delivery, or rebalance. Solutions include using unique message IDs, storing processed IDs, and designing consumer logic to be idempotent.
Handling Message Backlog
Scale out consumers or add parallel queues to increase throughput.
Use batch re‑import during low‑traffic windows for expired messages.
If disk fills, temporarily drop consumption, increase storage, or write a fast‑consumer to purge backlog.
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.
Sohu Tech Products
A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.
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.
