Skip to main content

Exchange types

Each exchange type reads a different part of a message to pick its queues: the routing key, a pattern over it, nothing, or the headers.

One message with routing key order.eu.placed and the header region=eu, offered to five exchange types, and the queues each one picksExchange typesA message: routing key order.eu.placed,header region = eudirectkey equals the bindingQueues bound withorder.eu.placedtopickey matches a patternQueues bound withorder.*.placed or order.#fanoutkey ignoredEvery queuebound to itheadersx-match = all or anyQueues whose bindingasks region = eux-consistent-hashplugin: hash of the keyOne queue, chosen inproportion to its weight
The diagram as text
  1. Exchange types: direct key equals the binding; Queues bound with order.eu.placed; topic key matches a pattern; Queues bound with order.*.placed or order.#; fanout key ignored; Every queue bound to it; headers x-match = all or any; Queues whose binding asks region = eu; x-consistent-hash plugin: hash of the key; One queue, chosen in proportion to its weight
  2. A message: routing key order.eu.placed, header region = eu → Exchange types: published to
  3. direct key equals the binding → Queues bound with order.eu.placed
  4. topic key matches a pattern → Queues bound with order.*.placed or order.#
  5. fanout key ignored → Every queue bound to it
  6. headers x-match = all or any → Queues whose binding asks region = eu
  7. x-consistent-hash plugin: hash of the key → One queue, chosen in proportion to its weight

A direct exchange copies the message into every queue bound with a key equal to its routing key. The default exchange is a direct exchange, with every queue bound under its own name.

A topic exchange reads the routing key as words separated by dots. A binding is a pattern over those words: * stands for exactly one word and # for zero or more. The key order.eu.placed matches order.*.placed and order.#, and does not match order.*.

A fanout exchange ignores the key and copies the message into every queue bound to it.

A headers exchange ignores the key too. It compares the message's headers with the binding's arguments. With x-match set to all every argument has to match, with any one is enough. In this crate a binding with arguments is bind_with.

A message can match several bindings. It then reaches every matching queue, once per queue.

The consistent-hash exchange comes from a plugin that ships with RabbitMQ and has to be enabled. It hashes the routing key and sends the message to one bound queue. A binding's key is its weight: a queue bound with weight 2 gets about twice the keys of a queue bound with weight 1. Messages with one routing key go to one queue in normal operation; a node restart can move a key to another queue. The crate offers it as RabbitExchange::consistent_hash under the plugin-consistent-hash feature, and Lanes and order compares it with lanes inside one subscription.

RabbitExchange names the exchange of a binding. custom names any other type a plugin adds.

The routing rules are RabbitMQ's (exchanges, topic exchanges, consistent hash exchange).