When the answer is RabbitMQ
A broker, not a log: exchanges, bindings and routing keys, acks and the dead-letter exchange, prefetch, and the honest comparison that says tasks go here and facts go to Kafka.
Every Kafka interview eventually asks "and when would you use RabbitMQ instead?" A surprising number of Kafka deployments are answering that question wrongly in production: a task queue with a log's operational weight, or a log rebuilt badly on a broker that was never one. The two are not competitors with a winner. They are different shapes, and this lesson is the shape of the other one.
A broker, not a log
Kafka is a log. Producers append, the broker keeps everything for the retention period, and consumers keep their own position and read at their own pace, as many times as they like.
RabbitMQ (and AMQP generally) is a broker. A producer hands a message to an exchange. The exchange routes it into zero or more queues by rules called bindings. A consumer takes it from a queue, and once the consumer acknowledges it, the message is gone. The broker's job is delivery, not storage. A queue with no consumers grows, a queue with a consumer empties, and "replay yesterday's messages" is not a question it answers.
producer ──▶ exchange ──(binding: routing key "order.placed")──▶ queue: inventory ──▶ consumer
──(binding: "order.*")───────────────────▶ queue: analytics ──▶ consumerExchanges, bindings and routing keys
The exchange type decides how a message's routing key is matched against bindings:
| Exchange | Routes by | Use |
|---|---|---|
| direct | exact key match | a work queue: email.send → the email queue |
| topic | pattern: order.*, #.failed | pub/sub with selection, the common one |
| fanout | ignores the key: every bound queue | broadcast: cache invalidation to every instance |
| headers | message headers, not the key | rare |
Routing lives in the broker's configuration, which is the sharpest difference from Kafka: there, a consumer subscribes to a topic and filters in code; here, a queue is bound to the messages it wants and receives nothing else. Adding a consumer for order.placed is declaring a queue and a binding — usually from the consumer's own startup code, idempotently — and the producer never changes.
Queues can be durable (survive a broker restart) and messages persistent (written to disk), and both are needed for a message to survive a crash; the default is neither. Quorum queues, replicated by Raft across nodes, are the current answer for anything that must not be lost; classic mirrored queues are deprecated.
Acknowledgement, prefetch and the dead-letter exchange
A consumer receives a message and later sends an ack; until it does, the message is unacked and invisible to others, and if the consumer's connection dies the broker requeues it. That is at-least-once delivery, the same guarantee Kafka's manual commit gives, with the same consequence: the consumer must be idempotent. Ack after the work, not before (auto-ack is at-most-once, which is fine for metrics and wrong for orders). Nack or reject with requeue=false sends it to the queue's dead-letter exchange, if one is configured — and it should be, because the alternative for a poison message is a requeue loop that pins a consumer forever. Set x-message-ttl and x-max-length on queues, too: an unbounded queue is the backpressure lesson's unbounded buffer, and a broker whose memory is full stops accepting publishes from everyone.
Prefetch (basic.qos) is how many unacked messages a consumer may hold. It is the throughput-versus-fairness knob: a prefetch of 1 hands each message to whichever consumer is free (fair, slow); a prefetch of 100 lets a fast consumer batch (fast, and one slow message holds 99 behind it). Ten to fifty is the usual range for short tasks; one for long ones.
@RabbitListener(queues = "inventory")
void on(OrderPlaced event, Channel ch, @Header(AmqpHeaders.DELIVERY_TAG) long tag) throws IOException {
reserve(event); // idempotent, keyed on event.id
ch.basicAck(tag, false);
}With AcknowledgeMode.AUTO (Spring's default, not the broker's) the container acks after the listener returns and nacks on an exception, and spring.rabbitmq.listener.simple.retry.* adds the retry-with-backoff and the DLQ that the Kafka course's retries lesson built by hand.
Ordering, throughput and the comparison
A single queue with a single consumer delivers in order. Add a second consumer and ordering is gone — two messages are being processed at once, and a redelivery after a failure lands behind messages that arrived later. Kafka's answer, ordering per key within a partition, has no equivalent here; consistent-hash exchanges (a plugin) approximate it by routing each key to one of several queues, each with one consumer, which is a partition rebuilt by hand.
The honest comparison:
| RabbitMQ | Kafka | |
|---|---|---|
| model | deliver and forget | store and let consumers read |
| replay | no | yes, for the retention period |
| routing | in the broker, per binding | in the consumer, per topic |
| ordering | one queue, one consumer | per key, per partition |
| throughput | tens of thousands of messages/s per node, fine for most | millions/s, sequential disk |
| consumer count | cheap to add, each gets its own queue | each group re-reads the log |
| the fit | tasks, RPC over messaging, routing to many, per-message TTL and priority | events as the system of record, high volume, replay, stream processing |
Use RabbitMQ when the messages are work to be done — send this email, resize this image, call this API — and the questions are routing, priority, retry and fairness; the message has no value once handled. Use Kafka when the messages are facts that happened — an order was placed — that several consumers will read, now and later, and the log is worth keeping. A service that emits OrderPlaced to Kafka and dispatches SendConfirmationEmail through RabbitMQ is not confused; it is using each for its shape.
Under the hood: two routing models, and where "the same message to three readers" diverges
The exchange/binding table above says what routes where. It is worth being precise about how, because the mechanism is where RabbitMQ and Kafka stop looking like two flavours of the same thing.
RabbitMQ implements a specific dialect of AMQP 0-9-1 (not the later, unrelated AMQP 1.0 standard, which RabbitMQ supports separately through a plugin). The routing algorithm is part of that protocol, not an implementation detail:
- A direct exchange does an exact string match of the message's routing key against each binding's key.
- A topic exchange splits both the routing key and the binding pattern on
.and matches word by word. In a binding,*matches exactly one word and#matches zero or more. So a binding oforder.*.failedmatchesorder.payment.failed, but notorder.failedororder.payment.retry.failed. - A fanout exchange ignores the routing key entirely and delivers to every bound queue.
- A headers exchange matches on message header key/value pairs instead of the routing key, with an
x-matchbinding argument ofallorany.
None of that lives in consumer code. It is evaluated once, in the broker, at publish time.
That "evaluated at publish time" detail is the one with the real architectural consequence.
When a fanout (or wildcard topic) exchange has three queues bound to it, the broker makes three independent copies of the message, one per queue, the moment it is published. Each queue is a separate, ordered, consumable data structure with its own memory/disk footprint, and a message is removed from a queue the instant its consumer acks it. Fan-out to N readers in RabbitMQ means N physical copies sitting in N queues. A reader that has been offline a day has a queue that grew for a day, and needs a TTL or x-max-length, or it eventually takes the broker down (as the earlier section warns).
Kafka's fan-out happens the opposite way, at read time. The log is written once, and every consumer group tracks its own offset into that single copy, reading forward at its own pace. The __consumer_offsets entries grow, but the log itself grows only with retention, not with how many groups are reading it or how far behind any one of them is.
A consumer group that comes back after a day-long outage in Kafka just has a day of lag against retention it already budgeted for. The same outage against a RabbitMQ fanout queue is a queue the broker has been accumulating unboundedly the whole time. The fix (a TTL, a max length) means old messages for that reader are now silently gone. RabbitMQ forces that decision at design time. Kafka defers it to a single retention setting shared by every reader.
The delivery direction differs too. A RabbitMQ consumer traditionally registers a callback (basic.consume) and the broker pushes messages to it. basic.qos/prefetch is the only flow-control knob on that push (the earlier section on this page covers the tuning). A Kafka consumer pulls by calling poll(), so the consumer, not the broker, decides when it is ready for more.
That difference is why a slow RabbitMQ consumer needs prefetch tuned down to avoid the broker overwhelming it, while a slow Kafka consumer simply polls less often. The broker never has to decide how much to push to someone who has not asked.
Try it yourself
Three independent readers of the same event stream, in each model
A payments service emits OrderPlaced. Three consumers need every event, independently, at their own pace:
- a fraud-review service (near real time);
- a nightly analytics job (reads once a day, in a batch);
- a new recommendation service, added six months from now, that should be able to start by reading the last 90 days of
OrderPlacedevents it never saw the first time around.
Sketch this in RabbitMQ: what exchange type, how many queues, and what happens to the third requirement (a consumer added six months late reading 90 days of history)? Then sketch it in Kafka: how many topics, how many consumer groups, and what has to be true about the topic's configuration for the third requirement to work at all?
Working through both designs
RabbitMQ: a fanout exchange (or a topic exchange with three broad bindings) with three queues, one per consumer, each getting its own copy of every OrderPlaced message as it is published. The fraud-review and analytics queues are fine: they exist before the messages arrive, so they accumulate their copy and each consumer drains it at its own pace.
The third requirement cannot be satisfied at all with this design. The recommendation service's queue did not exist when those messages were published, so it was never bound to receive them. RabbitMQ has no concept of "give a newly bound queue everything published in the last 90 days": a queue only ever holds what was routed to it after it existed (and after it started being consumed at whatever ack rate keeps it from hitting its TTL or max-length limit). Getting that history would mean the producer had also been writing every OrderPlaced somewhere durable and replayable outside RabbitMQ. That is a second system doing the job the message broker itself does not do.
Kafka: one topic, order-placed, with retention configured to at least 90 days. This is the detail the requirement actually depends on, and nothing else in the design has to change for it. There are three consumer groups, fraud-review, analytics-nightly and recommendations, each independently tracking its own offset into the same log.
fraud-review and analytics-nightly read at whatever pace they want, same as the RabbitMQ case. recommendations, created six months later, simply starts a new consumer group with auto.offset.reset=earliest (or seeks to a specific offset/timestamp) and reads everything still inside the 90-day retention window. There is no coordination with the producer and no second durable store, because the log itself was already the 90-day store the whole time.
The exercise is really about where each model puts the cost. RabbitMQ pays it per consumer, at publish time, in queues that must already exist. Kafka pays it once, as a retention setting on the topic, and a new reader is free to show up late.
Misconceptions
- "Kafka is just strictly faster and more capable, so there's rarely a real reason to pick RabbitMQ." Kafka's throughput numbers come from sequential disk writes and consumers that pull in batches. That is exactly the shape of high-volume, mostly-uniform event traffic. It is not the shape of "call 200 rate-limited third-party APIs with per-destination backoff and priority", where the requirement is per-message routing and lifecycle decisions the broker itself should make, not raw messages per second. A RabbitMQ topic exchange with per-destination queues, TTLs and prefetch settings solves that directly. Building the equivalent routing and per-message expiry logic in Kafka consumer code, on top of a system whose native unit is an ordered partition rather than an individually routable message, is solving a problem Kafka's model was not shaped for.
- "A fanout exchange to three queues and three Kafka consumer groups on one topic are basically the same feature with different names." They produce similar-looking results, three independent readers of every message, through opposite mechanics. RabbitMQ copies the message into each queue at publish time, so a queue that does not exist yet (a consumer added later) gets nothing from before it existed. Kafka writes the message once and lets each consumer group read the same copy at its own offset, so a consumer group created later can still read everything the topic's retention has kept. Treating them as interchangeable is exactly the mistake in the
Try it yourselfexercise above. It only shows up once someone needs a late-arriving reader or a day-long outage, which is precisely when it is expensive to discover.
Sources
- RabbitMQ's own documentation on AMQP concepts, exchanges, bindings and queues — the specific dialect and extensions RabbitMQ implements on top of AMQP 0-9-1.
- Apache Kafka's documentation ("Introduction" / "Design" sections), which explicitly contrasts Kafka's log-based model with traditional queue-based messaging systems.
- Confluent's publicly published comparison writing on Kafka versus traditional message brokers (their engineering blog has covered this repeatedly; treat any specific numbers there as illustrative, not a benchmark to quote verbatim).