Lab · Traffic

Rate limiting

A rate limiter decides, per client, whether a request may go through or gets a 429 Too Many Requests. All five algorithms here enforce the same policy — compare how they handle bursts and window edges.

Try it

Lab time 3.0 s
Client (one API key), 3 req/sClient (one API key)3 req/sRate limiter, Token bucketRate limiterToken bucket6.9 / 10 tokensAPI service, 10 per 5 s allowedAPI service10 per 5 s allowed8 handled
About each part:
  • Client (one API key) — 3 req/s — healthy
  • Rate limiter — Token bucket — healthy — 6.9 / 10 tokens
  • API service — 10 per 5 s allowed — healthy — 8 handled
  • Request
  • Error / rejected
  • Response
Requests
8
Allowed
8
Rejected (429)
0
0% of all
Most in any 5 s
8/ 10
forwarded to the API

Numbers add up:8 requests = 8 allowed + 0 rejectedThe lab checks this sum as it runs, so a counting mistake would show here.

Controls

Algorithm

A bucket holds up to L tokens and refills at L/W per second. Each request spends one token.

Tokens left6.9 / 10 tokens
Policy
10 requests
5 s

Same policy for every algorithm: on average 2.0 requests per second.

Traffic
3 req/s

Timeline

Requests over the last 15 s
AllowedRejected (429)Last 5 s
0snow (3s)

What just happened

  1. 0.0sInfo: Policy: at most 10 requests per 5 s using Token bucket.

Compare two algorithms on the same requests

Both sides enforce one policy, at most 10 per 5 s, on the same requests at the same moments. Only the algorithm differs, so any gap comes from the algorithm. Both sides start with 20 seconds already run.

Side 1 algorithm

Count requests per clock window (e.g. 0–5 s, 5–10 s). Reset the count when the window changes.

Side 2 algorithm

Remember the time of every allowed request and count the ones inside the last W seconds.

3 req/s
Lab time 20.0 s
Side 1 Fixed window
Client (one API key), 3 req/sClient (one API key)3 req/sRate limiter, Fixed windowRate limiterFixed window0 / 10 this window · resets in 5.0 sAPI service, 10 per 5 s allowedAPI service10 per 5 s allowed40 handled
  • Client (one API key) — 3 req/s — healthy
  • Rate limiter — Fixed window — healthy — 0 / 10 this window · resets in 5.0 s
  • API service — 10 per 5 s allowed — healthy — 40 handled
Requests over the last 15 s
AllowedRejected (429)Window reset
5s10s15s20s5snow (20s)
Side 2 Sliding window log
Client (one API key), 3 req/sClient (one API key)3 req/sRate limiter, Sliding window logRate limiterSliding window log9 / 10 in the last 5 sAPI service, 10 per 5 s allowedAPI service10 per 5 s allowed38 handled
  • Client (one API key) — 3 req/s — healthy
  • Rate limiter — Sliding window log — healthy — 9 / 10 in the last 5 s
  • API service — 10 per 5 s allowed — healthy — 38 handled
Requests over the last 15 s
AllowedRejected (429)Last 5 s
5snow (20s)
  • A request
  • Rejected: 429 Too Many Requests
  • The API's reply
Compare two algorithms on the same requests: the numbers for each side and the gap between them
MeasureSide 1 · Fixed windowSide 2 · Sliding window logGap
Requests sent5858Same requests
Allowed4038Fixed window higher by 2
Rejected (429)1820Fixed window lower by 2
Most allowed in any 5 s (policy: 10)1510Sliding window log lower by 5
Average wait in the queue0 ms0 msSame

Changing a setting starts both sides again, so the numbers always describe one setup from its start. The window-edge test sends 10 requests just before a fixed window resets and 10 just after, on both sides at the same moment. Only the leaky bucket makes requests wait; the others answer at once.

What this model simplifies
  • One client and one limiter. Real limiters keep a counter per user, IP or API key, usually in a shared store such as Redis so every gateway instance sees the same count.
  • Checking and updating the counter is atomic here. In a distributed limiter you need an atomic operation (e.g. a Lua script or INCR with expiry) or two gateways can both let the last request through.
  • Rejected requests are simply dropped with a 429. Good APIs also return a Retry-After header so clients back off.