DESIGN A DISTRIBUTED RATE LIMITER
A limit that holds across ten servers
Limit 10 per second per client. Four servers, each with its own in-memory counter, sit behind a round-robin load balancer. The client sends 60 requests in one second. How many get through?
A rate limiter on one machine is a counter. On ten machines it is a distributed counter, and the limit only holds if every server counts against the same number.
Keep that number in a shared store such as Redis, keyed by client and window: rl:client42:0. Each request increments it and is rejected once it passes the limit. The key expires with its window, so memory stays bounded. The increment must be atomic. If servers read the count, compare, and write it back, concurrent requests all read the same value and all get through.
// per-server memory: each of N servers allows LIMIT
if (local[clientId] >= LIMIT) return 429
local[clientId]++
// shared, but not atomic
n = redis.GET(key) // all N servers can read the same n
if (n >= LIMIT) return 429
redis.SET(key, n + 1, EX 1) // ...and all write n + 1key = `rl:${clientId}:${floor(now / 1000)}`
n = redis.INCR(key) // atomic: every caller sees a different n
if (n == 1) redis.EXPIRE(key, 1) // or do both in one Lua script
if (n > LIMIT) return 429 Watch the CLIENT box and the invariant strip. LOCAL COUNTERS admits 40 in the first window. SHARED, GET THEN SET admits 30: four servers read the same count, all accept, and all write back count + 1. SHARED INCR + TTL admits exactly 10 per window under the same traffic, and the old window’s key expires at 1000 ms. Drag SERVERS to 1 and every design holds, which is why single-node tests never catch this.
Each server enforces 10 requests per second on its own. The client sends 60 requests in one second. Add servers until the client gets at least 5× its limit through.
GOAL: some window accepts >= 50 requests
Write allow for a shared store. The grader counts a few requests, then calls it from 3 servers at once and tries every interleaving of their store calls, then checks the rest of the window, a second client and the next window. Each client must get exactly 10 per window, and every key you create must expire.
API gateways (Envoy's global rate limit service, Kong with a Redis policy; nginx's shared zone is shared only between the workers of one host), Redis with INCR/EXPIRE or a Lua token bucket, login and OTP throttles, per-tenant quotas. The symptom of getting it wrong: a client that gets several times its limit, and the multiple grows when the fleet scales out.
“Design a rate limiter for an API running on many servers.” On the whiteboard: requirements first (per-client limit, low added latency, accurate enough, what happens when the limiter is down). Estimates: 1M active clients, about 100 bytes a key, is about 100 MB of counters, and 50,000 checks a second fits one Redis shard with room to spare. Design: a limiter middleware in each API server, a shared Redis keyed by client and window, INCR plus EXPIRE in one Lua script, and 429 with Retry-After. Name the window's limit too: a fixed window lets up to 2× the limit through across a window boundary, which a sliding window or token bucket smooths. Then the failure you would mention: local counters multiply the limit by the fleet size, GET-then-SET races under concurrency, a replica read is stale, and you must choose fail open or fail closed when Redis is unreachable.
USED IN
Chapters that build on this lesson.
- Platform, API and technical PMProduct Manager