Design a distributed rate limiter
A distributed rate limiter caps how many requests a client can make in a window, enforced consistently across many servers. The crux is a shared, atomic counter (Redis) and choosing the right algorithm for accuracy vs cost.
Infrastructure, 8 components, about 30 min
The full guide covers requirements, estimates, the API, the data model, deep dives, trade-offs, failure modes and the follow-up questions interviewers ask.
Read the full guide on SubstackKey points
- Algorithm: Token Bucket — refill rate R/sec, burst capacity B.
- Atomic Lua script in Redis: GET → check → DECR → SETEX in one round-trip.
- Key: rl:{user_id}:{endpoint}; sliding 1-min window.
- On Redis outage: fail-open (allow) is safer than fail-closed for read APIs.
- Return 429 with Retry-After header; emit metric per (user, route, allowed/denied).
Sign in to track this guide in your curriculum progress.