September 22, 2019

Tools to Micro-service (Part 4: RabbitMQ)

Tools to Micro-service (Part 4: RabbitMQ)

RabbitMQ  is a message broker. Basically, it allows your services to communicate asynchronously with each others. The principle is one service will publish a message to RabbitMQ which should correspond to a request or an event. And on the other end, one or multiple services (called consumers) will listen to these messages and "react" (whether by executing a task, saving something in db or anything really)

RabbitMQ Publish Subscribe Pattern

You don't "require" RabbitMQ to run a microservice architecture but it brings a loads of benefits.

  • Better performance: On a large scale, async communications provide better performances and use ressources more effectively than synchronous communications. Why should you services hang until receiving a response that in case of a crash may never come
  • Better system recovery: You can configure rabbitMQ in such a way to confirm the execution of a task before disgarding the message. This way rabbitMQ will save messages that don't get treated successfuly and replay them later on, so system continue running as usual
  • Decoupling between services: Your services don't know and should not care how the messages that they publish get treated. You can design a system in an event driven style where services react to messages, do their thing and exit. An example can be : "user login" creates a message in rabbitMQ, service A logs the event in datatbase, service B loads user's data to rabbitMQ and so on.
  • More control: You can configure your services to treat X requests each second so you prevent overwhelming your servers
  • Debugging: RabbitMQ actually comes with a good manager interface that allows you to identify performances bottle necks and failing services.
  • Service Discovery: Actually it's not that rabbitMQ provides a solution for service discovery, but it eliminates the problem all together. With services not communicating directly services don't need to know the adresses and ports of each other
  • System flexibility: Services can join and exit the system dynamicly without any downtime.