RabbitMQ Exchange Types: Direct, Fanout, Topic, and Headers
Fast recall
Quick revision
Core answer
Whether the candidate understands the core routing mechanisms that underpin every RabbitMQ design decision.
Confusing topic and headers exchanges, or not knowing that fanout completely ignores the routing key.
Explain real-world use cases for each type and mention that direct is the default exchange type in Spring AMQP.
Interview-ready guide
Interview answer
Every message in RabbitMQ passes through an exchange before reaching a queue — the exchange is the routing engine, and the four exchange types differ in how they decide which queues receive each message.
Direct exchange — exact key matching:
- Publish with routing key
order.createdand only queues bound with that exact key receive the message — the workhorse for task queues and point-to-point messaging - The default exchange is a nameless direct exchange where every queue is auto-bound using its name as the routing key, which is why beginner tutorials work without explicit exchange declarations
- Direct exchanges can also fan out: bind multiple queues with the same key, and each gets a copy — surprising for those who assume direct is always point-to-point
Fanout exchange — unconditional broadcast:
- Ignores the routing key entirely and broadcasts to every bound queue, making it the right choice for cache invalidation and notification fan-out where every subscriber needs every message
- A common debugging trap: publishing to a fanout while expecting filtered delivery because you set a routing key — the key is silently ignored
Topic exchange — wildcard pattern routing:
- Uses dot-delimited routing keys with wildcards:
*matches exactly one word,#matches zero or more;audit.#catchesaudit.payment.failed,audit.user.login, everything under audit - The most frequent topic exchange bug: confusing
*and#—audit.*would NOT matchaudit.payment.failedbecause that's two words after the dot
Headers exchange:
- Routes by message header attributes with AND/OR logic via
x-match: allorx-match: any— powerful for multi-dimensional routing but slower than key matching due to per-message header inspection
I recommend starting with direct for simple task distribution, moving to topic when you need pattern-based routing, and treating headers as a last resort where the overhead is justified.
RabbitMQ Exchange Types: Direct, Fanout, Topic, and Headers
Direct Answer
RabbitMQ has four exchange types: direct routes by exact routing key match, fanout broadcasts to all bound queues, topic routes by pattern matching on routing keys, and headers routes based on message header attributes instead of routing keys.
Common Mistakes & Pitfalls
Confusing topic and headers exchanges, or not knowing that fanout completely ignores the routing key.
Check your understanding
Answer these without looking back at the article.
- What happens to a message if no queue is bound to an exchange with the given routing key?
- How would you implement a publish-subscribe pattern where new subscribers automatically receive events?
- When would you choose a topic exchange over multiple direct exchanges?
Related interview prep categories
1Want a guided curriculum order?
These modules are also sequenced in our flagship backend interview track — open it when you prefer an ordered path instead of browsing by topic.