Querying data from multiple microservices

Viewed 355

I'm trying to design an application that uses the microservices architecture. The service would have an api gateway, a user service and then a seperate service for each analytic source.

For example, lets say I had twitter analytics data and instagram analytics data. There would be a seperate microservice for twitter analytics data and instagram analytics data. These microservices would handle oauth for that service, storing data and pulling data. My question is this: how would I request data from one service, lets say the user service, and then based on data from the user service, make a request to the instagram service and twitter service to request analytics info for that user.

However what would happen if I had n amount of services that I needed to request to? Like let's say a user needs analytics data from facebook, instagram, twitter and tiktok. How would I handle making a seperate request to each and then combine into a single response for the client?

I was thinking of using an api gateway as I've read that you can support request fanning-out to multiple microservices. I'm exactly sure how to do that however I found this article from aws outlining the different strategies.

I've also read over this question that says "You should implement endpoints in the services to facilitate these two requests. If you're making n requests, you've done it wrong". So now I'm not even sure if I'm on the right track.

Am I approaching this the correct way?

1 Answers

I think the solution to the above problem will be the usage of CQRS design pattern.

The idea will be a single service that listens to the events in Twitter analytics and Instagram analytics and save those details in a database. Once the data is stored we can hit this service to get the required data.

Eventually we are introducing an intermediatory service which will listen to the events in those analytics services and save them to a database. Now we can avoid multiple api calls by making a single api call to this particular service which will give you the required details.

I think this might be of some help !!.

References : https://microservices.io/patterns/data/cqrs.html#:~:text=How%20to%20implement%20a%20query%20that%20retrieves%20data,published%20by%20the%20service%20that%20own%20the%20data. https://medium.com/design-microservices-architecture-with-patterns/cqrs-design-pattern-in-microservices-architectures-5d41e359768c

Related