System design · 3 of 16
Mid
Senior
- Monitoring and alerting for 100 servicesSRE · Platform · DevOps
- Secrets management platformDevOps · Platform · SRE
- Multi-region active-active serviceSRE · Platform
- Distributed job scheduler (cron at scale)SRE · Platform
- Internal developer platform (IDP)Platform
- Multi-tenant Kubernetes platformPlatform · DevOps · SRE
- GitOps for a fleet of 100 clustersPlatform · DevOps
- ML training platform and feature storeMLOps · Platform
- LLM inference platformMLOps · Platform · SRE
MID · 10 MIN · SRE · PLATFORM · DEVOPS
API rate limiter
Token buckets in Redis at the gateway: protect services, share limits across many servers, and fail safely.
- Peak load
- 50,000 req/s
- Availability target
- 99.99%
- RTO: back up within
- 20 s
- RPO: data you can lose
- not needed
← Swipe sideways to see the whole diagram →
The question
"Design a rate limiter so each API key can make at most N requests per minute, across a fleet of gateway servers."
Requirements
- Limit by API key (and sometimes by IP or user) with limits per plan, for example free 60/min, pro 1,000/min.
- Work across many gateway servers: the limit is global, not per server.
- Add very little delay (about 1 ms).
- Tell clients clearly: HTTP 429 with a
Retry-Afterheader.
Algorithms in plain words
| Algorithm | How it works | Good and bad |
|---|---|---|
| Fixed window | count per minute, reset at :00 | simple, but allows a double burst at the edge of two windows |
| Sliding window | weight the previous window by how much of it is still "inside" | smoother, still cheap |
| Token bucket | a bucket refills at a steady rate; each request takes a token | allows short bursts, steady average; most common |
| Leaky bucket | requests leave at a fixed rate from a queue | very smooth output, adds waiting |
Think of it like: a cinema with a ticket machine that prints one ticket per second. You can arrive with friends and use saved-up tickets (a burst), but on average only one person per second gets in.
How it works
- A request reaches the API gateway with its API key.
- The gateway runs one small Lua script in Redis: refill tokens based on time passed, take one if available, and set a TTL. Doing it in one script makes it atomic, so two servers can't both take the last token.
- Allowed: forward to the backend. Not allowed: return 429 with
Retry-After. - Limits per plan come from a rules service and are cached in the gateway.
- Metrics show 429 rates and the top limited keys.
Key decisions
| Decision | Choice | Why |
|---|---|---|
| Where it runs | gateway / edge | stops bad traffic before it costs anything |
| Shared state | Redis cluster, keys sharded by API key | fast, atomic, scales out |
| If Redis is down | fail open with a local per-server limit | a limiter outage should not become a full outage |
| Very hot keys | small local token cache, sync to Redis | fewer Redis calls |
What breaks and how it heals
- Redis slow or down: fall back to an in-memory limit per server (limit ÷ number of servers) and alert.
- Clock differences: use Redis server time inside the script, not each gateway's clock.
- Retry storms: clients retry immediately after 429;
Retry-Afterplus jittered backoff in SDKs spreads them out.
Load and capacity
| Item | Assumption | Result |
|---|---|---|
| Traffic | 50,000 requests/s at peak | 50,000 Redis calls/s (one Lua call per request) |
| Redis capacity | ~100,000 simple ops/s per shard | 3 shards gives 6x headroom |
| Memory | 1 million API keys × ~100 bytes per bucket | ~100 MB: tiny |
| Added latency | Redis in the same AZ | under 1 ms at p99 |
High availability and disaster recovery
| Target | Number | How it is met |
|---|---|---|
| Availability | must never lower the gateway's 99.99% | fail open with local limits when Redis is slow |
| Redis shard fails | ~10-20 s of approximate limits | replica per shard in another AZ promotes automatically |
| Region fails | moves with the gateway | each region has its own Redis; limits are per region |
| Data loss (RPO) | not needed | counters are short-lived; losing them only resets a minute of counting |
What interviewers listen for
- You can compare algorithms and pick one with reasons.
- Atomic updates (Lua or
INCRwith TTL) and a global view across servers. - A clear decision on failing open or closed, and the 429 contract.