Elias
Elias

Reputation: 937

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?

Upvotes: 60

Views: 36175

Answers (4)

user6665788
user6665788

Reputation: 21

Rabbit MQ provides asynchronous communication. when we need to publish lots of events at same time it is very difficult to store in DB at same time. When event got published, it will send to MQ. From MQ it will be consumed by multiple consumers, you can also store data in MQ based on configuration for few days. Rabbit MQ is best for event-based approach at microservice level. Ex: order creation and cancellation.

Upvotes: 2

Lovisa Johansson
Lovisa Johansson

Reputation: 10568

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).

Upvotes: 52

Simon
Simon

Reputation: 140

The same question can be why REST is necessary for microservices? Microservice concept is not something new under moon. A long time distribution of workflow was used for backend engineering and asynchronous request processing, Microservice is the same component in a separated jvm which matches with S(single responsibility) in SOLID. What makes it micro SERVICE - is that it is balanced. And that is the all! Particularly (!), it can be REST Service on Spring Cloud/REST base, which is registered by Eureka, has proxy gateway and load balancing over Zuul and Ribbon. But it is not the whole world of microservices!By the way, asynchronous distributed processing is one of tasks which microservices are used for. Long time ago services(components) in separated JVM was integrated over any messaging and the pattern is known as ESB. Microservices are the same subjects the pattern. Due to fashion for Spring Cloud REST seems like it is the only way of microservices. Nope! Message based asynchronous microservice architecture is supported by Vertx https://dzone.com/articles/asynchronous-microservices-with-vertx, for example. Why not to use RabbitMQ as message channel? In this case load balancing can be provided by building RabbitMQ cluster. For example:https://codeburst.io/using-rabbitmq-for-microservices-communication-on-docker-a43840401819. So, world is much wide more.

Upvotes: 0

Dimitar Tsonev
Dimitar Tsonev

Reputation: 3892

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

Upvotes: 82

Related Questions