Home > database >  Should API delegate all work to other services?
Should API delegate all work to other services?

Time:05-04

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:

  1. Be performed by the API itself?
  2. Delegated to a separate microservice that handles database queries?
  3. 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:

  1. This service is the only application which has direct access to its own data
  2. 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...

  • Related