Implementing API Gateway Patterns

1. API Gateway Pattern

ResponsibilityDetail
Single Entry PointClients hit gateway; gateway routes to services
Cross-CuttingAuthn/z, rate limiting, logging, CORS, TLS termination
Protocol BridgingHTTPS in → gRPC out, REST in → events out
ToolsKong, Apigee, AWS API Gateway, Istio, Spring Cloud Gateway, Envoy

2. Backends for Frontends Pattern

AspectDetail
One BFF per UIWeb BFF, iOS BFF, Android BFF
Owned byFrontend team that consumes it
PurposeTailor API shape to client needs (avoid over/under-fetching)
AvoidsOne-size-fits-all gateway becoming bottleneck

BFF Topology

[Web]    → [Web BFF]    ─┐
[iOS]    → [iOS BFF]    ─┼→ [Order Svc] [Catalog Svc] [User Svc]
[Android]→ [Android BFF]─┘
        

3. Gateway Routing Pattern

Routing TypeExample
Path-based/orders/* → order-service
Host-basedapi.shop.com vs internal.shop.com
Header-basedX-Tenant: acme → acme cluster
Weighted90% v1, 10% v2 (canary)
GeoRoute to nearest region

4. Gateway Aggregation Pattern

AspectDetail
DefinitionGateway calls multiple services and aggregates response
ReducesClient round-trips, especially on mobile
PatternParallel calls; merge results; return composite
CaveatRisk of becoming god-gateway; prefer BFF for complex aggregation

Example: Aggregated Order Detail

CompletableFuture<Order>     o = supplyAsync(() -> orderSvc.get(id));
CompletableFuture<Customer>  c = supplyAsync(() -> customerSvc.get(custId));
CompletableFuture<Shipment>  s = supplyAsync(() -> shippingSvc.get(id));
return CompletableFuture.allOf(o, c, s)
    .thenApply(v -> new OrderDetail(o.join(), c.join(), s.join()));

5. Gateway Offloading Pattern

Offloaded ConcernWhy at Gateway
TLS TerminationCentralizes cert management
AuthenticationValidates JWT once; passes claims downstream
Rate LimitingProtects all services uniformly
Compression / CachingReuse across services
Request/Response LoggingCentralized audit

6. Micro Gateway Pattern

AspectDetail
DefinitionSmall, dedicated gateway per service or service group
AvoidsMonolithic gateway bottleneck
DeployedCo-located with service or as sidecar
Use WithService mesh; per-team ownership

7. GraphQL Gateway Pattern

AspectDetail
SchemaUnified schema (federation/stitching)
ResolversEach field resolved by owning service
Client BenefitSingle round-trip; ask exactly what you need
ToolsApollo Federation, GraphQL Mesh, Hot Chocolate
WatchoutN+1 resolver problem → use DataLoader

8. API Gateway with Circuit Breaker

BehaviorDetail
Per-Route BreakerTrip independently per upstream
FallbackCached response, default value, fail-fast 503
MetricsTrip counts exposed for alerting
Built-InSpring Cloud Gateway + Resilience4j; Istio outlier detection

9. Edge Service Pattern

AspectDetail
DefinitionFirst service hit at network edge; combines gateway + business logic
CapabilitiesAuthn, routing, request shaping, A/B testing, header injection
ExamplesNetflix Zuul, Cloudflare Workers, AWS Lambda@Edge

10. Gateway Rate Limiting Pattern

Limit ScopeUse Case
Per-API KeyTier-based (Free: 100/min, Pro: 10k/min)
Per-IPAnti-abuse
Per-UserFairness across tenants
Per-EndpointProtect expensive ops
GlobalBackstop for total throughput