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
- 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
Numbers add up:8 requests = 8 allowed + 0 rejectedThe lab checks this sum as it runs, so a counting mistake would show here.
Controls
Timeline
What just happened
- 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.
Count requests per clock window (e.g. 0–5 s, 5–10 s). Reset the count when the window changes.
Remember the time of every allowed request and count the ones inside the last W seconds.
- 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
- 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
- A request
- Rejected: 429 Too Many Requests
- The API's reply
| Measure | Side 1 · Fixed window | Side 2 · Sliding window log | Gap |
|---|---|---|---|
| Requests sent | 58 | 58 | Same requests |
| Allowed | 40 | 38 | Fixed window higher by 2 |
| Rejected (429) | 18 | 20 | Fixed window lower by 2 |
| Most allowed in any 5 s (policy: 10) | 15 | 10 | Sliding window log lower by 5 |
| Average wait in the queue | 0 ms | 0 ms | Same |
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.