AMQP Bindings

LavinMQ allows a lot of flexibility. A lot of this flexibility is in the bindings and how it is defined by the routing key, headers, and the type of exchange. A binding is an association or relation between a queue and an exchange. It describes which queue is interested in messages from a given exchange.

What is a binding?

A binding is an association or relation between a queue and an exchange. A queue tells the exchange that it is interested in messages from the given exchange, given some directives in the routing key on the binding and headers.

Bindings between queues and exchanges

Bindings are rules that exchanges use to route messages to queues. A queue must be bound to at least one exchange in order to receive messages from publishers.

Code example in Python, Pika:

channel.queue_bind(exchange=exchange_name, queue=queue_name)

Routing keys

Bindings can take an extra parameter called routing key. A routing key can also be sent with a message. The routing key on the binding is sometimes called a binding key. The routing key in the message is what the exchange is looking at while delivering messages.

Code example with routing key in Python, Pika:

channel.queue_bind(exchange=exchange_name, queue=queue_name, routing_key=test)

Read more about how different exchanges are using routing keys.

Header attributes

In the header exchange, a special argument named x-match, added in the binding between exchange and queue, specifies if all headers must match or just one. Either any common header between the message and the binding count as a match, or all the headers referenced in the binding need to be present in the message for it to match.

The x-match property can have two different values: any or all, where all is the default value. A value of all means all header pairs (key, value) must match, while a value of any means at least one of the header pairs must match. Headers can be constructed using a wider range of data types, integer or hash for example, instead of a string. The headers exchange type (used with the binding argument any) is useful for directing messages which contain a subset of known (unordered) criteria.

Code example in Ruby (bunny):

queue   = channel.queue("", :exclusive => true).bind(x,
  :arguments => {"os" => "linux", "cores" => 8, "x-match" => "all"})