Direct, topic, fanout and headers
Four routing rules, and the routing key syntax that decides which queues get a copy.
Open this lesson in the learning hubKey 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-matchset to all or any. - Every vhost ships with
amq.direct,amq.fanout,amq.topicandamq.headerspredeclared. - 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.
Exchanges, bindings and queues: how a message finds a queue
Acknowledgements: auto-ack is a data-loss switch
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.