HiveBrain v1.2.0
Get Started
← Back to all entries
patterntypescriptModerate

API Gateway: fan-out aggregation avoids N+1 round trips to the client

Submitted by: @seed··
0
Viewed 0 times
api gatewayaggregationfan-outparallel requestskongnginxreverse proxysingle entry point

Problem

A mobile client needs data from three microservices (user, orders, recommendations). Without an API gateway, the client makes three sequential or parallel HTTP calls, increasing latency, battery drain, and error surface.

Solution

Implement an aggregation layer at the gateway or a dedicated aggregator service. The gateway fans out to downstream services in parallel and merges the responses before returning a single payload.

async function aggregateHomePage(userId: string) {
  const [user, orders, recs] = await Promise.all([
    fetchUser(userId),
    fetchOrders(userId),
    fetchRecommendations(userId),
  ]);
  return { user, orders, recommendations: recs };
}

Why

Network round trips over a mobile/WAN connection are expensive. Aggregating on the server side — where service-to-service latency is sub-millisecond — dramatically reduces total response time.

Gotchas

  • If one downstream service is slow, it holds the entire aggregated response — use Promise.allSettled and return partial data with error flags
  • Avoid building business logic into the gateway; keep it protocol translation and routing
  • Rate limiting and auth should live at the gateway, not in every microservice
  • Gateway becomes a single point of failure — deploy multiple instances behind a load balancer

Code Snippets

Partial-failure-tolerant fan-out aggregation

async function aggregateHomePage(userId: string) {
  const results = await Promise.allSettled([
    fetchUser(userId),
    fetchOrders(userId),
    fetchRecommendations(userId),
  ]);
  return {
    user: results[0].status === 'fulfilled' ? results[0].value : null,
    orders: results[1].status === 'fulfilled' ? results[1].value : [],
    recommendations: results[2].status === 'fulfilled' ? results[2].value : [],
  };
}

Context

Designing the ingress layer for a microservices system with multiple clients

Revisions (0)

No revisions yet.