Implementing API Gateway

1. Routing Requests

Routing ByExample
Path/orders/* → order-service
Hostapi.shop.com → shop-stack
HeaderX-Tenant: acme → tenant cluster
MethodGET → cache-tier; POST → app-tier
Weighted90% v1 / 10% v2 (canary)

2. Implementing Request Aggregation

PatternDetail
Scatter-gatherOne client call → fan-out to N services → merge
BFF aggregationPer-client tailored composite endpoint
GraphQL gatewayFederation handles aggregation declaratively
CaveatBound concurrent fan-out; respect deadlines

3. Handling Protocol Translation

ExternalInternal
REST/JSONgRPC
GraphQLREST + gRPC
WebSocket / SSEKafka / NATS subscribe
SOAP LEGACYREST

4. Implementing Rate Limiting

ScopeKey
Per-IPX-Forwarded-For
Per-userJWT subject
Per-API keyHeader
GlobalProtect upstream
AlgorithmToken bucket / sliding window

5. Managing Authentication

StepDetail
Verify tokenJWT signature, expiry, issuer
Strip / re-issueReplace external JWT with internal one
Inject claimsX-User-ID, X-Roles
mTLS upstreamEncrypt and authenticate to services

6. Implementing Request/Response Transformation

TransformationExample
Add headerInject correlation ID
Rewrite path/v1/users/users
Body transformJSON → XML, snake_case ↔ camelCase
Filter fieldsStrip internal fields from responses

7. Handling API Versioning

StrategyRouting Rule
Path/v1/* → v1 cluster
HeaderAccept: application/vnd.shop.v2+json
Subdomainv2.api.shop.com
DefaultPin to latest stable; deprecate old via headers

8. Implementing Response Caching

LayerTTL
Edge cacheSeconds–minutes for hot reads
Honor headersCache-Control, ETag, Vary
BypassCookies, auth, mutating methods
InvalidationOn write events; surrogate keys (Fastly/Varnish)

9. Using Circuit Breakers

SettingDetail
Error threshold50% over rolling window
Open duration30s; then half-open probe
FallbackCached response or 503
Per-routeDon't trip whole gateway

10. Implementing CORS Handling

HeaderUse
Access-Control-Allow-OriginAllowed origins (no * with credentials)
Access-Control-Allow-MethodsGET, POST, PUT, DELETE, PATCH
Access-Control-Allow-HeadersAuthorization, Content-Type, ...
Access-Control-Allow-Credentialstrue for cookies
Access-Control-Max-AgePreflight cache (e.g. 86400)

11. Managing API Documentation

ToolDetail
OpenAPI 3.1JSON/YAML spec; aggregate per gateway
Swagger UI / RedocInteractive docs
AsyncAPIDocument async/event APIs
BackstageCatalog of all service APIs
SpectralLint OpenAPI for consistency

12. Implementing Backend for Frontends

BFFTailored For
web-bffBrowser; aggregates many services
mobile-bffSmaller payloads, offline support
partner-bffExternal integrations, stricter auth
Owned byFrontend team (loose coupling to backend)
[Web]   → [web-bff]   ─┐
[Mobile]→ [mobile-bff]─┼─► [orders] [users] [payments]
[B2B]   → [partner-bff]┘