Designing API Gateway and BFF Patterns

1. Designing Centralized API Gateway

ConcernHandled At Gateway
RoutingPath/host → service
AuthJWT/OIDC validation
Rate limitPer consumer
ObservabilityTrace ID injection, access logs
RiskBecomes monolith / SPOF if not HA

2. Designing Backend for Frontend Pattern

BFF PerReason
WebLarger payloads, server-side render
MobileCompact payloads, battery-aware
Partner / 3rd-partyStable contract, scoped data
Owned byFrontend team for that channel

3. Designing Request Routing and Load Balancing

StrategyDetail
Path-based/api/orders → orders-svc
Host-basedapi.example.com vs admin.example.com
Header-basedX-Tenant → tenant-specific cluster
WeightedCanary / A-B
Geo-basedRoute to nearest region

4. Designing API Composition and Aggregation

Example: Aggregator BFF endpoint

// /bff/order-page/{id}
public OrderPage get(String id) {
    var futures = List.of(
        async(() -> orderSvc.get(id)),
        async(() -> userSvc.get(orderUserId)),
        async(() -> shippingSvc.track(id))
    );
    return OrderPage.from(awaitAll(futures, Duration.ofMillis(800)));
}
PatternTrade-off
Parallel fan-outFaster; more load on backends
SequentialNeeded when one depends on another
Hedged requestsLower tail latency

5. Designing Protocol Translation

TranslationUse
REST ↔ gRPCPublic REST, internal gRPC
REST ↔ GraphQLAggregated mobile API
SOAP ↔ RESTLegacy modernization
WebSocket ↔ MQReal-time over async backend

6. Designing Request Transformation

TransformUse
Header rewriteAdd x-tenant-id, drop sensitive headers
Path rewrite/v1/old → /v2/new
Body shapingStrip fields, rename
Auth enrichmentConvert JWT → user context headers

7. Designing Response Aggregation

PatternDetail
MergeCombine multiple responses into one JSON
FilterProject only required fields per channel
EnrichAdd user context, locale
Cache aggregateShort TTL; reduces backend fan-out

8. Designing Gateway Security Policies

PolicyDetail
TLS terminationTLS 1.3, HSTS
WAF rulesOWASP Core Rule Set
JWT verifyJWKS rotation; aud + iss check
IP allow/denyFor admin endpoints
Bot mgmtCloudflare, reCAPTCHA

9. Designing Edge Computing Patterns

CapabilityTech
Edge functionsCloudflare Workers, Lambda@Edge, Vercel Edge
A/B routingAt edge for low-latency variant select
Auth at edgeJWT verify, OIDC redirect
KV at edgeWorkers KV, Durable Objects

10. Designing Gateway Caching Strategy

LayerDetail
Response cacheBy URL + auth/tenant scope
Negative cacheCache 404s briefly
Cache key varyVary: Accept-Language, Authorization
InvalidationSurrogate-Key or tag-based purge

11. Designing Gateway Rate Limiting

ScopeExample
Per IP100 rps; DDoS edge
Per API keyTier quotas (free/pro)
Per routeHeavier limits on /search
BurstToken bucket: 200 burst, 50 sustained

12. Designing Gateway Observability

SignalDetail
Access logMethod, path, status, latency, ua
Trace IDInject W3C traceparent
MetricsPer-route p50/p95/p99, error rate
AuditAuth events, policy denials