Messaging Queues / A beginner's guide
RabbitMQ, step by step.
Learn how a message broker routes work from producers to queues and consumers.
1. What is RabbitMQ?
RabbitMQ is an open-source message broker. It accepts messages from producer applications, routes them, and delivers them to consumer applications.
It helps services communicate asynchronously. A producer can send work without waiting for a consumer to be ready, while RabbitMQ holds the message until a consumer can process it.
2. The main parts
RabbitMQ server or broker
A RabbitMQ server is the running RabbitMQ application. It manages exchanges, queues, bindings, connections, and message delivery. One server is also called a RabbitMQ node. Multiple nodes can work together as a cluster.
Producer
A producer is an application that publishes a message to RabbitMQ. For example, an Order Service can publish an order.created message.
Exchange
An exchange receives messages from producers and decides which queues should receive them.
Queue
A queue is a buffer that holds messages until consumers process them. Multiple consumers can read from one queue and share the work.
Consumer
A consumer reads messages from a queue and performs the work. A Payment Worker can consume messages from a payment queue.
Binding and routing key
A binding connects an exchange to a queue. A routing key is a value on a message that an exchange uses with its bindings to decide where to route the message.
3. What does an exchange do?
The producer normally sends to an exchange rather than directly to a queue:
Producer → Exchange → Queue → ConsumerThe exchange applies routing rules. One message may go to one queue, or copies may go to several queues.
Common exchange types
- Direct: routes when the routing key exactly matches.
- Topic: routes using patterns such as
order.*orpayment.#. - Fanout: broadcasts to every queue bound to the exchange.
- Default exchange: a built-in direct exchange that can route to a queue using the queue name.
4. What does a queue do?
A queue is where a message waits for a consumer. If all workers are busy, messages remain in the queue. When several consumers use the same queue, RabbitMQ distributes messages among them so they can share the work.
payment-queue
├── Payment Worker 1
└── Payment Worker 2Each message from this queue is normally delivered to one active worker. RabbitMQ can use prefetch settings to limit how many unacknowledged messages a worker receives at once.
5. Acknowledgements and retries
After a consumer receives a message, it processes the work and sends an acknowledgement, or ack. RabbitMQ can then remove the message from the queue.
If the consumer crashes or rejects the message before acknowledging it, RabbitMQ can requeue it for another attempt. Consumers should be idempotent because a message may be delivered more than once.
6. A practical example: order routing
An Order Service publishes messages to an order-exchange:
order.created
order.cancelled
payment.failedBindings route those messages to the right queues:
order-exchange
├── payment-queue ← order.created
├── inventory-queue ← order.created, order.cancelled
├── email-queue ← order.*
└── fraud-queue ← payment.*Payment, Inventory, Email, and Fraud workers can then process their own queues independently. This uses RabbitMQ’s routing capability instead of making the Order Service call every service directly.
RabbitMQ is a strong fit when messages represent work that should be routed and processed by workers. Kafka is a strong fit when many independent applications need to read and replay a durable event stream.
This introduction follows the official RabbitMQ documentation ↗.