Suppose we have an API server with several endpoints that serves user requests. I have been wondering what is a good trade-off between implementing logic in the API server vs. delegating to other microservices.
For example, suppose we want to fetch data from a database upon an API call. Should database query:
- Be performed by the API itself?
- Delegated to a separate microservice that handles database queries?
- Delegated to something even simpler than a microservice, say a lambda function in the cloud?
Thanks for help.
CodePudding user response:
In case of Microservices the service itself is the owner of its own data. This implies two things:
- This service is the only application which has direct access to its own data
- If ServiceB wants perform any operation on this data it has to do that via the ServiceA's API (not directly via the database)
If ServiceB needs to retrieve data frequently and the data is fairly static then the ServiceB could implement a local cache via replication. But the source of truth still remains at ServiceA.
CodePudding user response:
Afaik. microservices split up the problem horizontally, and what you are talking about is splitting up the problem vertically. Ofc. it is possible to combine both approaches and having multiple microservices each or some of them split up to smaller services in different layers. This can be both an architectural and a scaling decision, so better to check the numbers what kind of load you expect, what response time you need, and how much you want to spend on it imho. So better not to solve problems which are non-existent atm. but maybe in the future you will have them, or not...