API gateway
Amazon API Gateway
One front door for many services: routing, auth, rate limits, request validation.
In a monolith
The app’s router + middleware
In a monolith the “gateway” is your framework’s router and middleware chain (auth, CORS, rate limiting) inside the app.
In a distributed system
Shared edge for every service
Clients call one API; the gateway authenticates, throttles, validates and routes to the right service — so fifteen services don’t each reimplement auth. It can also aggregate responses (BFF).
On AWS
Amazon API Gateway
REST, HTTP and WebSocket APIs with authorizers (Cognito/JWT/Lambda), throttling, usage plans, request validation, and direct integrations with Lambda, SQS, DynamoDB and Step Functions.
- API Gateway HTTP API$Cheaper, lower latency, JWT authorizers, fewer features
- API Gateway REST API$$Usage plans/API keys, request validation, caching, WAF
- ALB + service mesh$$Common for internal traffic; sidecars handle mTLS and retries
- Reach for it when
- Several services or several client types, and cross-cutting rules (auth, throttling) are being duplicated.
- What it costs you
- Another hop (a few ms) and another bill
- A central chokepoint — its config can bottleneck every team’s deploys
- Temptation to put business logic in the gateway
- When it dies
- If the gateway is down, every API is down. Managed gateways are multi-AZ; self-hosted ones need several instances behind an LB.
Interview follow-ups
API gateway vs load balancer?
An LB spreads traffic across copies of one service. A gateway is an API-aware front door across many services: auth, rate limits, quotas, transformations, routing.
What is a BFF?
Backend-for-frontend: a gateway layer per client type (web, mobile) that shapes responses for that client, so the mobile app doesn’t need six calls.
Where should rate limiting live?
At the edge (gateway/WAF) for coarse protection, with shared counters (e.g. Redis) when there are many gateway instances; per-user business quotas may live in the service.
Connection rules
It talks to
- Load balancer
- App server
- Service
- Realtime server
- Identity provider
- WorkflowOnly when: API Gateway can start a Step Functions execution directly (async APIs).
- NoSQL storeOnly when: API Gateway → DynamoDB integration for trivial reads/writes with request mapping and no business logic.
- QueueOnly when: AWS API Gateway service integrations (e.g. POST /events → SQS, Kinesis or EventBridge) for ingestion and webhooks — fine when there’s no business logic beyond request mapping.
- Pub/sub topicOnly when: AWS API Gateway service integrations (e.g. POST /events → SQS, Kinesis or EventBridge) for ingestion and webhooks — fine when there’s no business logic beyond request mapping.
- Event streamOnly when: AWS API Gateway service integrations (e.g. POST /events → SQS, Kinesis or EventBridge) for ingestion and webhooks — fine when there’s no business logic beyond request mapping.
Gets traffic from
- Clients
- DNS
- CDN
- Third-party API
- Load balancerOnly when: Self-hosted gateways (Kong, Envoy) run as a fleet behind an LB.
- App serverOnly when: Calling another team’s public API or a partner API that only exists behind a gateway.
- ServiceOnly when: Calling another team’s public API or a partner API that only exists behind a gateway.
Never
- API gateway → WorkerWorkers pull from queues; they’re not HTTP targets.
- API gateway → SQL databaseA gateway routes to compute; data access needs your code.
- API gateway → Read replicaA gateway routes to compute; data access needs your code.
- API gateway → CacheA gateway routes to compute; data access needs your code.
- API gateway → Object storageA gateway routes to compute; data access needs your code.
- API gateway → SearchA gateway routes to compute; data access needs your code.
- API gateway → DB proxyA gateway routes to compute; data access needs your code.