Design a distributed rate limiter
breadth · 45 min · senior/staff
Setting
You're on the platform team for a multi-tenant API gateway. Every request from every customer flows through a shared fleet of stateless gateway nodes before reaching backend services, and a handful of noisy tenants have twice taken those services down by bursting far past their contracted quota.
The ask
Design a rate limiter that the gateway calls on every request to admit or reject it, enforced per API key and per endpoint, consistently across the whole fleet.
Constraints & scale
- ~500K requests/sec sustained across the fleet, hundreds of gateway nodes, thousands of distinct API keys with different quotas
- The admit/reject decision must add < 5 ms p99 to every request
- Limits are enforced fleet-wide, not per-node — a key must not get N× its quota by spreading requests across N nodes
- A limiter outage must fail safe (open) for paying customers rather than take down the API, but must still stop an active abuse burst
- Quotas change per-customer (plan upgrades, temporary throttles) and must take effect within seconds, not on the next deploy
What a strong answer covers
Requirements → estimates → algorithm choice (token bucket / sliding window / etc., and why) → API/data model → high-level diagram → 1–2 deep dives → trade-offs. Likely probes: where the counters live, how nodes agree on a fleet-wide count without a synchronous call per request, and the fail-open/fail-closed decision.
A 45-minute plan
- 0-5 min · Requirements
- 5-10 min · Estimates/API
- 10-25 min · High-level
- 25-40 min · Deep dive
- 40-45 min · Wrap
What to cover, in order
- Requirements
- Estimates
- Algorithm choice (token bucket / sliding window / etc., and why)
- API/data model
- High-level diagram
- 1–2 deep dives
- Trade-offs
Where the follow-up questions go
Pick one of these for your deep dive before the interviewer picks for you.
- Where the counters live
- How nodes agree on a fleet-wide count without a synchronous call per request
- The fail-open/fail-closed decision
Key trade-offs
Decisions a strong answer weighs, and what pushes each one either way.
- Algorithm. A token bucket allows bursts up to its capacity and is cheap to store. A fixed window is the simplest and allows double the limit across a boundary. A sliding window is smoother and costs more memory or an approximation. Pick based on whether bursts are acceptable to the backend.
- Central store or local counters. A shared store enforces one limit across the fleet and puts a network hop on every request. Local counters are fast and let the effective limit grow with node count. A common compromise is local budgets leased from the shared count, which trades some accuracy for far fewer remote calls.
- Accuracy or latency. Exact counting needs an atomic operation per request on a shared key. Approximate counting, with batching or asynchronous sync, keeps the 5 ms budget and lets a key overshoot briefly. Say how much overshoot the contract tolerates.
- Fail open or fail closed. Failing open keeps paying customers working when the limiter breaks, and allows abuse while it is down. Failing closed protects backends and turns a limiter fault into an outage. You can fail open per tenant class and keep a coarse local limit as a floor.
- Config propagation. Pushing quota changes gives fast effect and needs a delivery channel. Polling or short caches are simpler and lag by the interval.
Common mistakes
What goes wrong most often on this problem, and why it hurts.
- A counter in each gateway's memory. The limit then multiplies with the node count and shifts as nodes scale or restart.
- Separate read and increment steps. Two requests can both read below the limit and both pass. The check and update must be one atomic operation.
- Not explaining what the client sees. Return a clear rejection with retry timing, so well-behaved clients back off instead of hammering.
- Hot keys on one store shard. A single tenant with huge traffic puts all its counter operations on one node. Plan for that with local aggregation or splitting the counter.
- Forgetting that the limiter is a dependency. Under the same surge it is meant to stop, its own store can saturate. Size it for peak, with timeouts and a defined fallback.
Graded on
Requirements & scoping · Estimation · API & data model · High-level architecture · Deep dive · Scalability & performance · Reliability & consistency · Trade-offs · Operability · Communication & drive