Implementing Request Aggregation
1. Configuring Multi-Backend Requests
Client → Gateway → ┬→ /users/42 (user-svc)
├→ /orders?u=42 (order-svc)
└→ /prefs/42 (prefs-svc)
Aggregate → Single JSON response
2. Using Parallel Request Execution
Example: Parallel fetch (Java)
CompletableFuture<User> u = async(() -> userClient.get(id));
CompletableFuture<Orders> o = async(() -> orderClient.byUser(id));
CompletableFuture<Prefs> p = async(() -> prefsClient.get(id));
CompletableFuture.allOf(u, o, p)
.orTimeout(2, TimeUnit.SECONDS)
.join();
return new Profile(u.join(), o.join(), p.join());
| Property | Value |
|---|---|
| Latency | max(call_i), not sum |
| Failure mode | All-or-partial |
| Cancellation | Cancel in-flight on timeout |
3. Implementing Sequential Aggregation
| When | Reason |
|---|---|
| Result of A feeds B | Dependency |
| Auth → resource | Need user from token first |
| ID resolution → details | Two-phase fetch |
4. Setting Aggregation Timeout
| Strategy | Detail |
|---|---|
| Overall budget | 2s total wall-clock |
| Per-call deadline | Decremented from budget |
| Hedged requests | Send duplicate after p99 |
| Soft timeout | Return partial after T |
5. Implementing Response Merging
| Pattern | Example |
|---|---|
| Embed | {user: {...}, orders: [...]} |
| Join by key | Merge on shared ID |
| Hypermedia _embedded | HAL/JSON:API |
| Side-by-side | Parallel arrays |
6. Configuring Partial Failure Handling
Example: Partial result with errors[]
{
"data": {
"user": { "id": 42, "name": "Ana" },
"orders": null
},
"errors": [
{ "source": "order-svc", "code": "timeout", "message": "504 after 2s" }
],
"_meta": { "partial": true }
}
7. Using GraphQL-Style Aggregation
| Feature | Benefit |
|---|---|
| Single round trip | Client requests exact fields |
| DataLoader | Batches N+1 calls |
| Schema stitching | Merge multiple backends |
| Federation | Independent subgraphs |
8. Implementing Batch Requests
Example: Batch endpoint
POST /v1/_batch
[
{ "method": "GET", "path": "/users/1" },
{ "method": "GET", "path": "/users/2" },
{ "method": "POST", "path": "/audit", "body": {"event":"viewed"} }
]
→
[
{ "status": 200, "body": {...} },
{ "status": 404, "body": {"error":"not_found"} },
{ "status": 201, "body": {"id":"a1"} }
]
9. Configuring Response Ordering
| Order | Use |
|---|---|
| Input order | Match request array |
| By dependency | Resolve dependencies first |
| By priority | Critical first |
| Streamed | Emit as ready (NDJSON/SSE) |
10. Using Conditional Aggregation
| Trigger | Effect |
|---|---|
| Field requested | Skip backend if not asked |
| Permission scope | Skip PII calls if no scope |
| Feature flag | Include only if enabled |
| Cache hit | Skip upstream entirely |