vicky99
vicky99

Reputation: 49

Microservice calling multiple functions vs custom client specific function

I have a microservice which exposes some gRPC functions. Each gRPC function just gets data from a table as a single record (using id parameter) or all records. The client is a backend data management system and it needs to build a report which will require data from multiple service functions.

Now the obvious thought while using microservices is that client should make multiple service calls and combine the data at its end as per the requirement.

Pros - Client and Microservice will be independent of each other

Cons - Multiple gRPC calls (consider 5 per record * 30 records)

But somehow this doesn't feel right (maybe its monolithic architecture thinking), just to show 30 records we'll have to make 150 gRPC calls. Hence the alternative could be to create a new gRPC function which combines all the data at the service itself.

Pros - Only 1 gRPC call

Cons - Client and microservice become dependent of each other which kind of defeats the purpose of microservice.

I am more biased towards first approach but want to confirm what others think about this scenario.

Upvotes: 1

Views: 648

Answers (1)

alltej
alltej

Reputation: 7285

If I understand your statement correctly, the back-end client is aggregating data from your multiple services/functions to build a report. So I assume this is read-only since it's for reporting. Reports itself is a different bounded context or sub-domain. It has different behavior characteristics from read-write/CRUD models. This is what CQRS pattern is about - the notion that you can use a different model to update information than the model you use to read information.

So in your case, it is practical, efficient and optimal to create a different service/function that already combine data before returning it to the consuming clients. These combined data models can be straight from your data layer (via select query or stored procs).

On another note, for me even working with microservices, the main rule of distributing objects still applies which is "Do not distribute objects. If possible. (Martin Fowler, Enterprise Integration Patterns)".

Upvotes: 1

Related Questions