Rate Limiter system design: how it works
Gives each caller a bucket of tokens that refills over time, and answers 429 Too Many Requests when the bucket runs dry.
Loading the interactive diagram…
How Rate Limiter works, step by step
Gives each caller a bucket of tokens that refills over time, and answers 429 Too Many Requests when the bucket runs dry.
Callers
- Client app
- Client · Hypothetical. Anything calling the API with its key: an app, a partner's server, or a script gone wrong.
Front door
- API gateway
- Gateway · Hypothetical. Every request comes through here and is checked before it can reach the API.
Services
- Rate limiter
- Service · Hypothetical. Decides allow or block for each request, using a token bucket per key.
- Your API
- Service · Hypothetical. The real work: search, orders, whatever the API does. It only sees requests that were allowed.
Data
- Counter store
- Cache · Hypothetical. One small entry per key: tokens left and when it was last refilled.
- Limit rules
- Database · Hypothetical. How much each kind of caller may do: free plan 60 a minute, paid plan 1,000, and so on.
Flow: A request within the limit
The usual case: a token is taken, the request goes through, and the answer says how many are left.
- Client app → API gateway (GET /search (key k_7)). An app calls the search API with its key.
- API gateway → Rate limiter (k_7 allowed?). Before doing anything else, the gateway asks the rate limiter.
- Rate limiter → Counter store (Take 1 token for k_7). The limiter tops the bucket up for the time that has passed, then takes one token, in a single atomic step.
- Counter store → Rate limiter (OK, 41 left). There were tokens to spare: 41 remain.
- Rate limiter → API gateway (Allowed). The limiter says yes. All this takes about a millisecond.
- API gateway → Your API (GET /search). Only now does the request reach the API.
- Your API → API gateway (Results). The API does its work and answers.
- API gateway → Client app (200 + 41 remaining). The answer carries headers like RateLimit-Remaining: 41, so well-behaved clients can slow down before they hit the limit.
Flow: A request over the limit
The bucket is empty, so the request is turned away at the door with 429 and told when to try again. The API never sees it.
- Client app → API gateway (GET /search (key k_7)). A buggy script calls the API in a tight loop.
- API gateway → Rate limiter (k_7 allowed?). The gateway asks the limiter, as for every request.
- Rate limiter → Counter store (Take 1 token for k_7). The limiter tries to take a token.
- Counter store → Rate limiter (Empty, next in 1 s). The bucket is empty. It refills at one token per second, so the next token arrives within a second: a script in a tight loop gets about one request a second through, which is exactly the limit.
- Rate limiter → API gateway (Blocked, retry in 1 s). The limiter says no.
- API gateway → Client app (429, Retry-After: 1). The gateway answers 429 Too Many Requests with a Retry-After header. The API behind it is untouched, which is the whole point.
Flow: The first request from a new key
A key the limiter hasn't seen recently: it looks up the key's plan, then starts it with a full bucket.
- Client app → API gateway (GET /search (key k_9)). A new customer makes their first call.
- API gateway → Rate limiter (k_9 allowed?). The gateway asks the limiter.
- Rate limiter → Limit rules (Limit for k_9?). The limiter has no rule for this key in memory yet, so it looks up the key's plan.
- Limit rules → Rate limiter (Free: 60 per minute). Free plan: a bucket of 60 tokens, refilling at one per second. The limiter remembers this for a while.
- Rate limiter → Counter store (New bucket, take 1). There's no bucket for k_9, so it starts full, and one token is taken.
- Counter store → Rate limiter (OK, 59 left). Plenty left for a first-time caller.
- Rate limiter → API gateway (Allowed). The limiter says yes.
- API gateway → Your API (GET /search). The request goes through to the API.
- Your API → API gateway (Results). The API answers.
- API gateway → Client app (200 + 59 remaining). The customer gets their results, and the headers show their limit.
HowItScales is not affiliated with the companies shown. Components marked Assumption or Hypothetical are not confirmed by the company.