RabbitMQ is an open-source message broker software that is widely used for message queuing, asynchronous communication, and distributed systems. It provides a way for different software components or systems to communicate and exchange data in a decoupled and scalable manner.
The exchange receives all the messages and then distributes them based on their destinations. An exchange can be linked to multiple queues, and these queues are connected to consuming services or consumers. In our case, there are two consuming services, Inventory and Shipping, that need to receive messages from the checkout service.
So, when the checkout service wants to send a message, it sends it to the exchange. The exchange is connected to queues through connections known as bindings, and these bindings are identified by binding keys. Subsequently, our consuming services or applications subscribe to these queues.
One of the notable advantages of this message model is the flexibility it offers in terms of how messages can flow through the system. This flexibility is largely attributed to the various types of exchanges available.
Message Flow in Rabbit MQ
The producer initiates the message’s publication, sending it to an exchange. It’s essential to define the exchange type during its creation.
The exchange, upon receiving the message, assumes responsibility for message routing. The exchange considers various message metadata factors, including the routing key, which is contingent on the exchange type.
Bindings are established from the exchange to specific queues. In this scenario, two bindings connect the exchange to two distinct queues. The exchange directs the message into the appropriate queues based on message metadata.
Messages persist in the queue until they are taken up for processing by a consumer.
Subsequently, the consumer engages in message processing.
Types of exchanges
Fanout Exchange
When using a Fanout exchange, the checkout service produces a message, and the exchange replicates the message, forwarding it to every single queue it’s aware of.
Direct Exchange
In the case of a direct exchange, the checkout service generates a message, which is then assigned a routing key. In this setup, the routing key is compared to the binding key, and if there is an exact match, the message is routed through the system accordingly.
Topic Exchange
With a topic exchange, it’s possible to perform partial matches between the routing key and the binding key, allowing for more flexible message routing.
Header Exchange
In a header exchange, the routing key is entirely disregarded, and message routing is determined based on the message header attributes. This approach leverages message header attributes for the routing process.
Default Exchange
It’s important to note that the default exchange is specific to RabbitMQ and is not part of the AMQP message model. It’s sometimes referred to as a nameless exchange.
Advantages of Rabbit MQ
- Delivery acknowledgments and confirmations enhance message queue reliability, effectively reducing the risk of message loss.
- Its adaptable routing capabilities enable the precise delivery of messages to designated queues and specific consumers.
- The support for various exchange types in messaging systems offers diverse methods for routing messages to consumers.
- Its lightweight nature allows for effortless deployment on enterprise web servers and in public cloud environments.
Disadvantages of Rabbit MQ
- Scalability: Although RabbitMQ can manage substantial message loads, it may face challenges when the message volume increases rapidly.
- Complexity: Setting up and administering RabbitMQ can be intricate, particularly in extensive deployments.
- Latency: Due to the storage of messages in memory, processing a large volume of messages can introduce some latency.