← All lessons

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.

15 min readUpdated November 2026Free

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:

Kong, Envoy, NGINX, Traefik, AWS API Gateway, Apigee and Spring Cloud Gateway are all this, with different amounts of configuration UI.

The point

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:

  1. The client sends a session cookie or a bearer token.
  2. 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).
  3. The gateway strips any client-supplied identity headers and adds its own: X-User-Id: 42.
  4. Services trust that header only because they are reachable only through the gateway.
Where candidates lose it

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.

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:

Saying it in an interview

Say it like this

“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

The follow-ups, and the one-line answers

  1. “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.
  2. “Isn’t it a single point of failure?” Stateless, horizontally scaled across zones behind a load balancer, with state in Redis.
  3. “Where does authorisation happen?” Coarse scopes at the gateway; per-resource checks in the service.
  4. “How do services trust the user id?” They’re unreachable except through the gateway, or they verify a signed internal token.
  5. “Rate limiting across 20 gateway instances?” A shared token bucket in Redis via a Lua script.
  6. “Gateway vs service mesh?” Gateway for traffic entering the system; mesh for service-to-service traffic inside it.