Design an API Gateway
Understanding the Problem
🚪 What is an API gateway? An API gateway is the single entry point that external clients (mobile apps, web apps, partner integrations) use to reach a system built from many internal services. It receives every request, works out which service should handle it, and on the way handles the concerns every service would otherwise repeat: authentication, rate limiting, TLS, logging and request shaping. Kong, AWS API Gateway, Apigee and Envoy-based gateways are common examples.
You might also be asked to design a Backend for Frontend (BFF). That's a specialized layer of the same design: a gateway built for one kind of client, which combines several backend calls into the exact response that client needs. We'll cover it as one of the core requirements.
Functional Requirements
Core Requirements
- The gateway should route each external request to the right internal service, based on host, path, method and API version.
- The gateway should authenticate every request (API keys, OAuth access tokens) and pass a verified identity to backend services.
- The gateway should enforce rate limits and quotas per client and per route.
- Different clients (iOS, Android, web, partners) should be able to get responses shaped for them, including one response built from several backend calls.
Below the line (out of scope)
- Traffic between internal services (east-west). That's a service mesh's job.
- A developer portal, API key self-service and usage billing.
- Web application firewall rules and DDoS scrubbing, handled in front of the gateway.
Non-Functional Requirements
Ask about traffic and the latency budget. For our design: 500k requests per second at peak, about 300 backend services, and a latency budget for the gateway itself of a few milliseconds at p99 on top of the backend's own time.
Core Requirements
- The gateway should add under 5ms at p99 for routing, auth and rate limiting.
- It should be highly available (99.99%), since every external request depends on it.
- A slow or failing backend must not slow down requests to other backends.
- Route, auth and limit changes should take effect within seconds, without restarts or dropped connections.
Below the line (out of scope)
- Multi-region active-active routing. We'll run in each region independently.
Two estimates change the design. First, capacity: a gateway node doing TLS, JWT verification, a rate limit check and proxying handles on the order of 10k to 20k requests per second, so 500k needs 25 to 50 nodes, plus headroom for losing a zone. Second, concurrency. By Little's law, requests in flight = arrival rate × time in system. If backends take 100ms on average, the gateway holds 500k × 0.1s = 50,000 requests in flight at any moment, and much more when a backend slows down. A thread-per-request design would need tens of thousands of threads; the gateway must use non-blocking, event-driven I/O (the model behind NGINX, Envoy and Netty-based gateways). It also means that any per-request network call the gateway makes for its own purposes, like asking an auth service whether a token is valid, is another 500k calls per second we'd rather avoid.
The gateway is on every request's path, so its own design rule is simple: never make a network call per request if a local check will do. Most of this design applies that rule to auth, rate limits and configuration.