RabbitMQ Exchange Types

Jhamukul
6 min readDec 24, 2022

RabbitMQ can scale up to around 50,000 messages per second.

It is less than Apache Kafka and Redis Pub/Sub. But there are some advantages of rabbitMQ over these two.

You can’t schedule messages in Apache Kafka and Redis Pub/Sub.

Message scheduling was a major use case, so I used rabbitMQ over Kafka and Redis.

Let’s deep dive into RabbitMQ.

You can’t publish messages directly to the queue.

Queue binds to the exchange.

Queue recoring_queue bind with exchange video.direct.exchange

You have to publish messages to exchange and exchange push messages to respective queues.

Exchange: 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.

There are various types of exchange types.

  1. Direct Exchange: A direct exchange delivers messages to queues based on the message routing key.

Process :

Queue binds with exchange with a specific binding key(b_k).

When a new message with routing key R arrives at the direct exchange, the exchange routes it to the queue if b_k = R

Topic Exchange
Direct exchange config in rabbitMQ console

Two queues recoring_queue bind to video.direct.exchange with binding key recording.

video_editing_queue binds to the same direct exchange with binding key editing.

Message publish to exchange video.direct.exchange with routing key recording and exchange routes it to the recoring_queue with binding key recording.

Message publish to video.direct.exchange with routing key recording
Message received by recoring_queue

You can scale by adding more queues with the same binding key to the exchange.

Direct exchanges are often used to distribute tasks between multiple workers in a round-robin manner.

Direct exchange distributes tasks between multiple workers in a round-robin manner.

2. Fanout Exchange: A Fanout exchange routes messages to all of the queues that are bound to it and the routing key is ignored.

Assume n queues bind with fanout exchange and a message arrives at fanout exchange, the exchange routes the message to all n queues.

Use Case :
Multiplier games, News broadcasts, Cricket commentary, etc.

Fanout Exchange
Fanout exchange and queues bind with exchange

Created a fanout exchange with name video.editing.fanout and bound two queues video_editing_queue and video_editing_queue_2 with the exchange.

Publish a message on video.editing.fanout exchange and it routed the message to bound queue video_editing_queue and video_editing_queue_2.

Message received by these two bind queues.
Message received by video_editing_queue and video_editing_queue_2 respectively.

3. Topic Exchange: 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.

Topic Exchange

There are two important special cases for binding keys:

  1. Star(*) can substitute for exactly one word.
  2. Hash(#) can substitute for zero or more words.
Topic exchange and queue binding with routing key

cricket_queue: wants to receive all cricket-related data like news, live score, etc

football_queue: wants to receive all football-related data like news, live score, etc

tennis_queue: wants to receive all tennis-related data like news, live score, schedules, etc.

When we publish a message with routing pattern live.cricket.news all the bound queues will receive it.

When we publish a message to exchange sports.topic.exchange with routing key live. cricket.news or live.cricket.score or live.cricket.today.news the message will be routed to cricket_queue.

A message published to sports.topic.exchange and received by cricket_queue
Message received by cricket_queue

Publishing message to exchange sports.topic.exchange with routing key live.cricket.today.news and the same message received by cricket_queue

A message published to sports.topic.exchange and received by cricket_queue

When we publish a message to exchange sports.topic.exchange with routing key tennis.live or tennis.news or tennis.{any single word} the message will be routed to tennis_queue.

Message published to sports.topic.exchange with routing key tennis. live and received by tennis_queue
Message received by tennis_queue

Want to learn more about topic exchange?

4. Headers Exchange: 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.

Header Exchange

It is possible to bind a queue to a headers exchange using more than one header for matching. In this case, the broker needs one more piece of information from the application developer, namely, should it consider messages with any of the headers matching or all of them?
This is what the x-match binding argument is for.

x-match argument

  1. any: Just one matching header value is sufficient
  2. all: all the values must match.

Let’s create a headers exchange video.converter.header.ex and bind three queues to it.

Header Exchange and bounded queues
Queue                    Header Arguments

audio_mp3 format:mp3 type:audio x-match:any
video_avi format:avi type:video x-match:all
video_mp4 format:mp4 type:video x-match:all

When the message is published to the exchange video.converter.header.ex with header arguments

format : mp3

The message will be delivered to queue audio_mp3 because exchange.

When the message is published to the exchange video.converter.header.ex with header arguments

format : mp4

There are two queues bound with video.converter.header.ex with header format: mp4 but both have x-match:all so all headers should match.

Message can’t be routed to the specific queue. and it would be discarded.

Default Exchange

The default exchange is a direct exchange with no name (empty string). When you declare a queue with the name “any_queue_name”, it will automatically bind to the default exchange with the routing key “any_queue_name”. Therefore, a message published to the default exchange with the routing key “any_queue_name” will be routed to the queue “any_queue_name”.

Dead Letter Exchange

If there is no matching queue for the published message, the message is dropped and discarded by the exchange. RabbitMQ provides an AMQP extension known as the “Dead Letter Exchange”. This exchange provides the functionality to capture messages that are not deliverable.

Hope you like it !!! Thank you for reading 😍

--

--