RabbitMQ explained: exchanges, queues and routing

RabbitMQ is a flexible message broker built around exchanges and queues. Learn how routing works and how to avoid losing or duplicating work.

Messages routed through a switch into queues

RabbitMQ is a message broker. Producers do not send to a queue directly. They publish to an exchange, which routes the message to one or more queues based on rules. Consumers read from queues.

The pieces

  • Exchange: receives messages and decides where they go.
  • Queue: stores messages until a consumer takes them.
  • Binding: a rule linking an exchange to a queue.
  • Routing key: a label on the message used by the binding.

Exchange types

  • Direct: delivers to queues whose binding key matches the routing key exactly.
  • Fanout: copies the message to every bound queue (broadcast).
  • Topic: matches patterns, such as orders.*.created.
  • Headers: routes by message headers.

Reliability

  • Acknowledgements: a consumer sends an ack after finishing. If it dies first, the message goes back to the queue and another consumer gets it. This gives at-least-once delivery, so make handlers idempotent.
  • Durable queues and persistent messages survive a broker restart.
  • Publisher confirms tell the producer the broker has the message.
  • Prefetch: limits how many unacknowledged messages one consumer holds, so one slow worker does not hoard work.
  • Dead-letter exchange: failed or expired messages are moved aside for inspection.
  • Quorum queues replicate across nodes for high availability.

Typical uses

Background jobs (emails, image processing), request routing between services, and work distribution across workers.

RabbitMQ vs Kafka

RabbitMQ Kafka
Model broker with smart routing distributed log
After reading message is removed message is kept for the retention time
Replay not by default yes
Best for task queues, flexible routing high-volume streams, many readers

Takeaway: use RabbitMQ when you need flexible routing and job queues, always use acks and a dead-letter path, and keep consumers idempotent.


System Design series, Part 2 (building blocks): topic 3 of 14. Previous: Apache Kafka explained: topics, partitions and consumer groups.

Keep reading