The AMQP Model: Exchanges, Queues and Bindings
How RabbitMQ routes a message: exchange, binding, queue, the default exchange, and why a message with no matching binding is dropped.
On this page (11)
- The problem: you published, and nothing arrived
- The big picture: exchange, binding, queue
- The default exchange
- Exchange types at a glance
- Run it: follow one message through the junction
- Why an unroutable message disappears
- Dropped, returned or rerouted
- Why does a message with no matching binding not wait in the exchange?
- Why does publishing to a queue name work without any binding?
- Summary
- Sources
In the previous lesson the Darkroom upload API sent a resize job and a worker picked it up. It looked as if the producer wrote straight into a queue, but every message in RabbitMQ goes through an exchange first, and the exchange decides which queues get a copy. This lesson shows that path, the exchange you have been using without knowing it, and the case where a message goes nowhere at all.
The problem: you published, and nothing arrived
Say you add a new exchange for upload events, darkroom.photos, and a queue for the thumbnail worker, darkroom.thumbnails. The API publishes a job. publish returns without an error. The queue stays empty and the worker waits forever.
Nothing crashed, and no log line tells you why. The message reached the broker and was then thrown away. To understand why, you need the model RabbitMQ uses to move messages.
The big picture: exchange, binding, queue
RabbitMQ implements AMQP 0-9-1. In that model, a publisher hands each message to an exchange, and the exchange copies it into queues by following bindings. Consumers then read from queues. A publisher never writes into a queue directly.
- Exchange. It receives messages and sends each one to any number of queues, including none. The exchange type decides how it routes.
- Binding. A rule that links an exchange to a queue. An exchange delivers to a queue only when that queue is bound to it. A binding can carry a routing key that some exchange types compare with the message's key.
- Queue. An ordered collection of messages. Consumers take messages from it.
Think of the exchange as a railway junction and bindings as the tracks leaving it. A message arrives with a routing key, and the junction sends it down every track whose rule matches. If no track matches, the train has nowhere to go.
The routing key is set by the publisher, per message. The binding key is set when you create the binding. For a direct exchange like darkroom.photos, a message goes to a queue when the two keys are exactly equal.
The default exchange
The previous lesson used an exchange too, even if the code never named one. In the amqplib 2.4.2 source, sendToQueue(queue, ...) is a one-line wrapper around publish('', queue, ...). The empty string is the name of the default exchange.
The broker creates the default exchange itself, so it is always there. Its name is the empty string, and it routes the way a direct exchange does. When you declare a queue, RabbitMQ binds that queue to the default exchange with the queue's name as the binding key. So publishing to "" with routing key darkroom.thumbnails lands in the queue of that name.
You cannot add your own bindings to it. The concepts guide says the default exchange "does not allow bind/unbind operations", and the experiment below shows the error you get. The default exchange is fine for a first program and for one-producer, one-queue jobs. Once one message needs to reach several queues, or the producer should not know queue names, you declare your own exchange.
Exchange types at a glance
The exchange type is the routing rule. This table is only a map; chapter 3 covers fanout, direct and topic in depth.
| Type | Routes a message to | Pre-declared exchanges | Where this course covers it |
|---|---|---|---|
| direct | queues whose binding key equals the routing key | "" (default), amq.direct | this lesson, then chapter 3 |
| fanout | every bound queue, key ignored | amq.fanout | chapter 3 |
| topic | queues whose binding pattern matches the routing key | amq.topic | chapter 3 |
| headers | queues whose binding matches message header values | amq.match, amq.headers | not covered |
The names in the third column come from the concepts guide. RabbitMQ also ships or supports other types, such as local random and consistent hashing, which are outside this course.
Run it: follow one message through the junction
Reproduce the empty-queue problem, then fix it. You need the broker from lesson 2 on localhost:5672 and Node.js 18 or newer.
mkdir darkroom-routing && cd darkroom-routing
npm init -y && npm pkg set type=module
npm install amqplib@2.4.2
Save this as routing-lab.js. It runs five steps and prints how many messages the queue holds after each one.
// Darkroom routing lab: exchange -> binding -> queue, the default exchange,
// and what happens to a message that matches no binding.
import amqp from 'amqplib';
const EXCHANGE = 'darkroom.photos';
const QUEUE = 'darkroom.thumbnails';
const pause = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const conn = await amqp.connect('amqp://127.0.0.1');
conn.on('error', (err) => console.error('connection error:', err.message));
const ch = await conn.createChannel();
ch.on('error', (err) => console.error('channel error:', err.message));
// Returned messages arrive here (only for publishes with mandatory: true).
ch.on('return', (msg) => {
const { replyCode, replyText, exchange, routingKey } = msg.fields;
console.log(`returned: ${replyCode} ${replyText} exchange=${exchange} key=${routingKey}`);
});
async function depth(step) {
await pause(200); // give the broker a moment to route
const { messageCount } = await ch.checkQueue(QUEUE);
console.log(`${step} -> ${QUEUE} holds ${messageCount}`);
}
const job = (photoId) => Buffer.from(JSON.stringify({ photoId, size: 256 }));
await ch.assertExchange(EXCHANGE, 'direct');
await ch.assertQueue(QUEUE);
// 1. Exchange and queue both exist, but nothing connects them.
ch.publish(EXCHANGE, 'thumbnail', job('p-101'));
await depth('1 no binding');
// 2. Add the binding: key "thumbnail" leads to the queue.
await ch.bindQueue(QUEUE, EXCHANGE, 'thumbnail');
ch.publish(EXCHANGE, 'thumbnail', job('p-102'));
await depth('2 bound with key thumbnail');
// 3. A key no binding matches, this time with mandatory: true.
ch.publish(EXCHANGE, 'panorama', job('p-103'), { mandatory: true });
await depth('3 key panorama, mandatory');
// 4. The default exchange (""): the routing key is the queue name.
ch.publish('', QUEUE, job('p-104'));
await depth('4 default exchange');
// 5. Try to bind to the default exchange on a throwaway channel.
const probe = await conn.createChannel();
probe.on('error', (err) => console.log('5 bind to default exchange ->', err.message));
await probe.bindQueue(QUEUE, '', 'anything').catch(() => {});
// What actually reached the queue?
for (let msg; (msg = await ch.get(QUEUE, { noAck: true })); ) {
console.log('in queue:', msg.content.toString(), 'via', JSON.stringify(msg.fields.exchange));
}
await ch.deleteQueue(QUEUE);
await ch.deleteExchange(EXCHANGE);
await conn.close();
Run it with node routing-lab.js. This is the output from RabbitMQ 4.4.1, amqplib 2.4.2 and Node.js 24:
1 no binding -> darkroom.thumbnails holds 0
2 bound with key thumbnail -> darkroom.thumbnails holds 1
returned: 312 NO_ROUTE exchange=darkroom.photos key=panorama
3 key panorama, mandatory -> darkroom.thumbnails holds 1
4 default exchange -> darkroom.thumbnails holds 2
5 bind to default exchange -> Channel closed by server: 403 (ACCESS-REFUSED) with message "ACCESS_REFUSED - operation not permitted on the default exchange"
in queue: {"photoId":"p-102","size":256} via "darkroom.photos"
in queue: {"photoId":"p-104","size":256} via ""
Each numbered line matches a step in the script:
- Job
p-101vanished. The exchange and the queue both existed, but no binding joined them. - After
bindQueue, jobp-102arrived. The binding is the missing track. - Job
p-103used the keypanorama, which no binding matches. Because it was published withmandatory: true, the broker sent it back, and thereturnhandler printed it. It never entered the queue. - Job
p-104went through the default exchange, addressed by queue name, and arrived. - Binding to the default exchange closed the channel with
403 ACCESS_REFUSED.
Only p-102 and p-104 reached the queue. The script deletes its queue and exchange at the end, so you can run it again.
The lab calls assertQueue(QUEUE) with no options, and amqplib then sets durable: true. On this broker, whose overview reports default_queue_type: classic (you saw it in lesson 2), rabbitmqctl list_queues name type durable showed darkroom.thumbnails as classic and true. Lesson 3 declared darkroom.resize as a quorum queue instead. Do not pass durable: false here: transient non-exclusive classic queues cannot be declared by default from RabbitMQ 4.3.0. On 4.4.1 the broker closes the whole connection:
Connection closed: 541 (INTERNAL-ERROR) with message "INTERNAL_ERROR - Feature `transient_nonexcl_queues` is deprecated.
By default, this feature is not permitted anymore.
The feature will be removed from a future major RabbitMQ version, regardless of the configuration; actual version to be determined.
To..."
Durability gets its own lesson in chapter 2.
Why an unroutable message disappears
An exchange can send a message to any number of queues, including none, and the queues are where messages wait. If routing finds zero matching queues, the message is unroutable, and the broker has to do something with it right away. What it does depends on how the publisher sent it:
- With
mandatoryfalse, which is the default, the guide says the message "is discarded or republished to an alternate exchange, if any". - With
mandatorytrue, "the message will be returned to it". Your publisher sees it only if it listens for returned messages; in amqplib that is the channel'sreturnevent.
This is why step 1 failed so quietly. publish in amqplib does not wait for the broker, and with the default flags the broker has no reason to answer. The producer cannot tell "routed" from "dropped".
Dropped, returned or rerouted
You have three ways to handle a message that matches no binding:
| Option | What happens to the message | Producer finds out? | Cost |
|---|---|---|---|
Default (mandatory: false) | discarded | no | none, but losses are silent |
mandatory: true | returned with a reply code, 312 NO_ROUTE above | yes, through a return handler | handler code in every publisher |
| Alternate exchange | republished to the alternate exchange, which routes it with its own bindings | no | one more exchange and queue to watch |
An alternate exchange receives what the original exchange could not route. The message lands in a queue only if the alternate exchange has a matching binding; if not, it moves on to that exchange's own alternate exchange, when one is set (how it works). The docs name collecting unroutable messages and topology migrations as its uses. This course does not set one up; the alternate exchange guide shows how.
Returned messages also do not cover the opposite question: did the broker accept the message at all? That needs publisher confirms, which chapter 2 covers. RabbitMQ also has a metric for unroutable dropped messages, so dropped traffic can show up in monitoring even when no code handles it.
For Darkroom, a sensible starting point: declare exchanges, queues and bindings in code before the first publish, as the lab does. Then turn on mandatory for messages you cannot afford to lose without noticing, such as resize jobs.
Why does a message with no matching binding not wait in the exchange?
Because messages wait in queues, and the exchange only routes. When no binding matches, there is no queue to put the message in, so the broker drops it, returns it if mandatory is set, or hands it to an alternate exchange if one is configured.
Why does publishing to a queue name work without any binding?
Because of the default exchange. Every queue is bound to it automatically, with the queue name as the key. Publishing to exchange "" with routing key darkroom.thumbnails matches that automatic binding. It is still exchange routing, not a direct write into the queue.
Summary
- A message always goes publisher → exchange → binding → queue → consumer. Publishers never write into queues.
- A binding links an exchange to a queue. For a direct exchange, the routing key must equal the binding key.
- The default exchange
""is a direct exchange that binds every queue under its own name.sendToQueuein amqplib publishes to it. You cannot add bindings to it. - The exchange type sets the routing rule: direct, fanout, topic or headers.
- A message that matches no binding is discarded by default.
mandatory: truereturns it to the publisher; an alternate exchange gives it a second chance to be routed.
That closes chapter 1. Chapter 2, Work Queues and Reliable Delivery, follows a job after it lands in the queue: consumer acknowledgements, fair dispatch with prefetch, durable queues and persistent messages, and publisher confirms.
Sources
Looked up on 2026-11-29 against RabbitMQ 4.4 (Docker image rabbitmq:4.4.1-management) and amqplib 2.4.2 on Node.js 24.
- AMQP 0-9-1 Model Explained
- Exchanges, including the default exchange and alternate exchanges
- Publishers: unroutable message handling
- Alternate Exchanges
- Queues: durability
- amqplib README
- amqplib 2.4.2 source: lib/channel_model.js, lib/api_args.js
