Backend

How Message Queues Work: Kafka vs RabbitMQ

A visual guide to how message queues work, how Kafka and RabbitMQ differ, and a simple framework for choosing the right one for your system.

·
8 min read
·
AI-assisted

AI tooling disclosure

Generative AI materially contributed to this article.

Human review confirmed by the author.

How Message Queues Work: Kafka vs RabbitMQ

Kafka and RabbitMQ are the two names that come up whenever services need to talk to each other asynchronously. They look similar from a distance, but they start from different ideas: RabbitMQ is a message broker that routes messages into queues, while Kafka is a distributed log that stores an ordered stream of events.

Those differences have also been narrowing. RabbitMQ added a replicated log (streams), and Kafka added queue-style consumption (share groups). This post explains how each one works and where the real differences remain.

Versions: This post reflects Apache Kafka 4.3.x and RabbitMQ 4.4.x as of November 2026. Both have changed a lot recently, so older comparisons may be out of date.


1. What a Message Queue Does

A message queue sits between a producer (which sends messages) and a consumer (which processes them). The producer hands the message to the broker and moves on; the consumer picks it up when it's ready.

flowchart LR
    subgraph W["Without a queue"]
        A1["Order service"] -->|"direct call"| B1["Email service"]
        B1 -.->|"down or slow:<br/>order fails too"| A1
    end
    subgraph Q["With a queue"]
        A2["Order service"] --> M["Message queue<br/>holds messages safely"]
        M --> B2["Email service<br/>processes when ready"]
    end

Queues give you:

  • Decoupling: services don't need to be online at the same time.

  • Buffering: traffic spikes pile up in the queue instead of crashing consumers.

  • Retries: failed work can be delivered again.

  • Scaling: add more consumers to process messages faster.

Delivery guarantees

Guarantee

Meaning

Risk

At-most-once

Delivered zero or one time

Messages can be lost

At-least-once

Retried until acknowledged

Messages can be duplicated

Exactly-once

The effect happens once

Hard to achieve end to end

Both systems are typically run at-least-once, so consumers should be idempotent: processing the same message twice must be safe.


2. How RabbitMQ Works: The Smart Broker

In RabbitMQ, publishers never write to a queue directly. They publish to an exchange, which routes each message to one or more queues using rules called bindings. The broker then delivers messages from queues to subscribed consumers, and removes each one once the consumer acknowledges it.

flowchart LR
    P["Publisher<br/>routing key: order.created.eu"] --> X["Topic exchange"]
    X -->|"binding: order.created.*"| Q1["Queue A<br/>match"]
    X -->|"binding: order.*.*"| Q2["Queue B<br/>match"]
    X -->|"binding: payment.*"| Q3["Queue C<br/>no match"]
    Q1 --> C1["Consumer"]
    Q2 --> C2["Consumer"]

Exchange types

Type

Routes by

Typical use

Direct

Exact routing key match

Task routing, log levels

Fanout

Ignores the key; copies to every bound queue

Broadcasts, notifications

Topic

