Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 37 ยท AMQP and Architecture

The AMQP 0-9-1 Model

Learn the AMQP 0-9-1 model used by RabbitMQ: exchanges, queues, bindings, routing keys, frames and the wire protocol, with runnable Python stand-ins.

RabbitMQ's native language is AMQP 0-9-1, the Advanced Message Queuing Protocol. It is not just a wire format: it defines a model of how messages move, with named parts and precise rules. If you learn this model once, every RabbitMQ guide, client library and error message becomes easier to read.

AMQP 0-9-1 is the protocol most RabbitMQ clients speak. RabbitMQ 4 also has native support for AMQP 1.0, which is a different protocol with a different model, and for MQTT and STOMP. This course teaches the 0-9-1 model unless it says otherwise. Runnable blocks are Python stand-ins; they explain the model and do not talk to a broker.

The model in one picture

flowchart LR
    P["Publisher"] -->|"message + routing key"| X["Exchange"]
    X -->|"binding key matches"| Q1["Queue 1"]
    X -->|"binding key matches"| Q2["Queue 2"]
    X -. "no match" .-> D["dropped or returned"]
    Q1 -->|"deliver"| C1["Consumer"]
    Q2 -->|"deliver"| C2["Consumer"]

Publishers never write to queues; exchanges route using bindings.

The five nouns:

The AMQP nouns

Message Properties plus a body of bytes.
Exchange Receives messages and routes them by rules.
Queue Stores messages for consumers.
Binding A rule that links an exchange to a queue.

A routing key is a string label the publisher attaches to each message.

1 / 2

An exchange is a routing table, not storage. If a message arrives and no binding matches, the exchange has nowhere to put it: by default the message is discarded, or it is handed back to the publisher if the publisher used the mandatory flag. Only queues store messages.

Who is responsible for what

AMQP 0-9-1 is a programmable protocol: the client defines the topology. Applications issue commands such as exchange.declare, queue.declare and queue.bind at startup. This is different from systems where an administrator pre-configures everything. It means the application and the broker must agree, and mismatched redeclarations raise errors.

The commands you will use most, grouped by class:

Class Methods
connection start, tune, open, close
channel open, flow, close
exchange declare, delete, bind
queue declare, bind, unbind, purge, delete
basic publish, consume, get, ack, nack, reject, qos, return
confirm select
tx select, commit, rollback

Client libraries turn these into friendly names such as channel.basic_publish, but the protocol names appear in logs and error messages, so it is useful to recognize them.

A model in 30 lines

This stand-in implements the model: exchanges, bindings and queues with direct routing. It is intentionally tiny; later lessons add the other exchange types.

Python Exchange, binding, queue (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Observe the three facts the model guarantees: one routing key can fan out to several queues, one queue can have several bindings, and a message matching nothing goes nowhere.

Frames: how bytes travel

On the wire, AMQP 0-9-1 splits everything into frames. A frame is: one byte of type, two bytes of channel number, four bytes of payload size, the payload, then a final byte 0xCE. The frame types are:

  • 1 method frame (a command such as basic.publish)
  • 2 content header frame (message properties and body size)
  • 3 content body frame (a chunk of the message body)
  • 8 heartbeat frame

Publishing a message therefore sends a method frame, a header frame and one or more body frames. A connection starts with the 8-byte protocol header AMQP followed by 0 0 9 1.

The next block builds real AMQP 0-9-1 bytes with Python's struct module: the protocol header and a heartbeat frame. These bytes are what a real client would send.

Python Build real AMQP frames with struct

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The channel number is how many conversations share one TCP connection (Connections and Channels). Channel 0 is special: it carries connection-level traffic such as heartbeats.

The handshake in brief

When a client connects, the two sides negotiate:

  1. Client sends the protocol header.
  2. Broker answers connection.start listing authentication mechanisms.
  3. Client answers start-ok with credentials (usually PLAIN).
  4. Broker proposes limits in connection.tune: maximum channels, maximum frame size and the heartbeat interval.
  5. Client confirms with tune-ok, then sends connection.open naming the virtual host.

Heartbeats are small frames exchanged periodically so both ends notice a dead peer; the default is 60 seconds, negotiable during tune.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a publisher publish to in AMQP 0-9-1?

    Publishers send to exchanges. The exchange uses bindings to decide which queues receive the message.
  2. 2. What happens to a message published to an exchange with no matching binding?

    Exchanges do not store messages. Without a match the message is dropped, or returned to the publisher when mandatory is set.
  3. 3. Which frame type carries message body data?

    Type 1 is a method, type 2 the content header, type 3 the body and type 8 a heartbeat.
  4. 4. Which channel number carries connection-level traffic such as heartbeats?

    Channel 0 is reserved for the connection itself.

Summary

  • The AMQP 0-9-1 model is exchanges, bindings and queues, with publishers writing to exchanges.
  • Exchanges route; queues store.
  • The client declares the topology using exchange, queue and basic methods.
  • On the wire, everything is frames ending in 0xCE; channel 0 is for connection traffic.
  • The handshake negotiates credentials, limits and heartbeats and picks a vhost.

Next: Connections and Channels