RabbitMQ: why publish to a queue instead of calling another service directly
Today I was studying what RabbitMQ is and why it is used for communication between systems.
I wanted to understand why an application would send a message to a queue instead of calling another service directly through an API.
At first I thought that, if a system needs to tell another system about something, it would be enough to make an HTTP request directly. For example, when an order was created, the API could call another service to send an email or process a payment.
I found it interesting to realize that RabbitMQ helps decouple parts of the system. Instead of one service depending on the other to answer right away, it can publish a message to a queue, and another service can consume that message later.
In an order system, when an order is created, the application could publish a message like this:
{
"event": "order_created",
"order_id": 123,
"customer_email": "cliente@email.com"
}
Then a separate service could consume that message to send a confirmation email to the customer. In that scenario, creating the order does not have to stay blocked waiting for the email to be sent.
My initial impression is that RabbitMQ makes sense when I want to separate responsibilities and make the system more resilient. I am still starting to study it, but I could already see that queues help a lot when an action does not need to happen exactly in the main flow of the request.