Aggregation of data on API Gateway
Asked Answered
B

1

19

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.

Balbriggan answered 10/10, 2019 at 6:18 Comment(0)
F
14

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.

Fredette answered 10/10, 2019 at 8:35 Comment(2)
Format 1 is achievable thorough the API Aggregation using Azure API Management but not the Format 2 can you give a reference that supports Format 2. I have searched for this but could not get it.Balbriggan
@Balbriggan did you ever find a solution for format 2? I have a similar situation, and till now I'm thinking the best way to approach this is to implement this logic on an actual microservice, not on a BFF. You would then have a BFF endpoint which maps directly to this internal endpoint. For example, if we want to optimize the database queries to get format 2, there's no way you can do that through API gateway aggregation. I'm still brainstorming at this point though; would appreciate your feedback.Gula

© 2022 - 2024 — McMap. All rights reserved.