Direct, topic, fanout and headers

RabbitMQ Course · lesson 3 of 15 · 6 min read

Four routing rules, and the routing key syntax that decides which queues get a copy.

Open this lesson in the learning hub

Key points

  • A direct exchange delivers to queues whose binding key equals the routing key exactly.
  • A topic exchange splits the key on dots: * matches one word, # matches zero or more.
  • A fanout exchange ignores the routing key entirely and copies to every bound queue.
  • A headers exchange matches the headers table using x-match set to all or any.
  • Every vhost ships with amq.direct, amq.fanout, amq.topic and amq.headers predeclared.
  • A routing key is capped at 255 bytes, so encode a hierarchy in it, never the payload.

Example

# a topic exchange plus three subscribers, no publisher changes needed
rabbitmqadmin declare exchange name=orders type=topic durable=true

rabbitmqadmin declare queue name=invoicing durable=true
rabbitmqadmin declare queue name=audit     durable=true
rabbitmqadmin declare queue name=eu.fraud  durable=true

# exact event only
rabbitmqadmin declare binding source=orders destination=invoicing \
  routing_key="order.created"

# every order event, however deep the key
rabbitmqadmin declare binding source=orders destination=audit \
  routing_key="order.#"

# any single region word, then .cancelled
rabbitmqadmin declare binding source=orders destination=eu.fraud \
  routing_key="order.*.cancelled"

The exchange type is the routing rule; * is exactly one word and # is zero or more.

This is a reading copy. The full lesson — with the visual explainer, the interactive lab and a Run button for the code — lives in the RabbitMQ Course course, and every lesson in it is listed on the RabbitMQ Course contents page.