Hello guys!
First, I’ll explain what RabbitMQ is!
RabbitMQ is an open-source messaging software (message broker) that implements AMQP (Advanced Message Queuing Protocol) protocol and supports several languages.
Cool, but what is an Exchange?
When a producer (software responsible for sending a message) publishes a message in RabbitMQ, that message is received by an exchange.
Upon receiving the message, the exchange sends a copy of that message to the queues that are bound to it.
In a quick analogy, exchanges are often compared to post offices or mailboxes.
There are 4 types of default exchanges available in RabbitMQ.
Direct
Fanout
Topic
Headers
Below I will explain each type.
Direct
When creating a Direct exchange, it will direct the message to the queue that is bound to it.
Ok Julio, but if I have 2 queues on this exchange?
Good question my friend! ;)
In this case, the Routing Key takes effect.
Routing Key is an attribute responsible for informing the exchange to which queue the message should be directed.
Note: The exchange will only direct the received message to queues that have routing keys exactly the same.
Note 2: If the bound queues have the same routing key, this message is directed to those queues in parallel.
Fanout
Unlike the Direct type, in the fanout, we don't have the Routing Key figure.
When a Fanout exchange receives a message, a copy of this message is sent to all queues bound to it.
Topic
Imagine that the Topic type is the Direct type but with particularity, the Routing Keys Patterns.
In the Topic, it is possible to use patterns for routing keys.
These patterns can be created using the * and # characters.
When creating a routing key pattern with #, we are telling the exchange that this # can be replaced by 0 or n words.
When we use *, we are saying for exchange that * can only be replaced by 1 word.
Therefore, when an exchange of Topic type receives a message, it will check the Routing Key of the message, compare with the Routing Keys of the queues and send a copy of that message to all queues in which the Routing Keys (message and queue) combine.
Example:

Message Routing Key: routing.key.test
In the case above, the message will be sent to queue 1, 3 and 4. It will not be sent to 2 because the pattern requires that there is only one word after “routing.” and in our case, there are 2.
Headers
Before explaining this exchange, it is necessary to inform that it is possible to send attributes in the header of a message (similar to the header of HTTP)
Headers exchange is similar to Topic, but instead of comparing Routing Key, it compares the attributes present in the message header with the attributes present in the arguments defined when we bind a queue in exchange.
It’s possible to create a rule for this validation.
To create this rule, we use the argument with the “x-match” key with 2 possible values: any and all.
Any is comparable to || of a condition and all are comparable to &&.
Example:

When we send a message with the argument {“key1”: “value1”} in the header of it, this message will be directed only to the “headers.queue1” queue because there is the attribute {“key1”: “value1”} in the arguments for this queue.
In the case of the “headers.queue2” queue, the exchange will validate if the message header contains the 2 arguments since the “x-match” is set to all. Therefore, the exchange will not find the {“key2”: “value2”} argument in the message header and will not direct a copy of that message to that queue.
I hope that this quick explanation can help you to understand RabbitMQ Exchanges.
To know more: https://www.rabbitmq.com/tutorials/amqp-concepts.html
Join our community Slack and read our weekly Faun topics ⬇
Comments
No comments yet. Be the first.