Resolving design dependency between microservices

Viewed 1172

I am designing an e-commerce application with microservice approach, using ORM(JPA) for data persistence for one of the microservice named OrderService. OrderService owns functionality related to persisting and reporting orders, which essentially include customer and product information. Customer and product functionality is managed by different microservices. My question is at ORM layer OrderService need POJO which belongs to ProductService and CustomerService. What is the best way to deal with this dependency between services? Should application needs to design in different way?

3 Answers

There are few things that one should take into consideration when try to find a solution 1. You cannot access the database of other services, you have to make a call. 2. You should try not to keep data from other services into yours. Data duplication lead to an inconsistent state and should be avoided if you can 3. You should have a means to query data from other services when asked for.

Now with those points, I will mostly restrict data from other services to some reference ids (which should be immutable). At ORM layer I will just fetch the reference IDs and bloat them up by making an API call to concerned services(business layer).

You may realize that you are making way too many calls for say getting customer name to customer service using customer id, if that is the case, you may consider saving some of these information in your system. But be cautioned. Data that you saved should not be volatile and make sure you have done due diligence in making that call.

Recently, I have gone through many design principles of microservices and realizes that CQRS-ES and data replication with eventual consistency is best solution of this issue. We try to make communication Asynchronous as much as possible, uses point to point synchronous communication between microservices only when necessary.

This is a fairly common situation when designing microservices. Most microservices will require access to data available through another microservices or an external provider.

The best way to deal with this is to design each microservice as a "separate" application and think of all other microservices as external to it.

So, the developer of Microservice#1 (M1) would have to check into the Microservice#2 (M2) spec and write simple POJO classes for the data he fetches from there. Just like he would do if he were using some external API like Facebook.
Do note that that M1 will always talk to M2 (via REST for example) and never to the DB directly for the data it needs.

Ideally, each microservice would have its own database (or part clone of a central database)

Related