RabbitMQ Exchange Types: 6 Categories Explained Easy
RabbitMQ is a lightweight, open-source, and easy-to-deploy message broker that enables services and applications to communicate with each other. It supports multiple messaging protocols like AMQP (Advanced Message Queuing Protocol), and MQTT (MQ Telemetry Transport). STOMP (Simple (or Streaming) Text Orientated Messaging Protocol) and a few more.
RabbitMQ provides powerful routing capabilities that are fluid and can adapt to the ever-changing needs of the user and services. It employs the use of message queues to enqueue and dequeue messages in the FIFO manner, which makes it easy to scale processes. RabbitMQ exchange receives messages from producers and pushes them to queues depending on the rules defined by the RabbitMQ exchange type.
In this guide, we explore the four RabbitMQ exchange types: direct exchange, topic exchange, fanout exchange, and headers exchange. Before we get into them, we’ll go over the key ideas and how the basic message cycle works in RabbitMQ. In addition, we’ll look at two more RabbitMQ exchange types: default and a dead letter.
Table of Contents
- What Is RabbitMQ?
- What Is RabbitMQ Exchange?
- Role of RabbitMQ Exchange
- RabbitMQ Message Cycle
- RabbitMQ Exchange Types
- Conclusion
What Is RabbitMQ?
RabbitMQ is a multi-language, open-source messaging software (message broker) that implements the AMQP (Advanced Message Queuing Protocol) protocol. It is a simple message broker that can handle a wide range of protocols and can be utilized to meet high-scale and high-availability requirements in both distributed and federated environments.
The RabbitMQ server is written in the Erlang programming language and is clustered and failover-ready. Message queuing allows programs to communicate more effectively by sending out messages asynchronously. It also functions as a temporary storage location for messages while the destination application is busy or unavailable.
Using asynchronous communication, programs that generate and consume messages can communicate with a message queue, where the sender and receiver don’t need to interact at the same time.
What Is RabbitMQ Exchange?
In RabbitMQ, a producer never delivers a message straight to a message queue. Instead, it makes use of exchange as a routing middleman. As a result, the RabbitMQ exchange determines if the message is routed to one queue, several queues, or is simply discarded.
To provide a brief overview, RabbitMQ exchanges are message routing agents configured by the virtual host in RabbitMQ. Exchange is in charge of routing messages to different queues using header attributes, bindings, and routing keys.
Role of RabbitMQ Exchange
RabbitMQ’s exchange is used as a routing mediator, to receive messages from producers and push them to message queues according to rules provided by the RabbitMQ exchange type. Each RabbitMQ exchange type uses a separate set of parameters and bindings to route the message. Clients get the option to create their own exchanges or use the default exchanges.
RabbitMQ Message Cycle
RabbitMQ Message Cycle Concepts
Before you can fully grasp the RabbitMQ message cycle, you must first understand some fundamental concepts:
Producers/Publisher
In the real world, producers/publishers can be imaged as people who send their mail to the post office. In the RabbitMQ world, producers/publishers are operators who send the job to RabbitMQ exchanges. Just like postmen, who simply route mail to specified postboxes, RabbitMQ exchanges simply route tasks to certain queues.
Task
In our real world, a task is mail that the postman delivers to the mailbox. In RabbitMQ, a task is a technical data that is sent to be exchanged.
Queue
Just like a postbox that holds all our mails, a queue is a container that holds all your jobs, which are processed in a first-in-first-out (FIFO) fashion. In FIFO, messages that are received first are processed first, and sent first, i.e., processing, and delivery happens in the order it is received.
Binding
A binding is a “connection” that you build between a queue and an exchange.
Routing Key
The routing key is a message attribute taken into account by the exchange when deciding how to route a message.
Basic RabbitMQ Message Cycle
With the basics of the RabbitMQ message cycle in place, let’s now have a look at how the RabbitMQ message cycle works.
Here’s what a basic RabbitMQ message cycle looks like:
Step 1: An exchange message is sent out by the producer.
Step 2: After the communication has been received, the exchange is responsible for sending it. It uses information from the RabbitMQ exchange type to direct the message to the relevant queues and exchanges.
Step 3: The queue receives the message and stores it until the consumer receives it.
Step 4: Finally, the consumer handles the message.
RabbitMQ Exchange Types
There are four basic RabbitMQ exchange types in RabbitMQ, each of which uses different parameters and bindings to route messages in various ways, These are:
Additionally, there are two more RabbitMQ exchange types:
Direct Exchange
The first RabbitMQ exchange type, the direct exchange, uses a message routing key to transport messages to queues. The routing key is a message attribute that the producer adds to the message header. You can consider the routing key to be an “address” that the exchange uses to determine how the message should be routed. A message is delivered to the queue with the binding key that exactly matches the message’s routing key.
The direct exchange’s default exchange is “amq. direct“, which AMQP brokers must offer for communication. As is shown in the figure, queue A (create_pdf_queue) is tied to a direct exchange (pdf_events) with the binding key “pdf_create”. When a new message arrives at the direct exchange with the routing key “pdf_create”, the exchange sends it to the queue where the binding key = routing key; which is queue A in this example (create_pdf_queue).
Topic Exchange
Topic RabbitMQ exchange type sends messages to queues depending on wildcard matches between the routing key and the queue binding’s routing pattern. Messages are routed to one or more queues based on a pattern that matches a message routing key. A list of words separated by a period must be used as the routing key (.).
The routing patterns may include an asterisk (“*”) to match a word in a specified position of the routing key (for example, a routing pattern of “agreements.*.*.b.*” only matches routing keys with “agreements” as the first word and “b” as the fourth word). A pound symbol (“#”) denotes a match of zero or more words.
In topic exchange, consumers indicate which topics are of interest to them. The consumer establishes a queue and binds it to the exchange using a certain routing pattern. All messages with a routing key that matches the routing pattern are routed to the queue, where they will remain until the consumer consumes them. For the topic RabbitMQ exchange type, “amq.topic” is the default topic exchange that AMQP brokers must provide for message exchange.
Fanout Exchange
A fanout exchange, like direct and topic exchange, duplicates and routes a received message to any associated queues, regardless of routing keys or pattern matching. Here, your provided keys will be entirely ignored.
Fanout exchanges are useful when the same message needs to be passed to one or perhaps more queues with consumers who may process the message differently. As shown in the image, a message received by the fanout exchange is copied and routed to all three queues associated with the exchange. When something happens, such as a sporting event or weather forecast, all connected mobile devices will be notified. For the fanout RabbitMQ exchange type, “amq.fanout” is the default exchange that must be provided by AMQP brokers.
Headers Exchange
A headers RabbitMQ exchange type is a message routing system that uses arguments with headers and optional values to route messages. Header exchanges are identical to topic exchanges, except that instead of using routing keys, messages are routed based on header values. If the value of the header equals the value of supply during binding, the message matches.
In the binding between exchange and queue, a specific argument termed “x-match” indicates whether all headers must match or only one. For the message to match, any common header between the message and the binding should match, or all of the headers referenced in the binding must be present in the message.
The “x-match” property has two possible values: “any” and “all,” with “all” being the default. A value of “all” indicates that all header pairs (key, value) must match, whereas “any” indicates that at least one pair must match. Instead of a string, headers can be built with a larger range of data types, such as integers or hashes. The headers exchange type (when used with the binding option “any”) is useful for steering messages containing a subset of known (unordered) criteria.
For the header RabbitMQ exchange type, “amq.headers” is the default topic exchange that AMQP brokers must supply.
Default Exchange
The default exchange is an unnamed pre-declared direct exchange. Usually, an empty string is frequently used to indicate it. If you choose default exchange, your message will be delivered to a queue with the same name as the routing key. With a routing key that is the same as the queue name, every queue is immediately tied to the default exchange.
Dead Letter Exchange
Some queue consumers may be unable to process certain alerts, and the queue itself may reject messages as a result of certain events. For instance, a message is dropped if there is no matching queue for it. In that instance, Dead Letter Exchanges must be implemented so that those messages can be saved and reprocessed later. The “Dead Letter Exchange” is an AMQP enhancement provided by RabbitMQ. This exchange has the capability of capturing messages that are not deliverable.
Conclusion
In this article, we shared four RabbitMQ exchange types: direct, topic, fanout, and headers. To route the message, each RabbitMQ exchange type has its own set of parameters and bindings. Exchanges are message routing agents created by the virtual host in RabbitMQ. By sending out messages, message queuing helps programs to communicate more effectively.
While the destination application is busy or unavailable, it also serves as a temporary storage area for messages. Asynchronous communication is enabled through message queues, which means that instead of talking directly with one another, other programs that generate and consume messages communicate with the queue, without having to be present at the same time. Customers can either create their own exchanges or use the default exchanges as provided by RabbitMQ.
For a reliable and fault-free data migration from your applications, companies employ the use of ETL solutions like Hevo Data. While extracting and integrating several heterogeneous sources into your Data Warehouse like Amazon Redshift, Google BigQuery, Snowflake, or Firebolt is also a big task, Hevo makes everything easier using its No-Code Data Pipeline creation tools.
No comments:
Post a Comment