Gateway Aggregation: Scatter Gather Pattern
The Scatter Gather Pattern
The gateway receives one client request, fans out to multiple backend services in parallel (scatter), collects all responses, and combines them into a single response (gather). A product page request scatters to product, inventory, pricing, and reviews services. Total latency equals the slowest service plus aggregation overhead, not the sum of all service latencies.
Parallel vs Sequential Calls
Parallel calls complete in max(service latencies). Four services at 50ms each complete in ~50ms parallel vs 200ms sequential. However, some calls have dependencies: you cannot fetch user preferences until you know the user ID from authentication. The gateway must model these dependencies and parallelize only independent calls.
Partial Failure Handling
What happens when one of four services fails? Options: Fail entire request: Simple but poor user experience. Return partial data: Product page shows without reviews if reviews service is down. Use cached fallback: Return stale data with a freshness indicator. The aggregation logic must handle missing fields gracefully and indicate data completeness to clients.
500ms outlier service makes your entire aggregated response take 500ms+. Consider timeouts that return partial data rather than waiting for slow services.Response Transformation
Raw service responses rarely match client needs exactly. The gateway transforms and reshapes data: rename fields, filter unnecessary data, convert formats, calculate derived values. A mobile client might need a flattened response while a web client needs nested objects. Transformation adds CPU overhead but reduces client side processing and bandwidth.
Aggregation Complexity
Complex aggregation logic in the gateway risks creating a distributed monolith. If changing the product page requires gateway changes, you have coupled the gateway to business logic. Keep aggregation simple: fetch, combine, transform. Move complex business logic to a dedicated Backend for Frontend (BFF) service that the gateway routes to.