Pattern match (* is one word, # is zero or more words)

Flexible event routing

Headers

Message header values

Attribute-based routing

A built-in default exchange routes by queue name, which is why simple examples seem to publish straight to a queue.

The life of a message

flowchart TD
    A["Publisher sends message<br/>to an exchange"] --> B["Exchange routes using bindings"]
    B --> C{"Any matching queue?"}
    C -- No --> D["Dropped, or returned to the<br/>publisher if flagged mandatory"]
    C -- Yes --> E["Stored in queue<br/>publisher confirm sent"]
    E --> F["Delivered to a consumer<br/>up to the prefetch limit"]
    F --> G{"Consumer result"}
    G -- "ack" --> H["Message removed"]
    G -- "nack with requeue,<br/>or consumer crash" --> I["Requeued"]
    I --> J{"Delivery limit<br/>reached?"}
    J -- No --> F
    J -- Yes --> K["Dead-lettered or dropped"]

Key mechanics:

  • Consumer acknowledgements: if a consumer's connection drops before it acks, RabbitMQ requeues the message for another consumer.

  • Publisher confirms: the broker tells the publisher when it has taken responsibility for a message.

  • Prefetch: limits how many unacknowledged messages a consumer holds, which spreads work fairly.

  • Dead-lettering: rejected or repeatedly failing messages can be sent to a dead-letter exchange. Quorum queues have a default redelivery limit of 20.

Queue types in RabbitMQ 4.x

Type

Storage and replication

Consumption

Classic

Local; not replicated (mirroring was removed in 4.0)

Destructive

Quorum

Replicated with Raft; the choice when data safety matters

Destructive

Stream

Replicated append-only log

Non-destructive, with replay

Recent releases keep improving quorum queues. RabbitMQ 4.3 added 32 strict priority levels and native delayed retries.

A minimal example (Python, pika)

import json
import pika

conn = pika.BlockingConnection(pika.ConnectionParameters("localhost"))
ch = conn.channel()

# Topology: a topic exchange and a replicated (quorum) queue bound to it
ch.exchange_declare(exchange="orders", exchange_type="topic", durable=True)
ch.queue_declare(queue="email-service", durable=True,
                 arguments={"x-queue-type": "quorum"})
ch.queue_bind(queue="email-service", exchange="orders",
              routing_key="order.created.*")

# Publish with confirms enabled
ch.confirm_delivery()
ch.basic_publish(
    exchange="orders",
    routing_key="order.created.eu",
    body=json.dumps({"order_id": 42}),
    properties=pika.BasicProperties(delivery_mode=2),   # 2 = persistent
    mandatory=True,
)

# Consume with manual acks and a prefetch limit
def handle(channel, method, properties, body):
    send_email(json.loads(body))                         # your work here
    channel.basic_ack(delivery_tag=method.delivery_tag)

ch.basic_qos(prefetch_count=10)
ch.basic_consume(queue="email-service", on_message_callback=handle)
ch.start_consuming()

3. How Kafka Works: The Distributed Log

Kafka stores messages (called records) in topics. Each topic is split into partitions, and each partition is an append-only log. Records are not deleted when read. They stay until the retention period expires (7 days by default), and every record has an offset, its position in the partition.

flowchart LR
    PR["Producer<br/>key: customer-42"] -->|"key decides partition"| T
    subgraph T["Topic: orders"]
        P0["Partition 0"]
        P1["Partition 1"]
        P2["Partition 2"]
    end
    subgraph GA["Consumer group A: email service"]
        A1["Consumer 1"]
        A2["Consumer 2"]
    end
    subgraph GB["Consumer group B: analytics"]
        B1["Consumer 3"]
    end
    P0 --> A1
    P1 --> A1
    P2 --> A2
    P0 --> B1
    P1 --> B1
    P2 --> B1

The ideas that matter

  • Partitions give parallelism. Records with the same key go to the same partition, so ordering is guaranteed within a partition, not across a whole topic.

  • Consumer groups share the work. Within one group, each partition is read by at most one consumer, so parallelism is capped by the partition count. Each group tracks its own position, so many groups can read the same topic independently.

  • Consumers pull. They poll the broker for records and remember their offset.

  • Replication: each partition has a leader and follower replicas. Since Kafka 4.0, the cluster runs on KRaft with no ZooKeeper.

Every group keeps its own bookmark in the same log:

flowchart LR
    subgraph P["Partition 0: append-only log"]
        O0["offset 0"] --> O1["offset 1"] --> O2["offset 2"] --> O3["offset 3"] --> O4["offset 4<br/>newest"]
    end
    GA["Group A:<br/>next read is offset 3"] -.-> O3
    GB["Group B:<br/>next read is offset 1"] -.-> O1
    W["Producer appends here"] -.-> O4

Because the log isn't consumed destructively, a new service can start from offset 0 and replay history, or a bug fix can reprocess old events.

Offsets and at-least-once

Consumers normally commit their offset after processing. If one crashes before committing, it restarts from the last committed offset, so some records are processed twice:

flowchart LR
    A["poll: fetch records from<br/>last committed offset"] --> B["Process records"]
    B --> C["Commit offset"]
    C --> A
    B -.->|"crash before commit"| D["Restart:<br/>re-read from last commit"]
    D --> A

Durability settings

For data you can't lose, Kafka needs deliberate configuration:

  • replication.factor=3 and min.insync.replicas=2 on the topic. Both default to 1.

  • acks=all on the producer, so a write is confirmed only after the in-sync replicas have it.

  • enable.idempotence=true, so producer retries can't create duplicates. It's on by default in the Java client since Kafka 3.0.

  • Transactions plus isolation.level=read_committed on consumers for exactly-once processing inside Kafka. Side effects outside Kafka, like sending an email, still need idempotent handling.

Kafka as a queue: share groups

Kafka's classic consumer groups tie parallelism to partitions and don't acknowledge individual records. Kafka 4.2 made share groups ("Queues for Kafka") production-ready. In a share group:

  • Many consumers can share a topic, and there can be more consumers than partitions.

  • Each record is acknowledged individually: accepted, released for redelivery, or rejected.

  • Delivery attempts are counted, so unprocessable records stop being retried.

  • Per-partition ordering is not guaranteed, so use share groups for independent tasks, not ordered streams.

At the time of writing, built-in dead-letter support for share groups is still pending.

A minimal example (Python, confluent-kafka)

import json
from confluent_kafka import Producer, Consumer

# Producer: durable settings
producer = Producer({
    "bootstrap.servers": "localhost:9092",
    "acks": "all",
    "enable.idempotence": True,
})
producer.produce("orders", key="customer-42",
                 value=json.dumps({"order_id": 42}))
producer.flush()

# Consumer: commit the offset only after processing succeeds
consumer = Consumer({
    "bootstrap.servers": "localhost:9092",
    "group.id": "email-service",
    "auto.offset.reset": "earliest",
    "enable.auto.commit": False,
})
consumer.subscribe(["orders"])

while True:
    msg = consumer.poll(1.0)
    if msg is None:
        continue
    if msg.error():
        print(msg.error())
        continue
    send_email(json.loads(msg.value()))                  # your work here
    consumer.commit(message=msg, asynchronous=False)

4. Kafka vs RabbitMQ Side by Side

Here's one "order created" event handled by each system:

flowchart LR
    subgraph R["RabbitMQ"]
        RP["Producer"] --> RE["Topic exchange"]
        RE -->|"order.created"| RQ1["Queue: email"]
        RE -->|"order.created"| RQ2["Queue: inventory"]
        RQ1 --> RC1["Email consumer<br/>ack removes message"]
        RQ2 --> RC2["Inventory consumer<br/>ack removes message"]
    end
    subgraph K["Kafka"]
        KP["Producer"] --> KT["Topic: orders<br/>log kept until retention expires"]
        KT --> KG1["Group: email<br/>own offset"]
        KT --> KG2["Group: inventory<br/>own offset"]
        KT --> KG3["Group: analytics<br/>can replay from offset 0"]
    end

RabbitMQ

Kafka

Core model

Broker with exchanges and queues

Distributed commit log

Routing

Rich: direct, fanout, topic, headers

Topic and partition key; consumers choose topics

Delivery to consumers

Broker pushes to subscribers (prefetch limit)

Consumers pull with poll

After consumption

Message removed on ack (queues)

Record stays until retention expires

Replay

Not with queues; yes with streams

Yes, by resetting offsets

Ordering

Per queue; competing consumers and requeues can reorder processing

Per partition

Scaling consumers

Add competing consumers to a queue

Up to the partition count per group (more with share groups)

Per-message handling

Acks, nacks, dead-lettering, TTL, priorities, delayed retries

Offset commits; per-record acks only in share groups

Replication

Quorum queues and streams (Raft)

Partition replicas with leader and followers

Protocols

AMQP 0-9-1, AMQP 1.0, MQTT, STOMP

Kafka's own binary protocol

In short: RabbitMQ is a smart broker with simple consumers (the broker tracks each message), while Kafka is a simple log with smart consumers (each consumer tracks its position).

Throughput is the other classic difference. Kafka's batched, append-only design is built for very high volume, and RabbitMQ's own documentation says its queue types can't compete with log-based systems on raw throughput, which is why it added streams. In practice, always benchmark your own workload.


5. Which Should You Choose?

flowchart TD
    S["What do you need?"] --> Q1{"Replay old events, or many independent<br/>services reading the same stream?"}
    Q1 -- Yes --> Q2{"Already run RabbitMQ,<br/>with moderate volume?"}
    Q2 -- Yes --> R1["RabbitMQ streams can work"]
    Q2 -- No --> K1["Kafka"]
    Q1 -- No --> Q3{"Flexible routing, priorities, delayed<br/>retries, or per-message handling?"}
    Q3 -- Yes --> R2["RabbitMQ"]
    Q3 -- No --> Q4{"Very high volume, stream processing,<br/>or data pipelines?"}
    Q4 -- Yes --> K2["Kafka"]
    Q4 -- No --> Q5{"Simple background job queue?"}
    Q5 -- Yes --> R3["RabbitMQ, or Kafka share groups<br/>if you already run Kafka"]
    Q5 -- No --> R4["Either works: pick what your team<br/>already knows how to operate"]

Choose RabbitMQ when you need task queues, complex routing, request/reply, priorities, or delayed retries, and you want each message handled and then gone.

Choose Kafka when you need an event backbone: high throughput, replayable history, stream processing, or many consumers reading the same events.

Use both when it fits. Many systems run Kafka for event streams and RabbitMQ for task queues.


6. Reliability Checklist

RabbitMQ

  • Use quorum queues (or streams) for data you can't lose; classic queues in 4.x aren't replicated.

  • Publish persistent messages and enable publisher confirms.

  • Use manual acks with a prefetch limit.

  • Set up dead-lettering or a delivery limit for poison messages.

  • Make consumers idempotent.

Kafka

  • Set replication factor 3, min.insync.replicas=2, and acks=all.

  • Keep the idempotent producer enabled.

  • Commit offsets only after processing, and make consumers idempotent.

  • Choose partition keys carefully, and plan partition counts early: changing them later can break per-key ordering.


Final Thoughts

RabbitMQ and Kafka both move messages between services, but they model messages differently. In RabbitMQ a message is a unit of work to be routed, delivered, and removed. In Kafka a message is an event in a shared log that anyone can read, and re-read.

With RabbitMQ streams and Kafka share groups, either can now cover much of the other's territory. Choose based on the center of gravity of your workload (task distribution or event streaming), and on which system your team can operate confidently.

Comments(0)

Join the discussion by logging into your account.

No comments yet. Be the first to comment!

Alysaa Lissy

Passionate developer sharing knowledge about modern web technologies and best practices.

Subscribe to Alysaa Lissy's Newsletter

Direct email dispatches when new stories are published. Zero algorithms.