Updated 24 October 2026
API gateways and backends for frontends
What an API gateway does at the edge of a system: routing, authentication, rate limiting, request transformation and aggregation, the backend-for-frontend pattern, GraphQL gateways, and how to keep the gateway from becoming a bottleneck or a monolith.
Try it yourself, in a playground:Distributed rate limiting
Clients should not need to know that "the app" is forty services. An API gateway is the single entry point that receives every external request, applies the cross-cutting rules, and forwards it to the right service. Almost every architecture diagram in an interview has a box labelled "API gateway"; knowing what goes in it (and what does not) makes that box meaningful.
What a gateway does
- Routing:
/orders/*to the order service,/users/*to the user service, by path, host, header or version. - TLS termination: decrypt once at the edge.
- Authentication: validate tokens or API keys, then pass the verified identity to services in a trusted header. See security and auth.
- Rate limiting and quotas: per user, per API key, per IP. See Design a Rate Limiter.
- Request validation and size limits: reject malformed or oversized requests early.
- Transformation: protocol translation (REST to gRPC), header rewriting, response compression.
- Observability: request ids, access logs, metrics and the root of every trace.
- Resilience: timeouts, circuit breakers, load shedding when backends are overloaded.
Examples: managed gateways (AWS API Gateway, Apigee), Envoy, Kong, NGINX, and edge platforms that run gateway logic close to users.
Aggregation
A mobile home screen may need data from five services. Without help, the phone makes five calls over a slow network. The gateway (or a layer behind it) can aggregate: one client request fans out to services in parallel, and the results are combined into one response. Fewer round trips, smaller payloads, and the internal structure stays hidden. Watch the latency: the response is as slow as the slowest call, so use timeouts and return partial results when an optional part fails. See tail latency.
Backends for frontends (BFF)
Different clients want different shapes: a TV app wants big images and few fields, a phone wants small payloads, a partner API wants stability. A backend for frontend is a thin service per client type that aggregates and shapes responses for that client, owned by the team that builds the client. The generic gateway handles auth and routing; BFFs handle client-specific composition. Netflix popularised the idea. See Design Netflix.
GraphQL as a gateway
A GraphQL gateway lets clients request exactly the fields they need, resolved across services (often with federation, where each service owns part of the schema). It solves over- and under-fetching, but you must guard against expensive queries with depth limits, cost analysis and persisted queries, and caching is harder than with plain GET requests. See API design.
Keeping the gateway healthy
- Stateless and horizontally scaled behind a load balancer, in every zone.
- No business logic: once order rules creep into the gateway, it becomes a shared monolith every team must change and deploy. Keep it to cross-cutting concerns.
- Config as code with reviews and gradual rollout; a bad route rule can take down everything.
- Isolation: separate gateway pools for public, partner and internal traffic, so one cannot starve the others.
- Fast auth: verify signed tokens locally rather than calling an auth service on every request; cache API key lookups.
Gateway, load balancer, mesh
| Component | Traffic | Main job |
|---|---|---|
| Load balancer | any | spread connections across healthy instances |
| API gateway | client to system (north-south) | auth, routing, limits, aggregation for external APIs |
| Service mesh | service to service (east-west) | mTLS, retries, traffic shifting, telemetry |
They are often layered: CDN, then load balancer, then gateway, then services connected by a mesh. See load balancing and service discovery and service mesh.
In the interview
Put the gateway in your diagram and say what it does in one line: "terminates TLS, validates the JWT, applies per-user rate limits and routes to services." If the client needs data from several services, mention aggregation or a BFF. If the question is about a public API, mention API keys, quotas and versioning.
Checklist
- Single entry point with TLS, auth, rate limiting and request ids.
- Routing by path or version; services never exposed directly.
- Aggregation or BFFs for chatty clients, with timeouts and partial results.
- No business logic in the gateway.
- Stateless, multi-zone, config changes rolled out safely.
Questions people ask
What is an API gateway?
An API gateway is the single entry point that receives every external request to a system, applies the cross-cutting rules, and forwards each request to the right service. It means clients do not need to know that the app is forty services. Typical jobs are routing by path, host, header or version, TLS termination, validating tokens or API keys, rate limiting, request size limits, protocol translation such as REST to gRPC, and request ids for tracing.
API gateway vs load balancer: what is the difference?
A load balancer spreads connections across healthy instances for any kind of traffic, while an API gateway handles client-to-system (north-south) traffic and applies authentication, routing, rate limits and aggregation for external APIs. The two are usually layered rather than chosen between: a CDN, then a load balancer, then the gateway, then the services. The gateway itself runs stateless and horizontally scaled behind a load balancer in every zone.
API gateway vs service mesh: what is the difference?
An API gateway handles traffic from clients into the system (north-south), while a service mesh handles traffic between services (east-west). The gateway's job is authentication, routing, limits and aggregation for external APIs; the mesh's job is mTLS, retries, traffic shifting and telemetry between internal services. Many systems use both, with the gateway at the edge and the services behind it connected by a mesh.
What is the backend for frontend (BFF) pattern?
A backend for frontend is a thin service per client type that aggregates and shapes responses for that client, owned by the team that builds the client. It exists because clients want different shapes: a TV app wants big images and few fields, a phone wants small payloads, and a partner API wants stability. The generic gateway still handles authentication and routing, while each BFF handles client-specific composition. Netflix popularised the idea.
What is request aggregation in an API gateway?
Request aggregation means one client request fans out to several services in parallel, and the gateway (or a layer behind it) combines the results into one response. A mobile home screen that needs data from five services then makes one call instead of five over a slow network, with smaller payloads and the internal structure hidden. The response is as slow as the slowest call, so use timeouts and return partial results when an optional part fails.
Should an API gateway contain business logic?
No: an API gateway should hold only cross-cutting concerns such as authentication, routing, rate limiting and observability. Once order rules or similar logic creep in, the gateway becomes a shared monolith that every team must change and deploy. Keeping it healthy also means running it stateless in every zone, managing config as code with gradual rollout (a bad route rule can take down everything), and verifying signed tokens locally rather than calling an auth service on every request.