API Gateway
The single entry point: routing, auth, rate limiting, aggregation, and the BFF pattern.
Why a gateway exists
Without a gateway, every client must know every service's address, implement auth against each, and make dozens of calls to render one screen. An API gateway is the single entry point that hides internal topology from the outside world.
Cross-cutting concerns, handled once
The gateway centralizes work every service would otherwise duplicate:
- Routing — map /api/orders/** to the Order service.
- Authentication — validate JWTs once at the edge; forward identity headers.
- Authorization — coarse-grained policy (fine-grained stays in services).
- Rate limiting — protect the platform per user/API key/IP.
- TLS termination, caching, compression, request/response transformation.
- Response aggregation — fan out to several services, compose one response.
Backend-for-Frontend (BFF)
One gateway serving web, mobile, and partners tends to become a bloated compromise. The BFF pattern gives each client type its own thin gateway: the mobile BFF returns lean payloads and batches aggressively; the web BFF returns richer data. Each BFF is owned by the client team that consumes it.
Gateway configuration in practice
Whether you use Kong, NGINX, Spring Cloud Gateway, Envoy, or a cloud-managed gateway, the shape is similar: match rules, filters, and upstream targets.
routes:
- id: orders
uri: lb://order-service # 'lb://' = client-side load balancing via registry
predicates:
- Path=/api/orders/**
filters:
- StripPrefix=1
- name: RequestRateLimiter
args: { redis-rate-limiter.replenishRate: 20, burstCapacity: 40 }
- name: CircuitBreaker
args: { name: ordersCB, fallbackUri: forward:/fallback/orders }Keep the gateway dumb
The cardinal sin is putting business logic in the gateway — it becomes a new monolith that every team must coordinate around. Routing, security, and composition: yes. Domain decisions: never.
Key takeaways
- The gateway is the single entry point hiding internal topology from clients.
- Centralize cross-cutting concerns: auth, rate limiting, TLS, caching, aggregation.
- Use BFFs when web/mobile/partner needs diverge.
- Never put business logic in the gateway.
Knowledge check
Which concern does NOT belong in an API gateway?
What problem does the BFF pattern solve?
The mobile home screen makes 9 sequential API calls and takes 4s on 3G. What gateway capability helps most?