Skip to content
$ lynuxpro
← All system designs

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 →

ClientEdge / networkData storeServiceInfrastructureasync / background

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-After header.

Algorithms in plain words

AlgorithmHow it worksGood and bad
Fixed windowcount per minute, reset at :00simple, but allows a double burst at the edge of two windows
Sliding windowweight the previous window by how much of it is still "inside"smoother, still cheap
Token bucketa bucket refills at a steady rate; each request takes a tokenallows short bursts, steady average; most common
Leaky bucketrequests leave at a fixed rate from a queuevery 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

  1. A request reaches the API gateway with its API key.
  2. 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.
  3. Allowed: forward to the backend. Not allowed: return 429 with Retry-After.
  4. Limits per plan come from a rules service and are cached in the gateway.
  5. Metrics show 429 rates and the top limited keys.

Key decisions

DecisionChoiceWhy
Where it runsgateway / edgestops bad traffic before it costs anything
Shared stateRedis cluster, keys sharded by API keyfast, atomic, scales out
If Redis is downfail open with a local per-server limita limiter outage should not become a full outage
Very hot keyssmall local token cache, sync to Redisfewer 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-After plus jittered backoff in SDKs spreads them out.

Load and capacity

ItemAssumptionResult
Traffic50,000 requests/s at peak50,000 Redis calls/s (one Lua call per request)
Redis capacity~100,000 simple ops/s per shard3 shards gives 6x headroom
Memory1 million API keys × ~100 bytes per bucket~100 MB: tiny
Added latencyRedis in the same AZunder 1 ms at p99

High availability and disaster recovery

TargetNumberHow it is met
Availabilitymust never lower the gateway's 99.99%fail open with local limits when Redis is slow
Redis shard fails~10-20 s of approximate limitsreplica per shard in another AZ promotes automatically
Region failsmoves with the gatewayeach region has its own Redis; limits are per region
Data loss (RPO)not neededcounters 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 INCR with TTL) and a global view across servers.
  • A clear decision on failing open or closed, and the 429 contract.