Learn Labs
Routing

Exchange Types

Direct, fanout, topic, and headers exchanges, and what happens to unroutable messages

An exchange's type decides how it matches a message against its bindings. labs/exchanges.py sends the same messages through all four types:

docker compose -f compose_prod.yaml run --rm labs python exchanges.py

Direct: exact match

A message goes to every queue whose binding key equals the routing key. A queue can have several bindings.

bindings:   lab.direct.errors <- "error"
          lab.direct.all    <- "error", "warning", "info"
published:  info, warning, error, debug

lab.direct.errors  <- ['log:error']
lab.direct.all     <- ['log:info', 'log:warning', 'log:error']
# "debug" matched nothing and was dropped

Use it for routing by a known category: log levels, job types, regions.

Fanout: everyone

The routing key is ignored; every bound queue gets a copy:

lab.fanout.email  <- ['user signed up']
lab.fanout.sms    <- ['user signed up']
lab.fanout.push   <- ['user signed up']

Use it for broadcasting an event to every interested service.

Topic: patterns

Routing keys are dot-separated words. Binding keys can use * (exactly one word) and # (zero or more words):

bindings:   lab.topic.all-eu     <- "order.*.eu"
          lab.topic.created    <- "order.created.*"
          lab.topic.everything <- "order.#"
published:  order.created.eu, order.created.us, order.shipped.eu, order.refund.issued.eu

lab.topic.all-eu      <- ['order.created.eu', 'order.shipped.eu']
lab.topic.created     <- ['order.created.eu', 'order.created.us']
lab.topic.everything  <- [all four]

Note order.refund.issued.eu did not match order.*.eu: * is exactly one word, and refund.issued is two. It did match order.#.

routing keyorder.*.eulab.topic.all-euorder.created.*lab.topic.createdorder.#lab.topic.everythingorder.created.eu✓✓✓order.created.us✗✓✓order.shipped.eu✓✗✓order.refund.issued.eu✗✗✓

Topic exchanges are the most common choice in practice, because a well-designed key scheme (<entity>.<event>.<region>) lets new consumers pick any slice without changes on the producer side. The pre-loaded orders exchange works this way: orders.billing binds order.*.*, orders.shipping.eu binds order.*.eu.

Headers: match on headers

The routing key is ignored; bindings match message headers, with x-match: all (every listed header must match) or x-match: any:

lab.headers.pdf-eu     (all: format=pdf, region=eu) <- ['pdf/eu']
lab.headers.pdf-or-eu  (any: format=pdf, region=eu) <- ['pdf/eu', 'pdf/us', 'csv/eu']

Headers exchanges are flexible and slower than topic matching, and rarely needed. Reach for them when the routing attributes don't fit a dot-separated hierarchy.

Unroutable messages

When no binding matches, the exchange drops the message silently. The publisher is told nothing, unless it asks:

mandatory=Truereturn unroutable messages to the publisher (basic.return)alternate-exchangeexchange argument: send unroutable messages to another exchange instead
ch.confirm_delivery()
ch.basic_publish("lab.direct", routing_key="nobody-listens", body="lost?", mandatory=True)
# pika.exceptions.UnroutableError

unroutable message with mandatory=True: returned to the publisher (UnroutableError)

Exchange-to-exchange bindings

Exchanges can bind to other exchanges (exchange_bind), building routing trees: a fanout audit exchange bound to every topic exchange, for example, so one queue sees everything without each producer publishing twice.

Lab

  1. Run exchanges.py and match each result to its binding.
  2. Add a binding order.#.eu to a new queue and predict which of the four topic messages it receives.
  3. Declare a topic exchange with an alternate exchange (arguments={"alternate-exchange": "lab.unrouted"}) and a fanout lab.unrouted with a queue. Publish an unmatched key and find it.
  4. In the UI, publish to orders with routing key order.created.us and check which pre-loaded queues got it.

On this page