advanced
RabbitMQ exchanges
Choose direct, topic, fanout, or headers exchanges by routing intent and keep producers independent from queue topology.
Exchanges receive published messages and route them to queues via bindings. Direct exchanges match routing keys exactly; topic exchanges use wildcard patterns; fanout broadcasts to every bound queue; headers exchanges match message attributes. Producers publish to an exchange name and routing key without knowing which queues exist.
Choosing the exchange type is a topology decision: task queues often use direct or topic; event broadcast uses fanout; selective subscriptions use topic with stable key conventions. Decoupling producers from queue layout lets you add consumers without redeploying publishers.
On interviews: name the four exchange types, give one use case each, and explain why producers should not hard-code queue names.
Common pitfalls: using fanout when only a subset should receive messages; topic patterns that are too broad and create surprise deliveries; mixing exchange types without documenting routing contracts.
The trade-off is flexibility versus complexity—know when the simpler path is enough.
Checklist:
- Match exchange type to delivery intent.
- Keep routing keys stable and documented.
- Decouple producers from queue topology.
- Plan how new consumers are added safely.