Learn Labs
Foundations

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 task

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

exchangewhere publishers send messages; it ROUTES, it doesn't storequeuewhere messages wait until a consumer takes and acknowledges thembindinga rule: "exchange X sends messages matching K to queue Q"routing keya string the publisher attaches; exchanges match it against binding keys

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="...")   # -> nowhere

That 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

PropertyOnMeaning
durableExchange, queueSurvives a broker restart (quorum queues and streams always are)
exclusiveQueueOnly this connection may use it; deleted when the connection closes
auto_deleteQueue, exchangeDeleted when the last consumer or binding goes
delivery_mode=2MessagePersistent: written to disk (always true in quorum queues)
x-queue-typeQueueclassic, quorum, or stream
x-message-ttl, x-max-length, x-dead-letter-exchangeQueueExpiry, size limits, dead-lettering (or set by policy)

Lab

  1. In the UI (http://localhost:15672, admin/admin123), open vhost shop and find the orders exchange and its bindings.
  2. Publish a message from the UI's exchange page with routing key order.created.eu. Which queues received it?
  3. Declare orders.billing as a plain durable queue from Python, once as app and once as admin, and compare the two errors.
  4. Open 50 connections in a loop and watch Connections in the UI. Then open 50 channels on one connection instead.

On this page