Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 37 ยท Queues and Exchanges

Bindings and Routing Keys

Understand RabbitMQ bindings: how queue_bind links exchanges to queues, the difference between routing key and binding key, multiple bindings and unbinding.

A binding is the rule that connects an exchange to a queue (or to another exchange). Without a binding, an exchange has nowhere to send anything. Bindings are where most routing mistakes happen, because two similar-sounding words are involved: the routing key and the binding key.

pika snippets are read-only for a real broker. Runnable examples are Python stand-ins for the binding rules.

Two keys, two roles

  • The routing key is a string the publisher attaches to each message.
  • The binding key is a string a consumer-side declaration attaches to a binding.

The exchange compares them. What "compare" means depends on the exchange type: equal for direct, ignored for fanout, wildcard pattern for topic. In the AMQP specification the binding key is literally called the "routing key" argument of queue.bind, which is why the two are often confused. Keep them straight by asking who wrote the string: the publisher (routing key) or the binder (binding key).

flowchart LR
    P["Publisher<br/>routing key = invoice.paid"] --> X(["Exchange"])
    X -->|"binding key = invoice.paid"| Q1["billing"]
    X -->|"binding key = invoice.*"| Q2["audit"]
    X -->|"binding key = refund.#"| Q3["refunds"]

On a topic exchange the first two bindings match invoice.paid; the third does not.

Creating a binding

Python
# real pika code, read-only
channel.exchange_declare("billing", "topic", durable=True)
channel.queue_declare("billing.invoices", durable=True)

channel.queue_bind(
    queue="billing.invoices",
    exchange="billing",
    routing_key="invoice.*",      # this is the BINDING key
)

Facts worth memorizing:

  • Binding is idempotent: binding the same queue, exchange and key again does nothing.
  • A queue can have many bindings, to one exchange (different keys) or to several exchanges.
  • An exchange can have many queues bound with the same key.
  • Bindings of durable queues to durable exchanges survive restarts. If either side is transient the binding disappears with it.
  • When a queue or exchange is deleted, its bindings are removed automatically.

One copy per queue

If two bindings from the same exchange to the same queue both match a message, the queue still receives one copy. Copies are per queue, not per binding. This surprises people who bind log.* and *.error to the same queue and expect log.error to arrive twice. The stand-in confirms it.

Python Several matching bindings, one copy (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.

Removing a binding

Python
# real pika code, read-only
channel.queue_unbind(queue="billing.invoices", exchange="billing", routing_key="invoice.*")

Unbinding stops new messages from arriving; messages already in the queue stay. Use unbinding to turn a feed off without deleting the queue or losing its backlog.

Binding arguments

A binding can carry an arguments table. Direct, fanout and topic exchanges ignore it. Headers exchanges use it as the match rule, as you will see in Headers and the Default Exchange. Binding arguments also distinguish two otherwise identical bindings: the pair (queue, key, arguments) is the identity of a binding.

Exchange-to-exchange bindings

Python
# real pika code, read-only
channel.exchange_bind(
    destination="audit",
    source="billing",
    routing_key="#",
)

Here every message published to billing also flows into audit, which can route it onward. Use this to compose reusable layers rather than binding every queue to every exchange.

Inspecting bindings

In the management UI, an exchange's page lists its bindings, and a queue's page shows the exchanges feeding it. On the command line:

Terminal
rabbitmqctl list_bindings source_name destination_name routing_key

When messages "go missing", this is the first place to look: a typo in a binding key leaves the queue unbound while everything else appears healthy.

Printing a topology table

Reading a topology as a table makes mistakes obvious. This stand-in stores bindings and reports queues with no inbound binding.

Python Find queues nobody feeds (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.

Every queue is automatically bound to the default exchange by its own name, so even orphan can still be reached that way: but it receives nothing from named exchanges.

Naming conventions

Good names make bindings readable:

  • Exchanges named for the domain or event stream: orders, billing.events.
  • Queues named for the consumer and purpose: email-service.order-confirmation.
  • Keys that read like a path: order.placed.eu, invoice.paid.

Avoid including environment names in exchange or queue names; use separate vhosts instead.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Who sets the routing key?

    The routing key is part of each publish. The binding key is set when a queue is bound to an exchange.
  2. 2. A queue has two bindings to the same exchange that both match a message. How many copies does the queue get?

    Copies are made per queue, not per binding.
  3. 3. What happens to messages already in a queue when you unbind it?

    Unbinding only stops new messages from arriving.
  4. 4. What identifies a binding uniquely?

    A binding is the combination of exchange, destination, binding key and arguments.

Summary

  • A binding links an exchange to a queue or another exchange.
  • Routing key = publisher's string; binding key = binder's string; the exchange type defines how they are compared.
  • Bindings are idempotent, many-to-many, and removed with their queue or exchange.
  • A queue gets one copy of a message even when several bindings match.
  • Check bindings first when messages go missing.

Next: Queue Types: Classic, Quorum and Streams