Design a Rate Limiter
An educational design exercise, not a description of any company’s real system.
01Problem
Design a rate limiting component for a public API. The API is served by a fleet of several hundred gateway instances behind load balancers. Each incoming request must be checked against rules such as "an API key may make 100 requests per second with bursts up to 200" or "an IP may attempt login 5 times per minute". Requests over the limit are rejected with HTTP 429 before they reach backend services.
Because any request from a client can land on any gateway instance, the count for a key has to be shared across the fleet. The check sits on the critical path of every API call, so it must add very little latency, keep working when parts of it fail, and protect backend services and tenants from each other without blocking well-behaved clients.
02Functional requirements
- Check each request against one or more rules keyed by API key, user id, client IP and/or route, and return allow or deny.
- Support rules expressed as a rate plus burst (e.g. 100 req/s, burst 200) and as fixed quotas per window (e.g. 5 login attempts per minute).
- Return HTTP 429 with `Retry-After` and remaining-quota headers when a request is rejected; expose remaining quota on allowed requests too.
- Let operators create and change rules without redeploying gateways; changes take effect within 60 seconds.
- Support a shadow (log-only) mode for new rules so their impact can be measured before enforcement.
03Non-functional requirements
- Peak 1 million requests/s across the fleet (average 400,000/s), about 300 gateway instances per region.
- Added latency: p50 under 1 ms and p99 under 3 ms per request.
- The API must stay available if the limiter is degraded: fail open (with a local fallback limit) rather than reject all traffic.
- Accuracy: brief over-admission of up to about 10% is acceptable; rejecting a client that is under its limit is not.
- Up to 10 million active API keys and tens of millions of distinct IPs per day.
- Limits are enforced per region; there is no requirement for exact global limits across regions.
04Clarifying questions
Ask these first. Open one to see the answer the interviewer gives.
Is this a library inside each gateway, a separate service, or a feature of a third-party gateway?
We own the gateways. Build it as a library in the gateway backed by a shared store; a separate service is acceptable if you can justify the extra hop.
What do rules look like and what do they key on?
Per API key (tenant plans, e.g. 100 req/s burst 200), per authenticated user, per IP for unauthenticated traffic, and per route for expensive endpoints such as login or search. A request can match up to three rules.
How accurate must the limits be?
They protect capacity and fairness, not billing. Short over-admission of up to about 10% is fine. Monthly billing quotas are a separate system with durable counters and are out of scope.
What should happen if the shared counter store is unreachable?
Fail open. An outage of the limiter must not take the API down. Approximate local limiting during the outage is welcome.
Do limits apply globally across regions?
No. Each region enforces its own limits; a tenant's limit is split across regions by configuration. Cross-region coordination would add too much latency.
Is volumetric DDoS protection in scope?
No. Network-level floods are absorbed upstream by the CDN/WAF. This limiter enforces application-level fairness and abuse rules on traffic that reaches the gateways.
- Capacity estimation
- API design
- Data model
- High-level architecture
- Component deep dive
- Scaling strategy
- Failure handling
- Security
- Observability
- Trade-offs
- Alternative designs
- Follow-up questions
- Capacity estimation
- API design
- Data model
- High-level architecture
- Component deep dive
- Scaling strategy
- Failure handling
- Security
- Observability
- Trade-offs
- Alternative designs
- Follow-up questions