DESIGN A DISTRIBUTED RATE LIMITER

A limit that holds across ten servers

L318 MINSYSTEM DESIGN
PREDICT

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.

▶ SIMULATION
CLIENT 42
sent this window 0
accepted this window 0 / 10
t = 0 ms · window 0 (0–999 ms)
SHARED STORE (unused)
no keys
keys expired 0
SERVERS × 4
S1: accepted 0 · rejected 0 · local count 0
S2: accepted 0 · rejected 0 · local count 0
S3: accepted 0 · rejected 0 · local count 0
S4: accepted 0 · rejected 0 · local count 0
each server allows 10/s on its own: up to 40/s in total
windows—
last event—
✓ accepted per client per window <= 10
SCHEDULE
01[DESIGN]each server keeps its own in-memory counter: 10 per second
02[CLIENT]t=0 ms: client sends 6 requests at once
03[CLIENT]t=100 ms: client sends 6 requests at once
04[CLIENT]t=200 ms: client sends 6 requests at once
05[CLIENT]t=300 ms: client sends 6 requests at once
06[CLIENT]t=400 ms: client sends 6 requests at once
07[CLIENT]t=500 ms: client sends 6 requests at once
08[CLIENT]t=600 ms: client sends 6 requests at once
09[CLIENT]t=700 ms: client sends 6 requests at once
10[CLIENT]t=800 ms: client sends 6 requests at once
11[CLIENT]t=900 ms: client sends 6 requests at once
12[CLIENT]t=1000 ms: client sends 6 requests at once
13[CLIENT]t=1100 ms: client sends 6 requests at once
14[CLIENT]report
Start of local counters scenario. 14 steps.
// 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 + 1

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.

TUNE IT NOT YET

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

FIX IT NOT YET

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.

Mental model:

a distributed limit is one counter, not N copies of it, and every decision is an atomic increment that returns the new value, never a read followed by a write. Name the key after the window and give it a TTL, so the counters clean themselves up. Everything else (sliding windows, token buckets in Lua, local pre-checks) is an optimization on top of those three.

IN PRODUCTION

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.

IN INTERVIEWS

“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.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: CHAT & PRESENCE →