What is RabbitMQ

Server Tombak
nacressoftware
Published in
4 min readJun 23, 2024
RabbitMQ

Hi everyone, I hope you are all doing well and in good health. let’s learn about RabbitMQ, which might seem a bit complex but I’ll try to simplify it :)

What is RabbitMQ?

RabbitMQ is an open-source message broker that facilitates data transfer using the AMQP (Advanced Message Queuing Protocol). It receives data from publishers and delivers it to consumers. More details will be provided so go on to read it 👇👇

When to Consider using RabbitMQ?

Decoupling Services: When you need to decouple different parts of your application RabbitMQ ensures that services can communicate asynchronously without waiting for each other.

Load Balancing: RabbitMQ helps in distributing tasks or messages across multiple consumers (workers), ensuring efficient workload management and scaling.

Transactional Messaging: For operations where message delivery and processing must be atomic and reliable, RabbitMQ supports transactional messaging patterns

Error Handling and Retry Mechanisms: With RabbitMQ’s Dead Letter(Explained on Exchange Types section) Exchanges and retry mechanisms, you can manage failed messages and implement robust error handling strategies.

These are some of the reason that why we use RabbitMQ and some of the advantages.

How does it work?

We are publishing messages(data) to a queue and then consuming from there. And while we are publishing to the queue we are choosing an exchange type and taking it to the queue with routes. There are 4 different types of exchanges: Fanout, Direct, Topic, Headers, Default, and Dead Letter. Exchanges works together with routes while publishing the data to the queue. That’s why we will talk about exchange types and learn about routes too.

RabbitMQ Exchange Types

general exchange model

Direct Exchange: While sending the data we are giving a routing-key and if we are using the Direct Exchange type then its publishing the data to the queue which has the same routing-key.

Direct Exchange

Topic Exchange: Topic exchange works kind of similar with like operator in sql. So we are giving the keyword and some rules with routes and its publishing the message if the queue has that routing-key. For example: .finance, #.health.

Topic Exchange

Fanout Exchange: Fanout exchange publish the message to the all queues without check and look for any rule.

Fanout Exchange

Header Exchange: If you know Header in HTTP protocol this is kind of similar. We can add header values to the Exchange and queues. If the header values match then it publishing to the queue

In this Header exchange type, x-match is using as a pattern to check header values. And x-match has any and all operation types. When there are more than one header value and if we use any operation then x-match checks if any of the headers match with queue if it matches than it publishes. And with all operation type we are saying that all header values should match with queue header values otherwise it will not publish to the queue.

Default Exchange:

The default exchange in RabbitMQ exists implicitly for direct routing without explicitly creating it, and it uses the empty string (“”) as its name in AMQP. And its publishing the data to the queue that has the same routing key.

Dead Letter: When a message published to RabbitMQ cannot be routed to any queue, RabbitMQ can handle it using a Dead Letter Exchange (DLX). This mechanism allows you to define an exchange where such “undeliverable” messages are redirected.

So In summary we have a publisher and consumer. When we are publishing it to the queue we are doing that by choosing an Exchange type and using routes. That’s how we are publishing to the queue that we want. And after that operation the consumer service is consuming it from the queue that we published the message.

That was all I wanted to write about RabbitMQ I will write and publish a code demo with ASP.Net. You can check my medium account for it.

If you liked the article don’t forget to claps and follow on Medium to hear more from me. Thanks for reading :)

--

--

Server Tombak
nacressoftware

Software Engineer with a relentless passion for learning and sharing knowledge. Join me on my journey to explore the ever-evolving world of technology.