RabbitMQ exchange vs queue: what is the difference?

•6 min read•RabbitMQ tutorial

RabbitMQ exchange vs queue: what is the difference?

New RabbitMQ users often ask whether they should "send to an exchange or a queue", or whether a topic is a kind of queue. The confusion is normal: in most other messaging systems, you publish directly to the thing you read from. RabbitMQ splits that in two. This article explains the split, why it exists, and how to use it.

Quick answer

  • An exchange receives messages from publishers and decides where they go. It stores nothing.
  • A queue stores messages until a consumer acknowledges them.
  • A binding is a rule that connects an exchange to a queue (or to another exchange).

Publishers always publish to an exchange. Consumers always read from a queue. Bindings decide which queues get which messages.

publisher ──► exchange ──(binding)──► queue ──► consumer
                   └────(binding)──► queue ──► consumer

Exchanges: the router

An exchange is like a post office sorting desk. It looks at the message (its routing key, sometimes its headers), checks its list of bindings, and puts a copy in every queue that matches. Then it forgets about the message.

Key properties:

  • An exchange has a type that defines how it matches: direct, topic, fanout or headers. See RabbitMQ exchange types explained.
  • An exchange holds no messages. If no binding matches, the message is dropped (or returned to the publisher with the mandatory flag, or sent to an alternate exchange).
  • An exchange has no consumers. You can't "read from" an exchange.
  • One message can go to many queues. The exchange copies it to each matching queue.

Queues: the buffer

A queue is an ordered buffer of messages, waiting to be delivered to consumers.

  • A queue stores messages in memory and on disk, depending on its type and the message's delivery mode.
  • Consumers subscribe to a queue. If several consumers read the same queue, they share the work: each message goes to one consumer only (competing consumers).
  • A message stays until it's acknowledged, then it's removed. See message acknowledgment explained.
  • Queues have a type: classic, quorum or stream. See RabbitMQ queue types.

Bindings: the rules between them

A binding says "exchange X sends messages matching pattern P to queue Q". For a direct exchange, the pattern is an exact routing key. For a topic exchange, it can contain wildcards. For a fanout exchange, it's ignored.

Bindings are what make RabbitMQ flexible. The publisher doesn't know which queues exist. You can add a new consumer with its own queue and a new binding without touching the publisher.

Exchange vs queue at a glance

ExchangeQueue
RoleRoutes messagesStores messages
Who talks to itPublishersConsumers
Holds messagesNoYes
Typesdirect, topic, fanout, headersclassic, quorum, stream
One message goes toEvery matching queueOne consumer (or every stream consumer)
Can be durableYes (the definition survives restarts)Yes (definition and persistent messages survive restarts)
Can be deleted while in useYes, messages to it are then lostYes, its messages are lost

"Topic vs queue" in RabbitMQ

If you come from Kafka, JMS or a cloud service, you may look for "topics" in RabbitMQ and find a topic exchange. It's not the same thing.

  • In JMS and most brokers, a topic is pub/sub (every subscriber gets every message) and a queue is point-to-point (each message goes to one consumer).
  • In Kafka, a topic is a stored, partitioned log that consumers read at their own offset.
  • In RabbitMQ, a topic is a routing rule. The messages always end up in queues.

To get pub/sub in RabbitMQ, give each subscriber its own queue and bind all those queues to the same exchange. Each queue receives a copy:

                       ┌──► queue "billing"   ──► billing service
publisher ──► exchange ├──► queue "emails"    ──► email service
  "events"   (topic)   └──► queue "analytics" ──► analytics service

To get point-to-point (work queue), have several consumers read the same queue. RabbitMQ distributes messages between them.

To get Kafka-like behaviour (many readers of the same stored data, replay), use a stream. We compare the models in more detail in RabbitMQ vs Kafka.

Can I publish directly to a queue?

Sort of. Every RabbitMQ virtual host has a default exchange, a direct exchange with an empty name (""). Every queue is automatically bound to it with a routing key equal to the queue name. So this:

channel.sendToQueue('emails', Buffer.from('hello'));

is really this:

channel.publish('', 'emails', Buffer.from('hello'));

The default exchange is perfect for simple cases (one producer, one queue, RPC replies). It becomes limiting when a second service wants the same messages: the publisher has to change to send twice. Publishing to a named exchange from day one costs one line and keeps that door open.

