I am working on microservice architecture and I want to aggregate data from two microservices.
For example, Frontend calls the API Gateway and API Gateway calls two microservices Customer and Order microservices. Customer microservice returns customer details and Order microservice returns all ordered products by customer.
This is the format returned by API Gateway after aggregation from two microservice using Ocelot or Azure API Management.
Format 1
{
"Customers":[
{
"customerId":1001,
"customerName":"Tom"
},
{
"customerId":1002,
"customerName":"Jerry"
}
],
"Orders":[
{
"CustomerId":1001,
"Orders":[
{
"ProductId":"PRO1",
"ProductName":"Books"
},
{
"ProductId":"PRO2",
"ProductName":"Pens"
}
]
},
{
"CustomerId":1002,
"Orders":[
{
"ProductId":"PRO3",
"ProductName":"Pencils"
},
{
"ProductId":"PRO4",
"ProductName":"Toys"
}
]
}
]
}
The Format that I want is format 2.
Format 2
{
"OrderDetails":[
{
"customerId":1001,
"customerName":"Tom",
"Orders":[
{
"ProductId":"PRO1",
"ProductName":"Books"
},
{
"ProductId":"PRO2",
"ProductName":"Pens"
}
]
},
{
"customerId":1002,
"customerName":"Jerry",
"Orders":[
{
"ProductId":"PRO3",
"ProductName":"Pencils"
},
{
"ProductId":"PRO4",
"ProductName":"Toys"
}
]
}
]
}
The second format is achievable using Ocelot but the merging of data is based on the ids on Gateway and it requires some processing.
Is it a good practice to aggregate data on Gateway using business logic. If not what practices should be followed for this kind of aggregation?
It would be nice if you can provide some references for achieving this aggregation using the Azure API Management.
Use a gateway to aggregate multiple individual requests into a single request. This pattern is useful when a client must make multiple calls to different backend systems to perform an operation.
In IT industry, aggregator refers to a website or program that collects related items of data and displays them. So, in microservices the Aggregator Design Pattern is a service that receives a request, then makes requests of multiple services, combines the results and responds to the initiating request.
An API gateway often does API Composition, which enables a client such as mobile device to efficiently retrieve data using a single API request. The FTGO API gateway provides a coarse-grained API that enables mobile clients to retrieve the data they need with a single request.
This is known as API composition or Backend for Frontend. I would say it is fine to aggregate data using the API Gateway because it allows your API clients to use a simpler API interface. The actual mapping would be done in the API Gateway.
However, when you chain multiple microservices together in one Web API, the failure of one service will cause the entire Web API to fail. An alternative (and more complex) solution is to create a dedicated microservice that aggregates the datasets from the other microservices and expose a single API that shows the joined data. See - CQRS pattern.
A reference for how you might implement this in Azure is API Aggregation Using Azure API Management.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With