Skip to content

Exchanges & Routing

An exchange stores nothing. Its one job is routing: take a published message and decide which queues get a copy. How it decides is set by the exchange’s type. Learn the four types and you can express almost any message-flow topology.

LessonThe exchange decides by…
Direct exchangeAn exact routing key match
Fanout exchangeNothing — it copies to every bound queue
Topic exchangeA pattern match on a dotted routing key
Headers & choosingMessage headers, plus a guide to picking a type
flowchart TB
  msg["message
routing key: order.created"] --> direct["direct
match key exactly"]
  msg --> fanout["fanout
ignore key, copy to all"]
  msg --> topic["topic
match key pattern"]
  msg --> headers["headers
match on header values"]
How each exchange type routes the same message

The mental model to carry through this module:

  • A message always carries a routing key (a string like order.created) and optional headers.
  • Each binding from an exchange to a queue may carry a binding key (or pattern, or header conditions).
  • The exchange type is the rule that compares the message against the bindings to pick queues.

Everything else — pub/sub, work distribution, event filtering — is just a choice of exchange type plus some bindings.

What is the single job of an exchange?
What determines how an exchange routes a message?
Which exchange type ignores the routing key entirely?