0
0
Microservicessystem_design~7 mins

Request aggregation in Microservices - System Design Guide

Choose your learning style9 modes available
Problem Statement
When a client needs data from multiple microservices, making separate calls to each service causes high latency and complex client logic. This leads to slow responses and increased network overhead, degrading user experience.
Solution
Request aggregation collects data from multiple microservices on the server side and combines it into a single response. The client sends one request to an aggregator service, which calls the required microservices in parallel or sequence, then merges their results before replying.
Architecture
Client
Aggregator
Microservice 2

This diagram shows the client sending one request to the aggregator service, which then calls multiple microservices and combines their responses before replying to the client.

Trade-offs
✓ Pros
Reduces client complexity by centralizing multiple calls into one.
Decreases overall latency by parallelizing requests inside the aggregator.
Minimizes network overhead with fewer client-server round trips.
Allows centralized caching and response shaping.
✗ Cons
Aggregator can become a bottleneck or single point of failure.
Increases complexity in the aggregator service logic.
Harder to maintain if microservices change frequently.
Use when clients need data from multiple microservices frequently and low latency is critical, typically at scale above hundreds of requests per second.
Avoid if the system has very simple data needs or very low traffic (under 100 req/sec), where added aggregator complexity outweighs benefits.
Real World Examples
Netflix
Netflix uses request aggregation in their API gateway to combine data from multiple backend services into a single response for the client app, reducing latency and simplifying client logic.
Uber
Uber aggregates data from different microservices like driver location, pricing, and trip details into one response to provide a seamless user experience in their app.
Amazon
Amazon's frontend aggregates inventory, pricing, and recommendation microservices to present a unified product page quickly.
Code Example
The before code shows the client making two separate calls and merging data. The after code moves this logic to an aggregator service that calls microservices in parallel and returns combined data, simplifying the client.
Microservices
### Before: Client calls microservices separately
import requests

def get_user_profile(user_id):
    user = requests.get(f'http://user-service/users/{user_id}').json()
    orders = requests.get(f'http://order-service/orders?user={user_id}').json()
    return {'user': user, 'orders': orders}

### After: Client calls aggregator once
import requests

def get_user_profile(user_id):
    response = requests.get(f'http://aggregator-service/profile/{user_id}')
    return response.json()

# Aggregator service example
from flask import Flask, jsonify
import requests
import concurrent.futures

app = Flask(__name__)

@app.route('/profile/<user_id>')
def profile(user_id):
    with concurrent.futures.ThreadPoolExecutor() as executor:
        user_future = executor.submit(requests.get, f'http://user-service/users/{user_id}')
        orders_future = executor.submit(requests.get, f'http://order-service/orders?user={user_id}')
        user = user_future.result().json()
        orders = orders_future.result().json()
    return jsonify({'user': user, 'orders': orders})
OutputSuccess
Alternatives
API Gateway
API Gateway routes requests but may not aggregate responses; request aggregation combines multiple service responses into one.
Use when: Choose API Gateway when routing and authentication are primary needs without complex data merging.
Backend for Frontend (BFF)
BFF is a specialized aggregator tailored per client type (mobile, web), while request aggregation can be generic.
Use when: Choose BFF when different clients require different data shapes or aggregation logic.
Client-side aggregation
Client makes multiple calls and merges data locally, unlike server-side aggregation which centralizes it.
Use when: Choose client-side aggregation for simple apps with low latency tolerance and fewer microservices.
Summary
Request aggregation prevents high latency and complex client logic by combining multiple microservice calls into one server-side request.
It reduces network overhead and simplifies clients by centralizing data fetching and merging in an aggregator service.
This pattern is best for systems with multiple microservices and high request volumes needing low latency responses.