You can't bind or unbind anything to the default exchange, and you can't delete it.

A complete example

One orders exchange, two services that need order events, and a work queue shared by two instances of the billing service:

// setup (run once, or at startup of each service)
await ch.assertExchange('orders', 'topic', { durable: true });
 
await ch.assertQueue('billing', { durable: true, arguments: { 'x-queue-type': 'quorum' } });
await ch.bindQueue('billing', 'orders', 'order.created');
 
await ch.assertQueue('emails', { durable: true, arguments: { 'x-queue-type': 'quorum' } });
await ch.bindQueue('emails', 'orders', 'order.*');
 
// publisher: knows only the exchange
ch.publish('orders', 'order.created', Buffer.from(JSON.stringify({ id: 42 })), { persistent: true });
 
// consumers: know only their queue
// billing instance 1 and billing instance 2 both run this, and share the messages
await ch.prefetch(10);
await ch.consume('billing', async (msg) => {
  await charge(JSON.parse(msg.content.toString()));
  ch.ack(msg);
});

order.created lands in both billing and emails. order.shipped only lands in emails.

Where people get lost

As soon as you have more than a handful of exchanges, the hard question becomes "which queues will this message reach?". The management UI lists bindings per exchange and per queue on separate pages, so following a message across exchange-to-exchange bindings, dead-letter exchanges and alternate exchanges means opening many tabs.

For a visual approach, see how to visually explore RabbitMQ queue bindings.

FAQ

What is the difference between an exchange and a queue in RabbitMQ?

An exchange routes messages to queues using bindings and stores nothing. A queue stores messages until consumers acknowledge them.

Do I need an exchange in RabbitMQ?

Every message goes through one. If you publish "to a queue", you are using the default exchange (""), which routes by queue name.

Can an exchange send messages to another exchange?

Yes. Exchange-to-exchange bindings let you build routing hierarchies, for example a topic exchange that forwards some messages to a fanout exchange.

What happens to a message when no queue is bound?

It's dropped silently. Use the mandatory publish flag to get it back, or an alternate exchange to send it to a catch-all queue.

Is a RabbitMQ topic the same as a Kafka topic?

No. A RabbitMQ topic exchange is a routing rule. A Kafka topic is a stored log. RabbitMQ streams are the closest equivalent to Kafka topics.

Every binding, in one place

Visualize and navigate bindings, dead-letter and alternate exchanges without jumping between pages.

Explore my bindings

Available on Windows, Mac, and Linux.

Read more RabbitMQ tutorials

RabbitMQ topic exchange: routing key wildcards explainedRabbitMQ tutorialRabbitMQ topic exchange: routing key wildcards explainedHow the RabbitMQ topic exchange matches routing keys with * andRabbitMQ 3.x end of life: what it means and how to upgrade to 4.xRabbitMQ tutorialRabbitMQ 3.x end of life: what it means and how to upgrade to 4.xRabbitMQ 3.13 lost community support in 2024. Here are the support dates for every series, the risks of staying, the 4.0 breaking changes and a step-by-step upgrade path to 4.x.Messages stuck in a RabbitMQ queue? 7 causes and how to fix themRabbitMQ tutorialMessages stuck in a RabbitMQ queue? 7 causes and how to fix themMessages piling up in RabbitMQ and not consumed? Diagnose missing consumers, stuck unacked messages, prefetch, slow consumers, poison messages, alarms and routing mistakes, with the commands to check each one.RabbitMQ queue types: classic vs quorum vs streamRabbitMQ tutorialRabbitMQ queue types: classic vs quorum vs streamWhich RabbitMQ queue type should you use? Classic, quorum and stream queues compared on replication, data safety, ordering, features and performance, for RabbitMQ 4.x.RabbitMQ health checks: CLI, HTTP API, Docker and KubernetesRabbitMQ tutorialRabbitMQ health checks: CLI, HTTP API, Docker and KubernetesCopy-paste RabbitMQ health checks. rabbitmq-diagnostics commands, /api/health endpoints, a Docker Compose healthcheck and Kubernetes probes that don't hurt your cluster.The RabbitMQ Management UI: a complete guideRabbitMQ tutorialThe RabbitMQ Management UI: a complete guideHow to enable the RabbitMQ management plugin, open the UI on port 15672 (Docker too), log in, and what each tab does, plus its limits.