What is Message Queue? Why Message Queue? What is Message Broker? Part 1.
Background:
There was a time when monolith architecture was considered ideal for every use-case (in some cases it’s still best) but the major drawback was that the components are tightly coupled and in some scenarios two components need to talk to each other through TCP connections. Ok, no problem, we can make HTTP calls and request the data of our choice so what’s the big deal?
Let’s consider a monolith e-commerce application and there are two components Checkout and Inventory that need to communicate to each other for process completion. If the Inventory service is up and running then well & good but if the inventory service goes down, the checkout service will keep calling it and hence causing unnecessary traffic.
The Fig. 1 below is giving the visualization:
What is Message queue:
Now the message queue comes into picture. Message queue is a place where the messages (data which is to be processed by other service) will be stored in a queue structure (first come first serve) and hence managing the high traffic in an elegant way and its an excellent way to handle asynchronous operations i.e. where immediate response is not required.
What is Message broker:
The one who manages the message queue(s) is called message broker. e.g. RabbitMQ, Kafka, ActiveMQ etc.
Benefits:
- With message queue, the components become more decoupled as when inventory service goes down the messages will sit in queue for a pre-determined time and hence free the traffic load.
- Scalable as if more checkouts happen then the queue will be filled more quickly so another inventory service instances can be added to handle the load.
- Performant as the queue can sit on a different machine and hence resources can be utilized more efficiently.
Challenges:
The message queue implementation also comes with a cost because what happens if the inventory service fails to perform the desired action for a particular order(s) and hence data inconsistencies will come in place and in production systems you can’t take that lightly. Saga pattern can overcome this issue but always remember everything comes with a cost.
The next article will be about RabbitMQ and its different exchanges and their working. So stay tuned and don’t forget to follow and comment about your feedback.
References: