RabbitMQ and AMQP Explained
On this page
What is RabbitMQ?
You need messaging between services. Kafka (Day 43) is a retained log optimized for high-volume event streams. RabbitMQ is a traditional message broker: it routes each message to queues, delivers it to a consumer, and deletes it once acknowledged. It speaks AMQP (Advanced Message Queuing Protocol), among others. Which to choose depends on whether you need flexible routing of individual tasks or a durable, replayable stream.
RabbitMQ exchanges, queues and bindings
In RabbitMQ a producer never sends straight to a queue. It sends to an exchange with a routing key. Bindings connect exchanges to queues with rules, and the exchange copies the message into every queue whose binding matches.
| Exchange type | Routes to | Example |
|---|---|---|
| Direct | Queues whose binding key equals the routing key | routing_key=resize goes to the image-resize queue |
| Topic | Queues whose pattern matches (* one word, # zero or more) | order.* matches order.created, order.shipped |
| Fanout | Every bound queue, ignoring the key | Broadcast a cache-clear to all services |
| Headers | Queues matching message header values | Route by region=eu and priority=high |
Worked example: order events with a topic exchange
The order service publishes to a topic exchange named orders with keys like order.created, order.cancelled and order.shipped.
queue shipping bound with order.created, order.cancelled
queue notifications bound with order.*
queue analytics bound with #Shipping sees creations and cancellations; notifications sees every order event; analytics sees everything on the exchange. Adding a fraud-check service means one new queue and one binding (order.created); the order service doesn't change.
Acknowledgments, prefetch and durability
A consumer acknowledges each message after processing it. If it crashes or its connection drops before acknowledging, RabbitMQ requeues the message for another consumer. A consumer can also reject a message, optionally without requeueing, which sends it to a configured dead-letter exchange.
Prefetch (basic.qos) limits how many unacknowledged messages a consumer holds at once. Without it, RabbitMQ may push thousands of messages to one consumer while others sit idle. A prefetch of 10 to 50 is a common starting point for work queues.
For durability, declare queues as durable and publish messages as persistent. For high availability, use quorum queues, which replicate each queue across nodes using the Raft consensus protocol. The older classic mirrored queues were removed in RabbitMQ 4.0 (2024), so clusters upgrading from 3.x must migrate.
Durable and persistent still isn't enough on its own. If the producer publishes and the broker crashes before writing the message, the producer never knows. Turn on publisher confirms: the broker acknowledges each message once it's safely stored, and the producer retries anything not confirmed. That's the producer-side twin of consumer acknowledgments.
Watch queue depth as well. RabbitMQ is designed for queues that drain. When consumers fall far behind, millions of waiting messages use memory and disk, and once a node crosses its memory threshold, RabbitMQ blocks publishers until it recovers. A growing queue is an alert, not a buffer to rely on.
RabbitMQ vs Kafka: which to choose
| Need | Better fit |
|---|---|
| Flexible routing per message (patterns, headers) | RabbitMQ |
| Task queues: resize this image, send this email | RabbitMQ |
| Per-message retries, priorities, dead-lettering | RabbitMQ |
| Very high-volume event streams | Kafka |
| Replaying history, new consumers reading the past | Kafka |
| Many independent consumer groups on the same events | Kafka |
Both can stretch into the other's territory (RabbitMQ has Streams; Kafka can act as a work queue), but these defaults hold. Many companies run both: RabbitMQ for background jobs, Kafka for event streams and data pipelines.
Check yourself
Use a topic exchange and bind each queue with a pattern: notifications with order.*, analytics with #, shipping only with order.created and order.cancelled.
Publisher confirms. Durable queues and persistent messages only help once the broker has actually stored a message. Without confirms, the producer sends and moves on, with no way to know whether the broker wrote the message before crashing, so lost messages are never retried. With confirms, the producer retries anything unconfirmed.
A prefetch limit. Without one, RabbitMQ pushes as many messages as it can to a consumer before acknowledgments come back, so one consumer can end up holding a huge backlog. Setting basic.qos with a small prefetch count limits unacknowledged messages per consumer and spreads work fairly.
Kafka. RabbitMQ is a broker that deletes a message once it's acknowledged, so a service added today can't see last month's orders. Kafka keeps messages for a configured retention period regardless of consumption, so a new consumer group can start from 30 days ago and read the whole history.