Search code examples
architecturemicroservices

Microservices data retrieve


good day i read some books about microservices architecture, but i still have question.. One of them is about situation when you need to retrieve data about some entities, which connected with other... for example: we have order and user microservices, for example each order have some information about user and customer wants to retrieve user orders so i see three ways to achieve this:

  1. Client app makes request to orders microserve and after makes n request to users microservice to retrieve user info the order
  2. Client app makes request to orders mircoservice which make inter-request to users microservices
  3. Orders microservice db store necessary information about user

For first case - it is complex for client app to construct and aggregate data together from two sources (orders and users)

For second case - if we have more than two microservices then total request time will grow

For third case connected with data consistent problem (user changed data, but order service db does not update yet)

which case is most used?

and small question #2 - in case of microservice and web api application - each microservice contains only one or maybe two controllers?


Solution

  • To answer to your all three cases, it will purely depend on microservices architecture.

    First case -> According to this case, your client have to make consolidated response by making call to different microservices. This is the overhead for client which is bad architectural approach.

    Second case -> This is good approach compared to first one. If you have independent database/tables (without any direct relation) then this would be good approach. Just you have to store references for orderId/userId in respective tables to get these details. Your total request time will group but your user module will work independently of order modules. With this approach you will achieve loose coupling.

    Third case -> If you don't have different databases for each microservices then this would be your best approach as it will reduce number of calls to database as well as to other microservice. You can get direct details by implementing service methods for each required model.

    which case is most used?

    Ans. I don't think any one uses first approach as its not good practice. If you have different databases for different microservices then second case would be your answer. If you have single database then you go ahead with third case as well. But your service layer code will be duplicated over different microservices.

    In case of microservice and web api application - each microservice contains only one or maybe two controllers?

    There is no standard for number of controllers in each microservice. It will depend on size of microservice and responsibilities performed by that microservice.