Try
↑ ↓ move↵ openesc close

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

Requests admitted in the last 20 ticks
Requests admitted in the last 20 ticks. Text version available below the chart.013253850tick 1tick 36tick 70timeadmitted
Requests rejected so far
Requests rejected so far. Text version available below the chart.013253850tick 1tick 36tick 70timerejected
Requests arriving at each tick
Requests arriving at each tick. Text version available below the chart.05101520tick 1tick 36tick 70timearrivals

All five on the same traffic

Each row is one algorithm. Worst window is the most it admitted in any 20 ticks in a row, against a limit of 20.
AlgorithmAdmittedRejectedWorst windowPeak state per clientMean delay
Fixed window4416202 valuesnone
Sliding window log44162020 valuesnone
Sliding window counter3327203 valuesnone
Token bucket501026 (1.3x the limit)2 valuesnone
Leaky bucket (queue)42102020 values8.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.

What to try

Each one sets up a starting point. Then make the change it describes.

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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