How to establish communication between micro services?

Viewed 65

Hi I am working on some micro services for demo. I have created two services. One is user which holds userId, name, address. Second service is Product service. This holds product id, productname, productdescription. Now I want to develop third service which is basket service. Which will take userid from userservice and productid from product service. Now in third service(Basket Service) I want to get data of user and product service. For example, User will send user id and product id to basket service. Now basket service should get user details and product details from user service and product details. Now I want to call user service and product service from basket service. I have gone through the communication between the micro services and understood few mechanisms. I followed this.

  1. Synchronous HTTP requests
  2. Asynchronous like Rabit MQ

I am just trying to understand scenarios like above which mechanism will be best suited? Can someone help me in this?

2 Answers

With an assumption that HTTP services are not always up and running, the most common way to communicate a POST/PUT request is via a message broker like for example Rabbit MQ.

Let's say you have only two services: A and B. Case 1: Synchronous HTTP requests

If B is down, and user tried to perform some post action from A to B, then the request is lost, then the end user will have to wait and retry his action, in order for it to work.

on the other hand, Case 2: Asynchronous like Rabit MQ

User creates a POST request from service A, then the A service, instead of calling B via HTTP post directly, it sends a message to the queue, and it's work is done - it can communicate a status like Accepted to the user.

Once B is up and running, it will pick up that message from the queue, process it and in some way communicate to the user that the action is indeed done: WebSocket/email etc.

Now in third service(Basket Service) I want to get data of user and product service. For example, User will send user id and product id to basket service. Now basket service should get user details and product details from user service and product details. Now I want to call user service and product service from basket service. I have gone through the communication between the micro services and understood few mechanisms.

These scenarios seems to best lend itself to HTTP or possibly asynchronous RabbitMQ with the request/reply pattern. I've worked a lot with HTTP microservices with a lot of success but have never used the request/reply pattern so I cannot tell you if it works well. In theory it would be much more efficient but it could be challenging to get it going.

There are scenarios that seem to better lend themselves to asynchronous in general:

  • The user doesn't need any information immediately after the request
  • The action is something that takes a long time
Related