1. Introduction
RabbitMQ is an open-source message broker that supports multiple messaging protocols. It provides message routing patterns by using exchanges to route messages to bound queues stored in a FIFO( first in, first out) manner. The routing rules vary depending on the exchange type, and this article will outline different RabbitMQ exchange types.
2. What is RabbitMQ Exchange
RabbitMQ exchange is a component of the message broker system which distributes messages to queues based on predefined rules. It allows publishers to send messages to an exchange that can be routed to one or more queues using a routing key or message header attributes.
When a publisher sends a message to the RabbitMQ, it’s never published directly to the queue. The broker uses exchanges as a routing mechanism for routing messages according to principles defined by the exchange type, such as queues whose binding key matches the message routing key.
The routing keys, queue binding and header attributes are different ways of routing messages.
3. Fanout Exchange
Fanout exchange in RabbitMQ is an exchange type that doesn’t use a routing key but sends the same message to all queues bound to the exchange. This type of exchange is ideal for dispatching messages to multiple destinations without specifying any routing pattern or headers. With fanout exchanges, messages are delivered indiscriminately to all queues bound to the exchange.
If you have multiple consumer applications that need to interpret the message differently, a fanout exchange is exactly what you’re looking for. It will deliver copies of your message to each application.
4. Direct Exchange
A Direct Exchange uses a routing key to route messages from the producer to the consumer. This can be used to send messages directly to specific queues without having to traverse all the exchange routes in the system. When a binding key matches the routing key of the exchange bound queue, it will receive the message. Direct Exchange is primarily used when there is a need to send messages directly and not be broadcasted across all the queues.
5. Topic Exchange
A topic exchange in RabbitMQ is a type of exchange that poses similar behaviour to direct exchange. It allows the message routing key to be based on exact strings or pattern-matching, known as binding keys.
The routing pattern can consist of special symbols called wildcard matching. The star (*) wildcard symbol will accept any word, but the (#) hash is even more adaptive as it can handle multiple words. Furthermore, when neither of these characters is present in a string, then topic exchange behaviour remains the same as a direct exchange.
This type of exchange means that messages can be routed to any queue whose binding key exactly matches by string or the pattern. For example, if multiple queues are bound to the same exchange, messages whose routing keys match one of those binding keys will be routed to the corresponding queue.
Messages whose routing keys match multiple binding keys can be routed to all queues with a matching key. However, the exchange will discard messages whose routing keys don’t match binding keys.
6. Header Exchange
Headers exchange in RabbitMQ is a powerful tool for routing messages based on the message header. Unlike default exchange, which routes messages to queues with a name equal to the routing key, headers exchanges allow you to specify multiple headers and their corresponding values when sending a message. The exchange then matches the specified headers against those sent with each incoming message and routes them accordingly. This makes sending messages to multiple queues possible without specifying individual routing keys for each queue. Header exchanges can also be combined with other types of exchange, such as direct or fanout, allowing for further flexibility in your messaging architecture.
When binding the header exchange with a queue, the extra x-match argument must be specified during the exchange queue binding; it can only hold two distinct values, all or any. It will expect to match all the headers by their key-value attributes when it’s set to all. In case of any, at least one header must match; otherwise, the message won’t be routed to that queue.
7. Default and Dead Letter Exchange
A default exchange is an anonymous, pre-declared direct exchange that sends messages with empty routing keys to the queue with a name equal to the routing key. This means sending a message with an empty routing key to this type of exchange will be routed to the corresponding queue.
As such, default exchanges are convenient for simple messaging scenarios, where the messages don’t need to be routed to multiple queues.
Dead Letter Exchange is a specific type of exchange that preserves messages that cannot be processed so they can be investigated and reprocessed later. When an exception occurs during the processing, such a message would be routed to DLE as part of exception handling.
When a message is dropped, if there is no matching queue, it could also end up in the exchange. The DLE and DLQ binding uses routing key conventions generally correspond to the preceding queue’s route with ‘dlq’ appended at the finish.
8. Summary
In this article, we have looked at RabbitMQ exchange types. Need your message to reach as many people as possible? Fanout’s got you covered! Or perhaps something more specific is what you’re looking for – Directs and Topics provide a solution tailored just for that. But suppose none of these fit the bill. In that case, Headers exchange offers additional flexibility with its ability to send messages based on their header content. At the same time, it can be combined with previous exchanges and their routing patterns.
Daniel Barczak
Daniel Barczak is a software developer with a solid 9-year track record in the industry. Outside the office, Daniel is passionate about home automation. He dedicates his free time to tinkering with the latest smart home technologies and engaging in DIY projects that enhance and automate the functionality of living spaces, reflecting his enthusiasm and passion for smart home solutions.
Leave a Reply