The AMQP Model
Connections, channels, vhosts, exchanges, queues, and bindings
RabbitMQ's core protocol, AMQP 0.9.1, has a small vocabulary. Learn these six words and the rest of the module is combinations of them.
Connections and channels
A connection is a TCP connection from a client to a node, authenticated as a user, scoped to one virtual host. A channel is a lightweight session multiplexed inside a connection. Almost every operation (publish, consume, declare, ack) happens on a channel.
conn = pika.BlockingConnection(pika.URLParameters("amqp://app:app123@rabbit-1:5672/shop"))
ch = conn.channel() # cheap: open one per thread, or per logical taskConnections are expensive (TCP and TLS handshakes, authentication, memory on the broker); channels are cheap. Opening a connection per message is the most common RabbitMQ performance bug: the broker spends its time on handshakes, and the Connections opened / s panel on the dashboard lights up. Open one long-lived connection per process and reuse it. Channels aren't thread-safe in most clients, so use one per thread.
Virtual hosts
A vhost is a namespace: its own exchanges, queues, bindings, users'
permissions, and policies. This stack has / and shop. Use them to
separate environments or tenants on one cluster. Nothing is shared between
vhosts.
Exchanges, queues, bindings
Publishers never write to queues directly. They publish to an exchange with a routing key; the exchange looks at its bindings and copies the message into every matching queue. If nothing matches, the message is dropped (or returned, see Publisher Confirms).
ch.exchange_declare("lab.topic", "topic", durable=True)
ch.queue_declare("lab.topic.all-eu", durable=True)
ch.queue_bind("lab.topic.all-eu", "lab.topic", routing_key="order.*.eu")
ch.basic_publish("lab.topic", routing_key="order.created.eu", body="...") # -> lab.topic.all-eu
ch.basic_publish("lab.topic", routing_key="order.created.us", body="...") # -> nowhereThat indirection is RabbitMQ's main idea: producers describe what a message is (its routing key); bindings, configured separately, decide who gets it. Adding a new consumer means adding a queue and a binding, with no change to any producer.
The default exchange
Every vhost has a nameless direct exchange, "", with every queue
automatically bound to it by its own name. Publishing to "" with
routing_key="lab.jobs" sends straight to the lab.jobs queue. Most "hello
world" examples use it, which hides the exchange concept entirely.
Declaring is idempotent
exchange_declare and queue_declare create the object if it's missing
and do nothing if it already exists with the same properties. If it
exists with different properties, the channel is closed with
PRECONDITION_FAILED:
# as admin: orders.billing exists as a quorum queue with a dead-letter exchange
ch.queue_declare("orders.billing", durable=True)
(406, "PRECONDITION_FAILED - inequivalent arg 'x-dead-letter-exchange' for queue 'orders.billing'
in vhost 'shop': received none but current is the value 'dlx' of type 'longstr'")
# as app: permissions are checked first
(403, "ACCESS_REFUSED - configure access to queue 'orders.billing' in vhost 'shop' refused for user 'app'")That's why many teams declare topology in one place, definitions.json
here or infrastructure-as-code, rather than in every application.
Properties that matter
| Property | On | Meaning |
|---|---|---|
durable | Exchange, queue | Survives a broker restart (quorum queues and streams always are) |
exclusive | Queue | Only this connection may use it; deleted when the connection closes |
auto_delete | Queue, exchange | Deleted when the last consumer or binding goes |
delivery_mode=2 | Message | Persistent: written to disk (always true in quorum queues) |
x-queue-type | Queue | classic, quorum, or stream |
x-message-ttl, x-max-length, x-dead-letter-exchange | Queue | Expiry, size limits, dead-lettering (or set by policy) |
Lab
- In the UI (http://localhost:15672,
admin/admin123), open vhostshopand find theordersexchange and its bindings. - Publish a message from the UI's exchange page with routing key
order.created.eu. Which queues received it? - Declare
orders.billingas a plain durable queue from Python, once asappand once asadmin, and compare the two errors. - Open 50 connections in a loop and watch Connections in the UI. Then open 50 channels on one connection instead.