Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microservices Why Use RabbitMQ?

I haven't found an existing post asking this but apologize if I missed it.

I'm trying to get my head round microservices and have come across articles where RabbitMQ is used. I'm confused why RabbitMQ is needed. Is the intention that the services will use a web api to communicate with the outside world and RabbitMQ to communicate with each other?

like image 454
Elias Avatar asked Jul 20 '17 07:07

Elias


People also ask

Why we need RabbitMQ in microservices?

RabbitMQ is a popular open-source message broker that is extensively used in building large-scale distributed systems. It gives your application a common platform to send and receive messages, and your letters are safe to live until received.

Why would I use RabbitMQ?

RabbitMQ is therefore ideal for long-running tasks or blocking tasks, allowing web servers to respond quickly to requests instead of being forced to perform computationally intensive tasks on the spot. RabbitMQ simply stores messages and passes them to consumers when ready.

Why message broker is required for microservices?

When using asynchronous communication for Microservices, it is common to use a message broker. A broker ensures communication between different microservices is reliable and stable, that the messages are managed and monitored within the system and that messages don't get lost.

Which message queue is best for microservices?

If you're in love with Redis and can't handle adding a new technology to your stack then go with Redis. If you're not maintaining your own infrastructure and you're heavily dependent on cloud services already then go with a cloud service for the message queue solution as well.


2 Answers

In Microservices architecture you have two ways to communicate between the microservices:

  • Synchronous - that is, each service calls directly the other microservice , which results in dependency between the services
  • Asynchronous - you have some central hub (or message queue) where you place all requests between the microservices and the corresponding service takes the request, process it and return the result to the caller. This is what RabbitMQ (or any other message queue - MSMQ and Apache Kafka are good alternatives) is used for. In this case all microservices know only about the existance of the hub.

microservices.io has some very nice articles about using microservices

like image 169
Dimitar Tsonev Avatar answered Oct 02 '22 13:10

Dimitar Tsonev


A message queue provide an asynchronous communications protocol - You have the option to send a message from one service to another without having to know if another service is able to handle it immediately or not. Messages can wait until the responsible service is ready. A service publishing a message does not need know anything about the inner workings of the services that will process that message. This way of handling messages decouple the producer from the consumer.

A message queue will keep the processes in your application separated and independent of each other; this way of handling messages could create a system that is easy to maintain and easy to scale.

Simply put, two obvious cases can be used as examples of when message queues really shine:

  1. For long-running processes and background jobs
  2. As the middleman in between microservices

For long-running processes and background jobs:

When requests take a significant amount of time, it is the perfect scenario to incorporate a message queue.

Imagine a web service that handles multiple requests per second and cannot under any circumstances lose one. Plus the requests are handled through time-consuming processes, but the system cannot afford to be bogged down. Some real-life examples could include:

  • Images Scaling
  • Sending large/many emails (like newsletters)
  • Search engine indexing
  • File scanning
  • Video encoding
  • Delivering notifications
  • PDF processing
  • Calculations

The middleman in between microservices:

For communication and integration within and between applications, i.e. as the middleman between microservices, a message queue is also useful. Think of a system that needs to notify another part of the system to start to work on a task or when there are a lot of requests coming in at the same time, as in the following scenarios:

  • Order handling (Order placed, update order status, send an order, payment, etc.)
  • Food delivery service (Place an order, prepare an order, deliver food)
  • Any web service that needs to handle multiple requests

Here is a story explaining how Parkster (a digital parking service) are breaking down their system into multiple microservices by using RabbitMQ.

This guide follow a scenario where a web application allows users to upload information to a web site. The site will handle this information and generate a PDF and email it back to the user. Handling the information, generating the PDF and sending the email will in this example case take several seconds and that is one of the reasons of why a message queue will be used.

Here is a story about how and why CloudAMQP used message queues and RabbitMQ between microservices.

Here is a story about the usage of RabbitMQ in an event-based microservices architecture to support 100 million users a month.

And finally a link to Kontena, about why they chose RabbitMQ for their microservice architecture: "Because we needed a stable, manageable and highly-available solution for messaging.".

Please note that I work for the company behind CloudAMQP (hosting provider of RabbitMQ).

like image 44
Lovisa Johansson Avatar answered Oct 02 '22 11:10

Lovisa Johansson