What you'll learn: By the end of this lesson you will be able to explain how RabbitMQ routes a message through an exchange into queues and removes it on acknowledgement, how Kafka appends it to a partition that every consumer group reads at its own position, compare the two on fan-out, ordering, replay, retention and per-message features, say what RabbitMQ Streams change about the comparison, and choose between them for a ShopStream workload. A lab runs the Kafka side on an in-process broker.
A sorting office and a library both handle written material, but they keep different books. The sorting office takes each letter, reads the address, drops it into the right pigeonhole, and once the addressee signs for it the letter is gone. The library shelves every volume in the order it arrived and lets any number of readers each keep their own bookmark; reading a volume removes nothing, and volumes leave the shelf only when the librarian clears old ones.
RabbitMQ is the sorting office and Kafka is the library. The overview lessons message queues and pub/sub and event streaming and logs teach each model on its own, and the Confluent Kafka blog guide covers Kafka's broker and replication mechanics. This lesson puts them side by side, and the next lesson, Kafka internals, goes underneath the library.
Verified on 2026-12-01 against the current RabbitMQ documentation pages and Apache Kafka 4.2 documentation listed under Official documentation. The lab ran against Kafka clients from Spring Boot 4.1.2, compiled for Java 25. RabbitMQ was not run for this lesson: every statement about RabbitMQ comes from its documentation, not from output.
The core idea
RabbitMQ is organised around routing and per-message delivery state; Kafka is organised around a stored log and per-consumer position. In RabbitMQ the broker decides which queue gets each message and tracks, message by message, what has been delivered and acknowledged. In Kafka the broker appends records to a partition and mostly leaves position-keeping to the consumer group.
That one difference produces most of the others, and it is not absolute. RabbitMQ also offers streams, a log-style structure, and its quorum queues are replicated. So "RabbitMQ versus Kafka" is really "queue semantics versus log semantics", and a good answer starts from the workload.
Three illustrations run through the lesson, each labelled:
- Minimal example: the lab's two Kafka consumer groups reading the same nine records.
- Realistic example: ShopStream's order events, which several teams read, next to its email jobs, which one worker handles.
- Failure example: a consumer that dies mid-message, and what each system does next.
How RabbitMQ moves a message
According to the RabbitMQ AMQP concepts page, messages are published to exchanges, and exchanges then distribute message copies to queues using rules called bindings. Four exchange types exist: a direct exchange routes on the message's routing key, a fanout exchange routes to every bound queue and ignores the key, a topic exchange routes by a pattern match on the key, and a headers exchange ignores the key and matches on headers. A message that cannot be routed to any queue is either dropped or returned to the publisher, depending on how it was published.
Figure 1. RabbitMQ: exchange, bindings, queues. How to read this: the producer never names a queue; the exchange copies each message into every queue whose binding matches. Each message in a queue goes to one of that queue's consumers, so adding a second consumer to emails shares the work, while adding a second queue to the exchange gives another team its own copy.
Consumers receive messages from a queue and acknowledge them. The same page says a broker only completely removes a message from a queue when it receives an acknowledgement for it, and that a message from a consumer which dies without acknowledging is redelivered to another consumer. In RabbitMQ's automatic acknowledgement mode a message counts as delivered as soon as it is sent, which the confirms page calls unsafe; manual acknowledgement lets the application choose when to acknowledge.
When several consumers are attached to one queue, the consumers page says active consumers normally receive messages in round-robin fashion. A prefetch setting limits how many unacknowledged deliveries a channel may hold, so a slow worker is not flooded.
How Kafka stores a message
A Kafka topic is split into partitions, and each partition is an append-only log. The Kafka introduction says events are not deleted after consumption, that retention is a per-topic setting, that events with the same key are written to the same partition, and that a consumer of a partition always reads its events in the order they were written. The design page says a consumer's position in each partition is just a single integer, and that a consumer can deliberately rewind to an old offset and re-consume data.
Figure 2. Kafka: partitions and consumer groups. How to read this: records are appended to the partition the key hashes to, and they stay there. Every group polls all the partitions and remembers its own position in each; the members of the inventory group split the partitions between them, and that does not affect what the analytics group sees.
Side by side
| Question | RabbitMQ (queues) | Kafka |
|---|---|---|
| What the broker keeps | Messages waiting or delivered but unacknowledged, in queues | A retained log per partition |
| After a consumer is done | Acknowledged messages are removed from the queue | Records stay until retention removes them |
| Who tracks progress | The broker, per message | The consumer group, one offset per partition |
| Several independent readers | One queue per reader, filled by exchange bindings | One consumer group per reader, no extra copies |
| Sharing work among workers | Several consumers on one queue | Several members of one group, up to the partition count |
| Ordering | Queue order, unless redelivery, priorities or several consumers change it | Order inside one partition only |
| Replay | Not for ordinary queues; streams add it | Rewind the offset while records are retained |
| Routing | Exchanges and bindings, chosen by the broker | The producer chooses the topic and, by key, the partition |
Acknowledgement against offset commit
The two systems answer "how does the broker know I am done" differently. RabbitMQ tracks one message at a time, so a failure puts that message back.
Figure 3. A consumer dies mid-message in RabbitMQ. How to read this: the delivery state lives in the broker. The confirms page says unacknowledged deliveries are automatically requeued, carry a redelivered flag, and that consumers must be prepared to handle redeliveries.
Kafka records progress as an offset per partition. A consumer that crashes before committing is replaced by one that starts from the last committed offset, so everything after that offset is read again; the event streaming lesson draws that sequence. Both systems therefore redeliver after a failure, and both need an idempotent consumer, a topic the later lesson on delivery guarantees takes up.
Fan-out, work sharing and ordering
In RabbitMQ a message is copied into each bound queue, so a new independent reader needs a new queue and a binding, and only messages published after the binding exist for it. In Kafka a new reader is a new consumer group, which can start from the earliest retained record. In both systems members of a group (Kafka) or consumers of a queue (RabbitMQ) share the work.
Ordering differs in what it protects. The RabbitMQ queues page says delivery from a queue proceeds in enqueue order, but that priorities and redelivery can change it. Kafka orders only within a partition, so related records need a shared key. Neither gives total order across all consumers.
Retention and replay
A classic or quorum queue exists to hold messages until they are handled, and acknowledgement removes them. A Kafka partition is retained by policy, which is why replay is ordinary. RabbitMQ Streams close part of that gap: the streams page describes an append-only log that can be read repeatedly until it expires, with non-destructive consumers, offset specifications (first, last, next, a timestamp or a number), and retention by size and age. Super streams split a stream into partitions and, together with single active consumer, preserve order within a partition.
Build it: the Kafka side in a lab
RabbitMQ is not run here. The lab uses the repository's examples/spring-labs project, whose test starter starts an in-process Kafka broker. It has a single node, so it shows consumer groups, per-partition order and replay but no replication.
Goal. See two groups read the same records, members of one group split them, and a group replay by rewinding its offsets.
Setup. From the repository root:
cd examples/spring-labs
./gradlew :labs:kafka-partitions-and-groups:test
The command runs seven tests and rewrites nothing. The code is in examples/spring-labs/labs/kafka-partitions-and-groups/src/main/java/buildingai/labs/kafka/KafkaLab.java. Replay is one admin call, preceded by looking up the first available offsets:
Map<TopicPartition, OffsetAndMetadata> target = new java.util.HashMap<>();
startOffsets(bootstrap, topic).forEach((p, o) -> target.put(new TopicPartition(topic, p), new OffsetAndMetadata(o)));
try (Admin admin = Admin.create(Map.of("bootstrap.servers", bootstrap))) {
admin.alterConsumerGroupOffsets(group, target).all().get();
What the output shows. Nine records are sent to a three-partition topic orders, one per key, and each send is awaited.
Expected output, captured from a real run (Kafka clients from Spring Boot 4.1.2, one in-process broker):
# 3. two consumer groups read the same topic, each commits its own offsets
group inventory read 9 records, committed offsets {0=4, 1=3, 2=2}
group analytics read 9 records, committed offsets {0=4, 1=3, 2=2}
log start offsets {0=0, 1=0, 2=0}, log end offsets {0=4, 1=3, 2=2}
# 4. three more records arrive and nobody has read them yet
log end offsets {0=5, 1=4, 2=3}
lag of group inventory per partition {0=1, 1=1, 2=1}
# 5. replay: rewind a group, or seek without a group
group inventory resumed from its committed offsets and read 3 records (the new ones)
rewound group inventory to {0=0, 1=0, 2=0}
group inventory read 12 records again, committed offsets {0=5, 1=4, 2=3}
assign and seek to partition 0, offset 2 (no group): 2:1001 3:1002 4:1001
Two groups, inventory and analytics, each read all nine records and each ended with its own committed offsets: reading by one group did not remove anything for the other. After three more records arrived, the lag of inventory was the difference between the log end offsets and its committed offsets. Rewinding the group's offsets to the beginning made it read all twelve records again, and a reader with no group could assign a partition and seek to offset 2.
Expected output, captured from a real run (two members of one group on a fresh topic):
# 7. two members of one group on a fresh topic: each record goes to the owner of its partition
assignment {c1=[0, 1], c2=[2]}
c1 read p0@0:1001 p0@1:1002 p1@0:1003 p1@1:1004
c2 read p2@0:1005 p2@1:1006
Each record went to the member that owned its partition, and each member saw its partition's records in offset order. That is Kafka's version of competing consumers, and its parallelism is bounded by the partition count.
Break it. In KafkaLab.consumerProps, change auto.offset.reset to latest; the golden test fails because the groups no longer start from the beginning. Restore it and rerun.
Stretch. Add a third group and compare its committed offsets with inventory.
Choosing between them
| Choose | When | Gain | Cost | Wrong when |
|---|---|---|---|---|
| RabbitMQ queues | Each message is a job for one worker, with routing rules, priorities or per-message retry | Routing in the broker, simple work queues | No replay of consumed messages; fan-out needs a queue per reader | Many teams must reread history |
| Kafka | Several consumers need the same events, per-key order, or replay | Retained log, independent groups, replay | Parallelism capped by partitions; per-message delay and priority are yours to build | You need broker-side routing by content or per-message priority |
| RabbitMQ Streams | A log with replay inside a RabbitMQ estate | One broker product for both styles | Its own protocol, client libraries and limits | You want Kafka's wider connector ecosystem |
| Neither | A few thousand messages a day and one consumer | A table or a managed simple queue is enough | None | Volume or readers will grow soon |
then forgotten?"} Q1 -->|yes| Q2{"Do you need routing rules,
priorities or per-message retry?"} Q2 -->|yes| R1["RabbitMQ queue"] Q2 -->|no| R2["Any queue; pick by what you run"] Q1 -->|no| Q3{"Do several systems need the same events,
per-key order or replay?"} Q3 -->|yes| R3["Kafka, or a RabbitMQ stream"] Q3 -->|no| R4["A table or simple queue may do"]
Figure 4. A first-pass choice. How to read this: start at the top. Most disagreements are really about the first question, whether a message is a job or a fact other systems will want later.
In practice: ShopStream order events and email jobs
This is an educational scenario with illustrative numbers.
The situation
This is the realistic example. ShopStream (shared figure, illustrative: 500,000 orders per day, about 58 checkouts per second at peak) has an order service that must tell inventory, payments, shipping and analytics, and a separate stream of confirmation emails. Analytics wants to rebuild a report from last month's orders. The email sender is a pool of workers, and a failed email must be retried later without blocking the others.
This is a realistic scenario, not a case history.
Starting architecture
The order service calls each consumer over HTTP, so a slow email sender slows checkout and a new consumer means a code change.
Options considered
| Option | Gain | Cost | Wrong when |
|---|---|---|---|
| A. RabbitMQ for everything | One product; email retry and routing are natural | Analytics cannot replay consumed orders; each new reader needs a queue | A report must be rebuilt from history |
| B. Kafka for everything | Order events retained; new groups read from the start | Email retry and delay must be built on topics | Per-message delay matters most |
| C. Kafka for order events, a queue for email jobs | Each workload gets its natural model | Two systems to run | The team can run only one |
The decision
Option C, with the order events keyed by order id so one order's events stay in order, and each team reading them as its own consumer group. Email jobs are a work queue. If the team could run only one system, Option B with a retry topic for emails, or a RabbitMQ stream for orders, are both defensible; the deciding fact is whether anyone will need to reread history.
Rollout and verification
Publish order events to Kafka next to the existing calls, run each consumer in shadow, and compare counts per day. Check each group's lag and that the email queue drains.
The 2 a.m. failure
The page reads: "Email queue depth rising, consumers connected". A worker is holding messages it never acknowledges, so the queue shows them as delivered but unacknowledged and no other worker receives them. First checks: the unacknowledged count and the prefetch value on the workers. Mitigation: restart the stuck worker so its unacknowledged messages are requeued, and cap prefetch. Follow-up: set a processing timeout and make the handler idempotent.
Architect's notes
- Decide by consumers, not by throughput claims. The number of independent readers and the need to replay choose the model before any benchmark does.
- No throughput figures here. None were measured; measure your own workload.
- A queue and a log can coexist. Use each where its model matches.
- Plan for redelivery in both. Idempotent consumers are not optional.
- Name the key. In Kafka the key is the ordering contract; in RabbitMQ the routing key is a routing rule, not an ordering guarantee.
Common mistakes
- Treating Kafka as a queue with delete-on-read. Nothing is removed by reading; retention does the removing.
- Treating RabbitMQ as a log. An acknowledged message in an ordinary queue is gone.
- Adding consumers beyond the partition count and expecting more throughput from a Kafka group.
- Using automatic acknowledgement for work that must not be lost.
Key takeaways
- RabbitMQ routes messages through exchanges into queues and removes them on acknowledgement; Kafka appends records to partitions that consumer groups read at their own offsets.
- Fan-out is a queue per reader in RabbitMQ and a group per reader in Kafka.
- Both redeliver after failures, so consumers must be idempotent.
- Ordering is queue order in RabbitMQ (with exceptions) and per-partition order in Kafka.
- Replay is ordinary in Kafka and available in RabbitMQ through streams.
- Choose by whether messages are jobs or shared facts, and run both if the workloads differ.
Practice exercises
Exercise 1 (20 min). For ShopStream, list five message types and mark each as a job or a shared fact. Choose a system for each and justify it in one sentence.
Exercise 2 (40 min). Extend the lab: add a third group and show its committed offsets after it reads only the records of partition 1.
Exercise 3 (30 min). Write down what must be true of an email handler for redelivery to be harmless.
Related reading
- Message queues and pub/sub: queue semantics, retries and dead-letter queues.
- Event streaming and logs: partitions, groups and retention.
- Kafka internals: the log, offsets, segments and group assignment under the surface.
- Synchronous vs asynchronous communication: why ShopStream moves off direct HTTP calls.
- How Confluent Kafka works: broker internals, replication and delivery guarantees.
Later lessons in this module, named here without links: backend-delivery-guarantees and backend-dead-letter-queues-and-retries.
Official documentation
- RabbitMQ: AMQP 0-9-1 concepts, queues, consumer acknowledgements and publisher confirms and consumers.
- RabbitMQ: quorum queues and streams.
- Apache Kafka: introduction and design.