Luca Brunner·
Per-client BFFs with parallel aggregation and partial-failure handling, Kong config included
Designs API Gateway infrastructure with routing, aggregation, protocol translation, and BFF pattern optimized for diverse client types.
API Gateway & Backend-for-Frontend Design
You are an API Infrastructure Architect who designed gateway layers at Netflix and Uber. Design an API Gateway + BFF architecture.
**Client Types**: {{client_types}} (web SPA, iOS app, Android app, partner APIs, IoT devices, admin dashboard)
**Backend Services**: {{backend_services}} (list of microservices: user-service, order-service, inventory-service, etc.)
**Traffic Volume**: {{traffic_volume}} (requests per second per client type, peak traffic, geographic distribution)
**Special Requirements**: {{special_requirements}} (mobile optimization, offline support, real-time needs, caching requirements)
Design the complete gateway architecture:
1. **Gateway Selection** - Kong, AWS API Gateway, NGINX, Envoy, Zuul, Traefik, KrakenD comparison with selection matrix
2. **BFF Architecture** - One BFF per client type (web-bff, mobile-bff, partner-bff) vs shared gateway with route-based aggregation
3. **Routing Configuration** - Path-based routing (/api/v1/users → user-service), header-based routing, weighted routing for canary
4. **Request Aggregation** - BFF composes multiple backend calls into single client request, parallel fetching, partial failure handling
5. **Protocol Translation** - REST to gRPC translation, HTTP/1 to HTTP/2, WebSocket to server-sent events, GraphQL federation at gateway
6. **Authentication Termination** - JWT validation at gateway, token refresh, session management, passing identity headers to backends
7. **Rate Limiting & Quotas** - Per-client-type limits, per-endpoint limits, burst handling, different tiers per API consumer
8. **Caching Strategy** - Gateway-level response caching (TTL per endpoint), cache invalidation via events, ETag support, cache key design
9. **Request/Response Transformation** - Header injection, body transformation, content-type conversion, field filtering per client
10. **Resilience Configuration** - Circuit breaker per backend, retry policies, timeout budgets, fallback responses for non-critical data
11. **Observability** - Per-client-type latency dashboards, error rate tracking, request volume by client, cache hit ratios
12. **Mobile Optimization** - Payload compression, field filtering (only send needed fields), connection pooling, request batching, delta sync
13. **Deployment Architecture** - Gateway as Kubernetes ingress, sidecar pattern, dedicated gateway instances per BFF, scaling strategy
14. **Developer Experience** - API discovery, sandbox environment, request/response examples per client type, SDK generation
Include the actual gateway configuration (Kong declarative YAML, Envoy config, or NGINX conf) for routing and plugin setup.
Ergebnisse
API Gateway + BFF for **web SPA, iOS, and Android** over `user`/`order`/`inventory` services. One BFF per client type beats a shared gateway here — mobile and web want fundamentally different payloads.
**Topology**
```
web ─▶ web-bff ─┐
iOS ─▶ mobile-bff ─┼─▶ [user-svc] [order-svc] [inventory-svc]
Android─▶ mobile-bff ─┘ (gRPC internally)
```
**Request aggregation** — the order-detail screen is one client call, three backend calls fetched in parallel with partial-failure tolerance:
```typescript
// mobile-bff: compose, run in parallel, degrade non-critical data
app.get("/m/orders/:id", async (req, res) => {
const [order, items, user] = await Promise.allSettled([
orderSvc.get(req.params.id),
inventorySvc.itemsFor(req.params.id),
userSvc.get(req.userId),
]);
if (order.status === "rejected") return res.status(502).json({ error: "order_unavailable" });
res.json({
order: order.value,
items: items.status === "fulfilled" ? items.value : [], // optional → degrade
customer: thin(user), // mobile gets a trimmed shape
});
});
```
**Kong** fronts the BFFs for cross-cutting concerns:
```yaml
services:
- name: mobile-bff
url: http://mobile-bff:8080
routes: [{ paths: ["/m"] }]
plugins:
- name: jwt # auth terminated at the gateway
- name: rate-limiting
config: { minute: 600, policy: redis }
```
**Mobile optimization** is the whole reason for a separate BFF: field-filtered (only what the screen renders), Brotli-compressed, batched. **Resilience:** a circuit breaker per backend with fallbacks for non-critical data. **Observability:** latency and error rate are tracked per client type, which is what makes a mobile-only regression visible instead of averaged away.
Modell: Claude Opus 4
70 Likes16 SavesScore: 39
2 Kommentare
Ahmed Hassan·
Saved straight to my snippets. This is cleaner than what I shipped last sprint.
Ryan Mitchell·
Tried a variant of this and it caught a bug I'd been chasing for days.