Lesson 2 of 9 · 23m
Exchange Types and Routing Keys That Age Well
Use direct, fanout, topic, and headers exchanges deliberately, with routing-key conventions that survive new consumers and changing ownership.
Bad routing-key design is quiet for six months and then very loud. A team adds one consumer for analytics, another for notifications, a regional partition, and a replay tool — suddenly nobody knows whether order.eu.created, order.created.eu, or order-created is canonical. RabbitMQ gives you flexible routing, but it does not forgive ambiguity. Choose a grammar before the graph grows.
The four exchange types in practice
direct routes when the routing key exactly equals the binding key. Use it for narrow command delivery:
exchange type: direct
routing key: invoice.generate
binding key: invoice.generatefanout ignores the routing key and copies to every bound queue. Use it for broadcast facts where every consumer wants the event:
exchange type: fanout
queues: audit.events, search.indexer, cache.invalidatetopic matches dot-separated words with * for exactly one word and # for zero or more words. This is the workhorse for domain events.
headers routes on message header attributes instead of the routing key. It exists, and some legacy systems use it well, but it is usually harder to inspect and evolve. Prefer topic unless you have a real reason.
A topic grammar that survives growth
Use nouns and lifecycle verbs in a stable order:
<domain>.<entity>.<event>.<qualifier>
order.created.eu
order.cancelled.eu
payment.captured.us
inventory.reserved.warehouse-04Then bindings stay expressive:
channel.queue_bind("audit.all", "domain-events", routing_key="#")
channel.queue_bind("billing.eu", "domain-events", routing_key="order.*.eu")
channel.queue_bind("fraud.orders", "domain-events", routing_key="order.created.#")Be careful with #. It is powerful and therefore easy to over-subscribe. A consumer bound to # will receive every future event type, including the ones it cannot safely process. Explicit bindings age better than universal curiosity.
Commands and events are different shapes
A command asks one worker to do something: invoice.generate. An event states that something happened: invoice.generated. Do not publish events to fanout exchanges and then pretend they are commands with exactly one processor. Conversely, do not force commands through broadcast semantics unless you intend competing consumers on one queue.
commands -> direct exchange -> one work queue -> competing consumers
events -> topic/fanout exchange -> one queue per interested consumer groupThat single distinction prevents a lot of accidental duplicate execution.
Inspect routing before you blame consumers
The management API and CLI can show bindings:
rabbitmqadmin list exchanges name type durable
rabbitmqadmin list bindings source destination routing_key
rabbitmqadmin list queues name messages consumersWhen a message "did not arrive," first answer: was the exchange correct, did any binding match, was the queue durable, and did the publish include the expected routing key? Routing failures are usually topology mismatches, not consumer crashes.
Field notes
- Wildcard matching applies to words separated by dots, not arbitrary substrings;
order.*does not matchorder.created.eu. - Topic bindings are evaluated at publish time by the broker; changing a binding does not rewrite messages already sitting in queues.
- Fanout multiplies write amplification. Ten bound queues means ten independent copies and ten independent backlogs.
- Document the ownership of every routing-key namespace before letting teams invent new ones. Small conventions prevent large migrations.
Was this lesson helpful?