Skip to the page

RabbitMQ · Concept

Direct, topic and fanout exchanges

The interviewer asks

In RabbitMQ a producer never sends straight to a queue. Explain exchanges, bindings and routing keys. With a topic exchange and these bindings, which queues get a message published with 'order.cancelled.mumbai'? And what changes with a direct or a fanout exchange?

queue refunds      bound with  order.cancelled.*
queue mumbai-ops   bound with  *.*.mumbai
queue audit        bound with  #
queue invoices     bound with  order.created.*
queue city-alerts  bound with  order.*

Then

Follow-ups the interviewer asks next

  1. Which queues get it, and why not city-alerts?
  2. What does # match that * doesn't?
  3. What happens to a message that matches no binding?
  4. Two copies of the refund service read one queue. Who gets each message?
  5. How do you give every service its own copy of each event?

The answer

What a strong answer covers

  • Producers publish to an exchange with a routing key; bindings connect an exchange to queues with a key or pattern; consumers read from queues.
  • Topic exchange: words separated by dots; * matches exactly one word, # matches zero or more. 'order.cancelled.mumbai' goes to refunds, mumbai-ops and audit. city-alerts (order.*) needs exactly two words, so it doesn't match, and neither does invoices.
  • Direct exchange: the routing key must equal the binding key exactly. Fanout: ignores the key and copies to every bound queue. Headers exchange: matches on message headers. The default exchange routes to the queue whose name equals the routing key.
  • Unroutable messages are dropped silently, unless the publisher sets the mandatory flag (the broker returns the message) or the exchange has an alternate exchange.
  • One queue with several consumers = competing consumers: each message goes to one of them. One queue per service, each bound to the exchange = every service gets its own copy (publish-subscribe).
  • RabbitMQ 4.x also speaks AMQP 1.0 natively next to AMQP 0-9-1; the routing model is the same.

The red pen

Common traps

Avoid:"RabbitMQ sends to queues directly"; thinking fanout looks at routing keys; not knowing unroutable messages vanish.

Practise this out loud

Say your answer out loud to an AI interviewer that follows up, and get a score for it.

Practise by voice

Practise questions like this out loud

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview