Search code examples
.netarchitecturemicroservices

How should I design my backend when data is drastically distributed in microservices?


I project with multiple microservices based on a single database with many schemas in it. There is a microservice for each database schema which does not make very much sense because some schemas only have two tables. Some microservices only do very simple CRUD operations which is not necessarily a microservice from my point of view. Example: contactDetails and address.

Now I have a requirement to implement a web application utilizing these services. It is suggested to build a API gateway using REST and forward the requests to respective microservices to fetch the data, aggregate them into a single JSON and return to the frontend. Some APIs require data from more than five microservices which is just a select statement from the database. I see this as a potential performance hit when the incoming traffic becomes high and also a communication overhead between services. I can use parallelization for few calls but some are inter dependant. Therefore, its sequential.

Following were my suggestions:

  • Directly use the database with a View/SP with cross schema and call microservices which handles shared logic and heavy operations. Because its a single database and there is no plan to have a single database for each microservice anytime soon. Best is to redesign the database if it happens.
  • Use a GraphQL gateway
  • Sync required data to a nosql/memory optimized table as the new web app is mostly a readonly app.

I would like to get some feedback on this case. I strongly believe the design is not feasible at the moment. But it doesn't make sense to make more than five internal service calls to fetch a simple dataset

Thanks!


Solution

  • For this particular scenario what you have described in your question I would recommend to directly access the database from your new service / web app.

    Whenever we are talking about microservices and data ownership it does not necessarily mean that all data access should be done via one of the endpoints of the related microservice. Ownership in this context means that all data manipulation should be done via the related microservice. In other words fetching data directly from the data source does not violate the data ownership principle.

    I've asked in the comment section whether there is any transformation logic or not. If there were any then the direct access would need to perform the same. Based on your comment here this is not the situation.


    For more information about this topic I would highly recommend to read the Monolith to Microservices book's 4th Chapter.