RabbitMQ Exchange Types: How messages are sent and received

By: Published: April 11, 2022

RabbitMQ Exchange Type- Featured Image

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?

RabbitMQ Logo: RabbitMQ Exchange Type
Image Source: Medium

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.

Read More About

What Is RabbitMQ Exchange?

RabbitMQ Exchange Process: RabbitMQ Exchange Type
Image Source: Chi Thuc Nyugen- Medium

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. 

Simplify your ETL and Analysis with Hevo’s No-code Data Pipeline

Hevo Data a Fully-managed Data Pipeline platform, can help you automate, simplify & enrich your data replication process in a few clicks. With Hevo’s wide variety of connectors and blazing-fast Data Pipelines, you can extract & load data from 100+ different sources (including 40+ free sources) straight into your Data Warehouse or any Databases. To further streamline and prepare your data for analysis, you can process and enrich raw granular data using Hevo’s robust & built-in Transformation Layer without writing a single line of code!

Get Started with Hevo for Free

Hevo is the fastest, easiest, and most reliable data replication platform that will save your engineering bandwidth and time multifold. Try our 14-day full access free trial today to experience an entirely automated hassle-free Data Replication!

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: RabbitMQ Message Cycle
Image Source

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:

  1. Direct Exchange
  2. Topic Exchange
  3. Fanout Exchange
  4. Headers Exchange

Additionally, there are two more RabbitMQ exchange types:

  1. Default Exchange
  2. Dead Letter Exchange

Direct Exchange

RabbitMQ Exchange Type: Direct Exchange
Image Source

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).

What makes Hevo’s ETL Process Best-In-Class

Providing a high-quality ETL solution can be a difficult task if you have a large volume of data. Hevo’s automated, No-code platform empowers you with everything you need to have for a smooth data replication experience.

Check out what makes Hevo amazing:

  • Fully Managed: Hevo requires no management and maintenance as it is a fully automated platform.
  • Data Transformation: Hevo provides a simple interface to perfect, modify, and enrich the data you want to transfer.
  • Faster Insight Generation: Hevo offers near real-time data replication so you have access to real-time insight generation and faster decision making. 
  • Schema Management: Hevo can automatically detect the schema of the incoming data and map it to the destination schema.
  • Scalable Infrastructure: Hevo has in-built integrations for 100+ sources (with 40+ free sources) that can help you scale your data infrastructure as required.
  • Live Support: Hevo team is available round the clock to extend exceptional support to its customers through chat, email, and support calls.
Sign up here for a 14-day free trial!

Topic Exchange

Topic Exchange: RabbitMQ Exchange Type
Image Source

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

Fanout Exchange: RabbitMQ Exchange Type
Image Source

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

Headers Exchange: RabbitMQ Exchange Type
Image Source

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.

Dead Letter Exchange: RabbitMQ Exchange Type
Image Source

Recommended

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. 

Visit our Website to Explore Hevo

You wouldn’t need extensive training to be able to use Hevo. Setting up a Data Pipeline is as easy as a few clicks: select your source, provide credentials, and choose your destination from Data Warehouses or Databases like PostgreSQL, MySQL, or MS SQL Server. Hevo offers 100+ pre-built integrations that you can choose from.

Want to try Hevo for yourself? Why not Sign Up and take advantage of our 14-day free trial.

You’d be amazed to see how easy everything becomes from Data Migration, Data Transformation to Data Analysis. Hevo Data with its strong integration with 100+ Sources allows you to not only export data from sources & load data to the destinations, but also transform & enrich your data, & make it analysis-ready so that you can focus only on your key business needs.

Check out our unbeatable pricing page for more details on selecting the best option for your needs.

Post your opinions about learning RabbitMQ exchange types in the comment box below.

Syeda Famita Amber
Freelance Technical Content Writer, Hevo Data

Syeda is a freelance writer having passion towards wiriting about data industry who creates informative content on data analytics, machine learning, AI, big data, and business intelligence topics.

No-code Data Pipeline for Your Data Warehouse