Search code examples
node.jsweb-servicesrestrabbitmqmicroservices

Inter-communication microservices - How?


I'm working on a personnal project which is to transform a monolithic web application into microservices (each service has its own database).

At this moment the monolithic backend is made with NodeJS and is able to reply REST request. When I began to split the application into multiple services I faced the next problem : How to make the communication between them nicely ?

First I tried to use REST call with the next example : "Register Service" inserts interesting things into its database, then forward (HTTP POST) the user information to the "User Service" in order to persist it into the "user" database. From this example we have 2 services thus 2 databases.

I realized at this moment it wasn't a good choice. Because my "Register Service" depends on "User service". They are kind of coupled and this is an anti-pattern of the microservices conception ( from what I read about ).

The second idea was to use a message broker like RabbitMQ. "Register Service" still insert interesting things into its own database and publish a message in a queue with the user information as data. "User Service" consumes this message and persists data into its "user" database. By using this conception, both of the services are fully isolated and could be a great idea.

BUT, how about the response to send to the client ( who made the request to "Register Service"). With the first idea we could send "200, everything's ok !" or 400. It is not a problem. With the second idea, we don't know if the consumer ("User Service") persisted the user data, so what do I need to reply to the client ?

I have the same problem with the shop side of the web application. The client post the product he wants to buy to "Order Service". This one needs to check the virtual money he has into "User Service" then forward the product detail to "Deliver Service" if the user has enough money. How to do that with fully isolated services ?

I don't want to use the http request time from the client to make async request/reply on the message broker.

I hope some of you will enlighten me.


Solution

  • Tom suggested a pretty good link, where the top-voted answer with its reasoning and solution is the one you can rely on. Your specific problem may be rooted in the fact that Register Service and User Service are separate. Maybe they should not be?

    Ideally, Register service should publish "UserRegistered" event to a bus and return 200 and nothing more. It should not care (know) at all about any subscribers to that event.