API Gateway
Responsibilities of an API gateway, gateway vs BFF vs service mesh, cross-cutting policies, and the anti-pattern of business logic in the gateway.
What it is
An API gateway is the single entry point for external (north-south) API traffic. It handles cross-cutting concerns so services do not each reimplement them:
- Routing and request/response transformation
- Authentication (validate JWT/OAuth tokens), coarse authorization
- Rate limiting, quotas and API keys
- TLS termination, CORS, request size limits
- Caching of safe responses
- Observability (access logs, metrics, tracing headers)
- API versioning and canary routing
API management platforms add a developer portal, API products/plans, monetization and lifecycle governance (Apigee, Azure API Management, AWS API Gateway + Developer Portal, Kong Konnect, MuleSoft).
Gateway vs BFF vs service mesh
| Component | Traffic | Owner | Purpose |
|---|---|---|---|
| API gateway | External → internal | Platform team | Cross-cutting edge policies |
| Backend for Frontend (BFF) | Specific client → services | Client/product team | Aggregate and shape data for one UI |
| Service mesh | Service → service (east-west) | Platform team | mTLS, retries, traffic shifting, telemetry |
flowchart LR
W[Web app] --> GW[API gateway<br/>authN, rate limits, WAF]
M[Mobile app] --> GW
P[Partners] --> GW
GW --> BW[Web BFF]
GW --> BM[Mobile BFF]
GW --> PA[Partner API]
BW --> S1[Catalog svc]
BW --> S2[Order svc]
BM --> S1
BM --> S2
PA --> S2
subgraph Mesh[Service mesh: mTLS, retries]
S1
S2
endReal-world example
A bank exposes open-banking APIs to fintech partners through Apigee/Azure API Management with OAuth 2.0 client credentials plus mTLS, per-partner quotas, and a developer portal. Internal mobile and web apps use BFFs behind the same edge, each owned by the respective product team.
Advantages and disadvantages
| Advantages | Disadvantages |
|---|---|
| Consistent security and policies | Extra hop and latency |
| Decouples clients from service topology | Can become a bottleneck or monolithic choke point |
| Central analytics and API catalog | Teams blocked by a central gateway team if governance is heavy |
Technology options
AWS API Gateway (REST/HTTP/WebSocket), Azure API Management, Google Apigee and API Gateway, Kong, Tyk, Envoy Gateway, NGINX, Spring Cloud Gateway, Kubernetes Gateway API implementations.
Architect-level considerations
[!WARNING] Anti-pattern Putting business logic, orchestration or data aggregation for many clients in the shared gateway recreates an enterprise service bus. Keep the gateway thin; put client-specific composition in BFFs.
[!EA] Use the gateway/APIM catalog as the API inventory for governance (OWASP API9: improper inventory management) and enforce standard policies as code.
Interview questions and answers
Should the gateway do authorization?
Coarse checks (valid token, scope present, tenant active) yes. Fine-grained, object-level authorization must be enforced in the service that owns the data, because only it knows whether user X may read order Y.
How do you avoid the gateway becoming a single point of failure?
Use a managed, multi-AZ (and for global products, multi-region) gateway, autoscaling, no stateful logic, health-checked deployments, and capacity tests. Keep configuration in Git with staged rollout.
When do you need a BFF?
When different clients need different data shapes, aggregation or protocols, and you want the client team to evolve their API without changing core services.