AMQP Exchange Types
AMQP 0-9-1 (RabbitMQ) defines 4 exchange types. Publishers send to exchanges; exchanges route to bound queues. The exchange type determines routing logic.
4
Exchange Types
Direct Exchange
A direct exchange routes messages to queues whose binding key exactly matches the message's routing key. If routing key = 'order.created', only queues bound with 'order.created' receive the message. The default exchange (empty name) is a pre-declared direct exchange that routes by queue name.
Fanout Exchange
A fanout exchange broadcasts every received message to all queues bound to it. The routing key is completely ignored. Every bound queue gets a copy of the message. Use fanout for broadcast patterns: cache invalidation across services, event notifications to multiple subscribers, logging fanout to multiple sinks.
Topic Exchange
A topic exchange routes messages by matching the routing key against binding patterns using wildcards. * matches exactly one dot-separated word. # matches zero or more words. 'order.created.eu.de' matches 'order.created.#', 'order.*.eu.*', and '#'. Topic exchanges enable flexible selective subscriptions without changing publishers.
Headers Exchange
A headers exchange routes messages based on matching message header attributes rather than the routing key. Bindings specify key-value pairs and a match mode: 'x-match: all' requires all headers to match (AND), 'x-match: any' requires at least one header to match (OR). More flexible than topic but less commonly used due to overhead.