Scrivio Interview library
ConceptFoundations5 min read · updated 12 November 2026

Explain the direct, topic, fanout and headers exchange types in RabbitMQ. An order service publishes with routing key "order.created.eu"; which binding patterns on a topic exchange would match it?

A direct exchange routes a message to the queues whose binding key equals the routing key exactly. A topic exchange matches dot-separated words, where * matches exactly one word and # matches zero or more words. A fanout exchange copies every message to all bound queues and ignores the routing key, and a headers exchange routes on message header values instead. For the routing key order.created.eu, the patterns order.created.*, order.#, #.eu and *.created.* all match, while order.* does not, because * covers only one word.

Why this question is asked

In RabbitMQ, producers never send directly to a queue. They send to an exchange, and the exchange decides which queues get a copy. Picking the right exchange type and writing correct binding patterns is the core of every RabbitMQ design, and a wrong wildcard silently drops messages or delivers them to the wrong service. Interviewers ask this to check the model and the exact matching rules. In this answer, we will recap exchanges, queues and bindings, go through the four exchange types with examples, work through the order.created.eu patterns word by word, and look at the default exchange that hides behind many simple examples.

1A Quick Recap of Exchanges, Queues and Bindings

A queue stores messages until a consumer takes them. An exchange receives messages from producers and routes them. A binding is a rule that connects an exchange to a queue, usually with a binding key that says which messages the queue wants. When a producer publishes, it names an exchange and gives a routing key, a short string describing the message, such as order.created.eu. The exchange compares the routing key, or for one type the headers, with each binding and puts a copy of the message into every queue whose binding matches.

So the exchange type decides the matching rule. If no binding matches, the message is dropped, unless the producer asked for unroutable messages to be returned.

2Direct, Fanout and Headers Exchanges

A direct exchange uses exact equality. A queue bound with key invoice.paid receives messages whose routing key is exactly invoice.paid, nothing else. Several queues may use the same key, and each gets a copy. It suits routing by a fixed category, such as a priority level or a task type.

A fanout exchange ignores the routing key completely and copies every message to every bound queue. It suits broadcast: every service that is bound gets every message, as in cache invalidation.

A headers exchange ignores the routing key too and matches on message headers. A binding lists header values and an x-match argument: all means every listed header must match, any means one is enough. It is useful when the routing criteria do not fit naturally into one dotted string, but it is less common and a little slower than topic routing.

The four exchange types
TypeMatches onExample
directrouting key equals binding keykey invoice.paid -> queue bound with invoice.paid
topicdotted words with * and #order.created.eu -> queue bound with order.#
fanoutnothing: all bound queuesevery message -> every queue
headersheader values, x-match all or anyregion=eu, type=order -> audit queue

3How Topic Matching Works

A topic exchange treats the routing key as a list of words separated by dots. order.created.eu has three words: order, created and eu. A binding pattern is also a list of words, where two words are special: * matches exactly one word, and # matches zero or more words. Matching goes word by word from the left, and the whole routing key must be consumed by the whole pattern.

Java
routing key: order.created.eu (3 words)
order.created.* -> match // * = eu
order.# -> match // # = created.eu
#.eu -> match // # = order.created
*.created.* -> match // * = order, * = eu
# -> match // # = everything
order.*.eu -> match // * = created
order.created.eu -> match // exact words
order.* -> no match // * takes created, eu is left over
*.created -> no match // eu is left over
order.created -> no match // a pattern with no wildcard needs all 3 words
order.created.eu.# -> match // # may match zero words
Step throughMatching order.created.* against order.created.eu
  1. Compare word 1order = order
  2. Compare word 2created = created
  3. Word 3: pattern has ** takes exactly one word: eu
  4. Both lists fully used?
    YesMatch: queue gets a copyNoNo match

4 steps in order. Play them, or step through them one at a time.

Notice the order.* case, which is the one people get wrong. It looks like it should mean anything starting with order, but * is exactly one word, so it matches order.created and not order.created.eu. To mean anything under order, we write order.#. The last line shows the other side of #: it may match nothing at all, so order.created.eu.# still matches a key with exactly those three words.

4The Default Exchange

Every RabbitMQ virtual host has a nameless direct exchange, written as the empty string, and every queue is bound to it automatically with its own name as the binding key. So publishing to the exchange "" with routing key invoices puts the message into the queue named invoices. This is why simple examples seem to send straight to a queue. It is convenient for point-to-point work, but it ties the producer to a queue name; using a named exchange lets us add, rename or split queues later without changing producers.

Java
rabbitTemplate.convertAndSend("", "invoices", invoice); // default exchange: queue invoices
rabbitTemplate.convertAndSend("orders", "order.created.eu", order); // topic exchange orders

5Follow-up questions

When would you choose a headers exchange over a topic exchange?

When the routing criteria are several independent attributes that do not have a natural order, such as region, customer tier and message type, and consumers want arbitrary combinations, or when a value contains dots that would confuse topic matching. With a topic exchange we would have to fix the word order in the routing key and every producer must follow it. In most systems, a well-designed routing key such as type.action.region is simpler and fast enough, so headers exchanges are the exception.

What happens if a message matches two bindings to the same queue?

The queue receives the message once. RabbitMQ delivers at most one copy per queue for a given publish, even if several bindings from the exchange to that queue match.

6Conclusion

In this answer, we saw that producers publish to exchanges, and the exchange type decides which bound queues receive a copy. Direct exchanges match the routing key exactly, topic exchanges match dot-separated words with * for one word and # for zero or more, fanout exchanges copy to every bound queue, and headers exchanges match header values. For order.created.eu, the patterns order.created.*, order.#, #.eu and *.created.* match, while order.* does not. The default exchange routes to the queue whose name equals the routing key.

What to say in the interviewAbout 80 seconds aloud

Open with

In RabbitMQ a producer publishes to an exchange with a routing key, and bindings connect the exchange to queues. The exchange type decides how the routing key is compared with the binding keys.

Then, if they want more

A direct exchange delivers to queues whose binding key is exactly equal to the routing key. A topic exchange treats the routing key as words separated by dots and supports two wildcards: star matches exactly one word and hash matches zero or more words.

A fanout exchange ignores the routing key and copies the message to every bound queue, which is good for broadcasts. A headers exchange ignores the routing key and matches on header values, with x-match all or any.

For the routing key order.created.eu, which is three words, order.created.star matches because the star takes eu, order.hash matches because hash takes created.eu, hash.eu matches because hash takes order.created, and star.created.star matches with one word on each side.

But order.star does not match, because star is exactly one word, so eu would be left over; to match everything under order you need order.hash. And there is also the default exchange, a nameless direct exchange where every queue is bound by its own name, so publishing with the queue name as routing key sends straight to that queue.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
RabbitMQ documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Explain the direct, topic, fanout and headers exchange types in RabbitMQ. An order...": https://scrivio-interview-library.netlify.app/spring-boot/rabbitmq-and-jms/75eecf76b3/