Nowadays, building systems using microservices is almost a must, but applying and keeping microservices concepts is quite challenging. We want service providers to process all requests, even in cases of high demand, but how can we guarantee this, and how can we improve performance? The answer is message queues. So what's a message queue, and how does it work?
A message queue is an architecture that provides asynchronous communication, allowing microservices to interact with each other without coupling. The communication is made by sending messages that contain information or commands that need to be processed. The sender is called the Producer. These messages are then stored (in memory or persisted) in a queue and processed by another microservice (called the Consumer). Then, once a message is processed, it is removed or dequeued, which assures that it is processed only once.
Okay, this seems great, but what if we need the same message to be processed by two or more microservices? Well, there are different types of message queues:
1. A message queue can be point-to-point, where there is only one queue and one consumer:
Image courtesy of Oracle
2. Alternatively, a message queue can use a Publisher-Subscriber format, where a Publisher (Producer) sends a message to a queue (in this case called a Topic), and all the Subscribers receive a copy of the message that can be retained or not:
Image courtesy of Oracle
To conclude, if you are thinking of creating microservices or if you need to improve their performance and the business logic doesn't require that they be processed immediately, then message queues are what you need. They will guarantee that all received requests are processed while decoupled from other microservices, and, if needed, they allow you to easily scale for periods of high demand.