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.pyDirect: 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 droppedUse 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.#.
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:
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)Silently dropped messages are one of the most common RabbitMQ data-loss
bugs: a typo in a routing key, a binding someone deleted, a consumer
service that hasn't declared its queue yet at deploy time. Publish with
mandatory=True (and handle returns), or give important exchanges an
alternate exchange that collects anything unroutable. And watch
rabbitmq_global_messages_unroutable_dropped_total, which the
RabbitMQUnroutableMessages alert does.
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
- Run
exchanges.pyand match each result to its binding. - Add a binding
order.#.euto a new queue and predict which of the four topic messages it receives. - Declare a topic exchange with an alternate exchange
(
arguments={"alternate-exchange": "lab.unrouted"}) and a fanoutlab.unroutedwith a queue. Publish an unmatched key and find it. - In the UI, publish to
orderswith routing keyorder.created.usand check which pre-loaded queues got it.