Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 509
Crayer Avatar asked Jan 13 '17 14:01

Crayer


People also ask

How do microservices communicate with each other in AWS?

The HTTP/S protocol is the most popular way to implement synchronous communication between microservices. In most cases, RESTful APIs use HTTP as a transport layer. The REST architectural style relies on stateless communication, uniform interfaces, and standard methods.

Which infrastructure helps for Inter microservices communication?

What tools can I use to implement inter-service communication for microservices? Among the message brokers are RabbitMQ, Kafka, ActiveMQ, and Kestrel. Cloud providers offer their own messaging systems such as Amazon SQS , Google Cloud Pub/Sub, and Firebase Cloud Messaging ( FCM ).

How do I share data between two microservices?

A basic principle of microservices is that each service manages its own data. Two services should not share a data store. Instead, each service is responsible for its own private data store, which other services cannot access directly.


2 Answers

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.

like image 199
IlliakaillI Avatar answered Oct 13 '22 01:10

IlliakaillI


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);
});
like image 36
buycanna.io Avatar answered Oct 12 '22 23:10

buycanna.io