# Exchanges, Queues, Bindings, Vhosts and Channels — RabbitMQ

Source: https://www.skillbyai.com/en/rabbitmq/f-model

> Describe the AMQP 0-9-1 model and how a message travels through it.

## Producers never publish to queues directly

In AMQP 0-9-1, producers publish to an **exchange**, not to a queue. Each message carries a **routing key** and optional **headers**. The exchange uses **bindings**, rules that link it to queues (often with a binding key or pattern), to decide which queues get a copy. **Queues** store messages until **consumers** receive and acknowledge them. A **virtual host (vhost)** is an isolated namespace with its own exchanges, queues and permissions, useful for separating applications or environments on one broker. Clients open a TCP **connection** (expensive, long-lived) and multiplex many lightweight **channels** over it; almost every operation happens on a channel. Because routing logic lives in exchanges and bindings, you can add a new consumer queue for an event without changing the producer at all.

## Declaring a small topology with pika (Python)

One exchange, one queue, one binding; declarations are idempotent if arguments match.

```python
import pika

params = pika.URLParameters("amqp://app:secret@rabbit.internal:5672/shop")   # vhost = shop
connection = pika.BlockingConnection(params)
channel = connection.channel()

channel.exchange_declare(exchange="orders", exchange_type="topic", durable=True)
channel.queue_declare(queue="billing.orders", durable=True,
                      arguments={"x-queue-type": "quorum"})
channel.queue_bind(queue="billing.orders", exchange="orders", routing_key="order.placed")

channel.basic_publish(exchange="orders", routing_key="order.placed",
                      body=b'{"orderId": "o-1001"}')
connection.close()
```

## A post office sorting room

You drop a letter in the post office (exchange) with an address (routing key). Sorting rules (bindings) decide which delivery bags (queues) it goes into, and postmen (consumers) carry each bag out. You never walk into the delivery van yourself.

**Quiz:** In AMQP 0-9-1, where does a producer publish a message?

- [ ] Directly into a queue file
- [ ] To a consumer's IP address
- [ ] To a vhost without any exchange
- [x] To an exchange, which routes it to queues through bindings

*Answer:* To an exchange, which routes it to queues through bindings. Producers publish to exchanges; bindings decide which queues receive the message.
