RabbitMQ

Exchange Types

Pick the exchange type that matches your routing pattern: direct, topic, fanout, or headers.

The exchange type determines how a message is routed from the exchange to queues. There are four. A direct exchange routes to queues whose binding key exactly matches the message's routing key — ideal for point-to-point or simple level-based routing. A topic exchange matches routing keys against patterns with wildcards (* for one word, # for zero or more), giving flexible publish/subscribe by category.

Direct is addressing a letter to one named department. Topic is a magazine subscription by category ("all sports.football news"). Fanout is the building-wide PA system — everyone hears it. Headers is routing mail by a checklist of attributes (urgent + international) rather than the address line.

Key Concepts

1
A fanout exchange ignores the routing key entirely and broadcasts to every bound queue — the classic pub/sub broadcast. A headers exchange routes on message header attributes instead of the routing key, matching all or any of a set of header values, useful when routing depends on multiple criteria.
2
There is also a default (nameless) exchange: publishing to it with a routing key equal to a queue name delivers straight to that queue, which is why simple examples appear to "publish to a queue".
3
What the interviewer is probing.
What the interviewer is probing.
4
1. "Name the exchange types and when each fits." *Probing:* breadth. *Stalls:* "Direct." *Moves up:* direct for exact routing key match, topic for pattern matching, fanout for broadcast, headers for attribute-based routing.
1. "Name the exchange types and when each fits."
5
2. "When would you use a topic exchange?" *Probing:* the fit. *Stalls:* "Always." *Moves up:* when consumers want subsets by pattern — a logging consumer taking all errors regardless of service, for example.
2. "When would you use a topic exchange?"
6
3. "What does a fanout exchange ignore?" *Probing:* the detail. *Stalls:* "Nothing." *Moves up:* the routing key entirely — every bound queue gets a copy.
3. "What does a fanout exchange ignore?"
7
4. "Why is a headers exchange rarely used?" *Probing:* the honest answer. *Stalls:* "It is common." *Moves up:* it is slower than topic routing and harder to reason about, and most cases fit a topic pattern.
4. "Why is a headers exchange rarely used?"