Rate limiter algorithms lab
Send the same traffic through a fixed window, a sliding log, a sliding counter, a token bucket and a leaky bucket, and compare what each admits, rejects and remembers.
limit = 20 per 20 ticksrate = 1 per tickoffered so far 60 requests in 70 ticks
| Series | Latest | Lowest | Highest |
|---|---|---|---|
| Limit (20) | 20 | 20 | 20 |
| Fixed window | 12 | 0 | 20 |
| Sliding window log | 12 | 0 | 20 |
| Sliding window counter | 1 | 0 | 20 |
| Token bucket | 12 | 0 | 26 |
| Leaky bucket (queue) | 20 | 0 | 20 |
| Series | Latest | Lowest | Highest |
|---|---|---|---|
| Fixed window | 16 | 0 | 16 |
| Sliding window log | 16 | 0 | 16 |
| Sliding window counter | 27 | 0 | 27 |
| Token bucket | 10 | 0 | 10 |
| Leaky bucket (queue) | 10 | 0 | 10 |
| Series | Latest | Lowest | Highest |
|---|---|---|---|
| Arrivals | 0 | 0 | 12 |
All five on the same traffic
| Algorithm | Admitted | Rejected | Worst window | Peak state per client | Mean delay |
|---|---|---|---|---|---|
| Fixed window | 44 | 16 | 20 | 2 values | none |
| Sliding window log | 44 | 16 | 20 | 20 values | none |
| Sliding window counter | 33 | 27 | 20 | 3 values | none |
| Token bucket | 50 | 10 | 26 (1.3x the limit) | 2 values | none |
| Leaky bucket (queue) | 42 | 10 | 20 | 20 values | 8.7 ticks |
Red means a window let through more than the limit. Memory counts values stored per client: the log keeps one timestamp per admitted request, the leaky bucket keeps its queue, and the rest keep two or three numbers.
- Fixed window: admitted 44, rejected 16, worst 20-tick window 20 against a limit of 20, peak state 2 values.
- Sliding window log: admitted 44, rejected 16, worst 20-tick window 20 against a limit of 20, peak state 20 values.
- Sliding window counter: admitted 33, rejected 27, worst 20-tick window 20 against a limit of 20, peak state 3 values.
- Token bucket: admitted 50, rejected 10, worst 20-tick window 26 against a limit of 20, peak state 2 values.
- Leaky bucket (queue): admitted 42, rejected 10, worst 20-tick window 20 against a limit of 20, peak state 20 values, mean delay 8.7 ticks, longest 19.
What to try
Each one sets up a starting point. Then make the change it describes.
The burst across a window edge
Pick the burst across a window edge, keep the offered traffic at 1.0, and compare all five. Read the worst window column and the first chart.
Look for. The fixed window admits a full limit just before the edge and another just after, so its worst window is twice the limit. The sliding log never goes above the limit. The token bucket and the sliding counter stay close to it.
Steady traffic under the limit
Pick steady traffic at 0.8 of the limit and compare all five.
Look for. The average is under the limit, but random gaps and clumps still push some windows over. The strict algorithms reject requests that the token bucket, with its spare tokens, lets through.
Shrink the token bucket
Use bursty traffic and drop the bucket size from 20 to 4. Watch the token bucket line in the rejected chart.
Look for. The refill rate does not change, but a small bucket cannot hold a burst, so most of every burst is rejected. Bucket size is the burst tolerance.
Delay instead of reject
Use bursty traffic at 0.7 and compare the leaky bucket with the token bucket. Read the mean delay column.
Look for. Both admit about the same number of requests. The token bucket lets a burst through at once. The leaky bucket holds the burst in its queue and releases it at a steady pace, so it adds delay.
What exactness costs in memory
Raise the limit to 60 and watch the peak state column.
Look for. The sliding log keeps a timestamp for every admitted request, so its state grows with the limit. The leaky bucket grows with its queue. The other three stay at two or three values however large the limit is.
New to the idea? Read the lesson first. Every number here comes from a seeded simulator, so the same seed gives the same run.
Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms