Inter-communication microservices - How?

12,688

Solution 1

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.

Solution 2

Use cote, it rocks! seriously. https://github.com/dashersw/cote

in time-service.js...

const cote = require('cote');
const timeService = new cote.Responder({ name: 'Time Service' });

timeService.on('time', (req, cb) => {
    cb(new Date());
});

in client.js...

const cote = require('cote');
const client = new cote.Requester({ name: 'Client' });

client.send({ type: 'time' }, (time) => {
    console.log(time);
});

Solution 3

Thank you for this link,

My problems turn the architecture I have into a new one. For people who has the same trouble I have, there are the things I made thanks to this link :

  • Bring the Register Service and the User Service together. Why ? Because it all depends on the user information ( same database requirements ) That's the solution IlliakaillI gave.

  • Separate the management of the user's money into a "Wallet" that only exists in "Order service". Thanks to that, we don't need to retrieve user information when an order is performed, we just need to check the wallet information. ( I encoded the username in a JWT when the user performs an Authentication. Thus I can use in my wallet the username as a foreign key to recognize which wallet is being supplied, or used etc.)

As you can see, I don't use a message broker anymore because for now I don't need one. But I can split the mailing logic into a new microservice and then use a message broker to send mail through all my microservices if one needs to.

Tell me if I'm wrong, But it sounds great to me.

Share:
12,688
Crayer
Author by

Crayer

Updated on June 06, 2022

Comments

  • Crayer
    Crayer almost 2 years

    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.