Rest – Should services talk directly to each other in a microservice architecture

Architecturemicroservicesrest

I have a number of web services that form a web application. Clients can access these services through REST APIs calls.

Should these services be able to talk directly to each other? If so wouldn't that make them couple which goes against the concept on microservices?

Should the client call them directly one after another to get the data it needs to load up a web page on the client?

Or should I have another layer on top of my services, that handles a request from the client, fetches the data for that request and then sends it back to the client?

Best Answer

If you want your services to look like on this picture, then yes: enter image description here It is not like you can't do it, but most of the time it will have bad consequences. Communication through REST will not decouple your services significantly. When some service interface changes, all dependent services most likely have to be changed and redeployed. Also while redeploying service, you will have to put all dependent services down, which is not considered a good design for high availability standards.

In the end, you will have microservices application which is slower than alternative monolith app would be, but having almost all the same problems!

I have to disagree with @Robert Harvey

In practice, however, one or more of your microservices (perhaps all of them) will talk to some central data store like a SQL database.

Integration database is well-known antipattern

A much better solution is to use publish-subscribe pattern in order to make communication between your services asynchronous:

enter image description here

Please take a look at CQRS/ES way of implementing this method of communication, Greg Young and other guys offer tons of nice videos on the topic. Just google for "CQRS/ES" keyword. In this approach, each service will become publisher and subscriber at the same time, allowing services to be much less coupled.

Here is a good series of articles about microservices that sheds the light on the controversy around the topic. Very detailed explanation with nice illustrations.

Related Topic