Introduction to Exchanges
Exchanges are AMQP entities where messages are sent to.
Exchanges take a message and route it into zero or more queues.
The routing algorithm used depends on the exchange type and rules called bindings.
AMQP 0-9-1 brokers provide four exchange types:
Exchange TypeDefault Pre-declared namesDirect exchange
(Empty string) and amq.direct
Fanout exchange
amq.fanout
Topic exchange
amq.topic
Headers exchange
amq.match (and amq.headers in RabbitMQ)
Exchanges are declared with a number of attributes, the most important of which are:
Name
Durability (exchanges survive broker restart)
Auto-delete (exchange is deleted when last queue is unbound from it)
Arguments (optional, used by plugins and broker-specific features)
Exchanges can be durable or transient.
Durable exchanges survive broker restart whereas transient exchanges do not (they have to be redeclared when broker comes back online).
Not all scenarios and use cases require exchanges to be durable.
The default exchange is a direct exchange with no name (empty string) pre-declared by the broker.
This makes it ideal for simple applications, every queue that is created is automatically bound to it by the messsage broker with a routing key which is the same as the queue name.
The default exchange makes it seem like it is possible to deliver messages directly to queues, even though that is not technically what is happening.
A direct exchange delivers messages to queues based on the message routing key.
A direct exchange is ideal for the unicast routing of messages. They can be used for multicast routing as well.
Working of direct exchanges,
A queue binds to the exchange with a routing key
K
.When a new message with routing key
R
arrives at the direct exchange, the exchange routes it to the queue, ifK = R
.If multiple queues are bound to a direct exchange with the same routing key
K
, the exchange will route the message to all queues for whichK = R
.
A fanout exchange routes messages to all of the queues that are bound to it and the routing key is ignored.
If
N
queues are bound to a fanout exchange, when a new message is published to that exchange a copy of the message is delivered to allN
queues. Fanout exchanges are ideal for the broadcast routing of messages.
Topic exchanges route messages to one or many queues based on matching between a message routing key and the pattern that was used to bind a queue to an exchange.
The topic exchange type is often used to implement various publish/subscribe pattern variations.
Topic exchanges are commonly used for the multicast routing of messages.
Whenever a problem involves multiple consumers/applications that selectively choose which type of messages they want to receive, the use of topic exchanges should be considered.
A headers exchange is designed for routing on multiple attributes that are more easily expressed as message headers than a routing key.
Headers exchanges ignore the routing key attribute.
The attributes used for routing are taken from the headers attribute.
A message is considered matching if the value of the header equals the value specified upon binding.
Headers exchanges can be looked upon as "direct exchanges on steroids".
Because they route based on header values, they can be used as direct exchanges where the routing key does not have to be a string; it could be an integer or a hash (dictionary) for example.
Learn more
Last updated