Skip to the page

RabbitMQ · core

RabbitMQ exchanges, bindings and routing keys

In RabbitMQ a producer sends to an exchange, not to a queue. Bindings route messages to queues, and the exchange type decides how.

The notes

What you must know

  • Producers publish to an exchange with a routing key. Bindings connect the exchange to queues with a key or a pattern. Consumers read from queues.
  • Topic exchange: words separated by dots. * matches exactly one word and # matches zero or more words.
  • Example: a message with key order.cancelled.mumbai matches bindings order.# and .cancelled. but not order.* (two words only).
  • Direct exchange: the routing key must equal the binding key. 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.
  • A message that matches no binding is dropped silently, unless the publisher sets the mandatory flag or the exchange has an alternate exchange.
  • One queue with several consumers means competing consumers: each message goes to one of them. One queue per service, each bound to the exchange, means every service gets its own copy.
  • Current RabbitMQ also speaks AMQP 1.0 next to AMQP 0-9-1; the routing model is the same.

The red pen

Common traps: mistake → fix

  • Mistake: RabbitMQ sends to queues directly

    Fix: producers use exchanges.

  • Mistake: Fanout looks at routing keys

    Fix: it ignores them.

  • Mistake: Unroutable messages come back as errors

    Fix: they vanish unless you use mandatory or an alternate exchange.

Easy to hard

Questions interviewers ask, easy to hard

  1. Explain exchanges, bindings and routing keys.
  2. Which queues get a message with key order.cancelled.mumbai, given these topic bindings?
  3. What is the difference between direct, topic and fanout?
  4. What happens to a message nobody is bound for?

Practise

Practise with these questions

Your first interview is free

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

Start your free interview