Key technologies
API gateway
The box every design draws between the client and the services. It earns its place by owning the cross-cutting concerns once, and loses it the moment business logic starts living there.
Nearly every system design diagram has a box labelled “API gateway” between the clients and the services. Most candidates draw it and move on. Interviewers notice, because the follow-ups are easy to ask and revealing: what does it actually do, how is it not a single point of failure, and how is it different from the load balancer you drew next to it.
What it is for
A gateway is a reverse proxy at the edge of your system that does, once, the work every request needs and no service should reimplement:
- Routing:
/orders/*to the order service,/searchto search, by path, host, header or version. - Authentication: validate the session cookie or JWT, reject anonymous calls, and pass a
trusted identity (
X-User-Id, or a short-lived internal token) downstream. - Rate limiting and quotas: per user, per API key, per IP — before the request costs anything.
- TLS termination: decrypt once at the edge.
- Request shaping: size limits, header normalisation, CORS, compression.
- Observability: one place to log every request, assign a trace id and measure latency per route.
Kong, Envoy, NGINX, Traefik, AWS API Gateway, Apigee and Spring Cloud Gateway are all this, with different amounts of configuration UI.
The gateway’s job is to make every service simpler: services can assume the caller is authenticated, within its limits, over TLS, with a trace id. If a concern is the same for every service, it belongs here. If it differs per service, it belongs in the service.
Gateway, load balancer, service mesh
Three things that are often drawn as one box:
| Sits | Knows about | Typical job | |
|---|---|---|---|
| L4 load balancer | In front of anything | IPs and ports | Spread TCP connections across healthy instances |
| L7 load balancer | In front of a service | HTTP: paths, headers | Route and balance requests, terminate TLS |
| API gateway | At the edge, for external clients | Users, API keys, products | Auth, rate limits, routing to many services, API versioning |
| Service mesh | Beside every service (sidecar) | Service-to-service calls | mTLS, retries, timeouts, circuit breaking between services |
The gateway handles north–south traffic (clients into the system). A mesh like Istio handles east–west traffic (services calling each other). In practice the gateway is usually an L7 load balancer with more policy attached, and it sits behind an L4 or cloud load balancer that spreads traffic across gateway instances.
Authentication at the edge
The common shape:
- The client sends a session cookie or a bearer token.
- The gateway validates it — for a JWT, by checking the signature against the identity provider’s public keys (cached), the expiry and the audience; for an opaque token, by asking the auth service (cached for a few seconds).
- The gateway strips any client-supplied identity headers and adds its own:
X-User-Id: 42. - Services trust that header only because they are reachable only through the gateway.
Point 4 is the one that bites. If a service is also reachable directly — a public load balancer left
open, a misconfigured network policy — anyone can send X-User-Id: 1 and become user 1. Either make
services unreachable except via the gateway, or pass a signed internal token instead of a bare
header so services can verify it themselves.
Authorisation — “may user 42 edit order 812?” — usually stays in the service, because it needs
the service’s data. The gateway can enforce coarse rules (this route requires the admin scope);
the fine-grained check belongs next to the data.
Rate limiting
The gateway is the natural place for it, because it sees every request before any work is done.
- Token bucket per key: refill at the allowed rate, allow bursts up to the bucket size. The usual choice.
- Sliding window when you need exact counts per window.
- Fixed window is the simple one that allows 2× bursts at window boundaries.
With many gateway instances, limits must be shared: a counter in Redis, updated atomically with a
Lua script, keyed per user. The Redis lesson has the exact mechanics. Return
429 Too Many Requests with a Retry-After header so well-behaved clients back off.
Limit at more than one granularity: per user (fairness), per IP (abuse before login), and a global cap per downstream service (protection) — the last one is what stops a retry storm from flattening a struggling service.
Backend for frontend (BFF)
A web app, an iOS app and a partner API want different shapes of the same data. A BFF is a thin gateway-layer service per client type that calls several services and composes one response for that client: the mobile home screen in one round trip instead of six.
It is the right home for aggregation and shaping. It is the wrong home for business rules — “orders over ₹5,000 get free shipping” in the BFF means the partner API, which has a different BFF, quietly disagrees.
Not a single point of failure
The gateway is on every request’s path, so:
- Run it stateless and horizontally scaled behind a cloud or L4 load balancer, across zones. Anything stateful — rate-limit counters, sessions — lives in Redis or the token itself.
- Keep it thin. Every millisecond of gateway latency is on every request. Every plugin is a failure mode on every request.
- Set timeouts and circuit breakers per route, so one slow backend fills its own connection pool, not the gateway’s.
- Deploy config changes carefully. A bad routing rule takes down everything at once; canary gateway config like code.
Saying it in an interview
“Clients hit a cloud load balancer that spreads traffic over a stateless gateway fleet in three zones. The gateway terminates TLS, validates the JWT against cached keys, strips any identity headers the client sent and adds the user id, applies a per-user token bucket in Redis and a global limit per backend, and routes by path. Services are only reachable from the gateway’s network. Authorisation — whether this user may see this order — stays in the order service, next to the data.”
Sizing it on the whiteboard
- Throughput: a lean Envoy or NGINX instance proxies on the order of 10,000–50,000 requests/s per few cores, depending on TLS and plugins. For 200,000 peak RPS, plan ~10–20 instances plus headroom for a zone loss.
- Latency budget: aim for 1–5 ms added at p99. JWT validation with cached keys is microseconds; a synchronous call to an auth service is not — cache it.
- Rate-limit store: one Redis round trip per request; at 200,000 RPS that is a Redis cluster, or local token buckets synchronised approximately.
The follow-ups, and the one-line answers
- “Gateway vs load balancer?” A load balancer spreads traffic; a gateway also authenticates, rate-limits and routes by API — usually it is an L7 load balancer with policy.
- “Isn’t it a single point of failure?” Stateless, horizontally scaled across zones behind a load balancer, with state in Redis.
- “Where does authorisation happen?” Coarse scopes at the gateway; per-resource checks in the service.
- “How do services trust the user id?” They’re unreachable except through the gateway, or they verify a signed internal token.
- “Rate limiting across 20 gateway instances?” A shared token bucket in Redis via a Lua script.
- “Gateway vs service mesh?” Gateway for traffic entering the system; mesh for service-to-service traffic inside it.