Six rate limiters, one stream of traffic

Every algorithm below sees exactly the same requests. Pick a scenario or shape the traffic yourself, and compare what each one lets through. The simulation runs the same TypeScript as the live limiter.

Policy

Burst is the bucket size for the token bucket, leaky bucket and GCRA. The window algorithms ignore it.

Traffic

Send requests with the button above the timeline, or press Space. Time runs for real.

Algorithms
  • Admitted (bars up)
  • Rejected (bars down)
  • Room left
  • Window edges

The six algorithms

Fixed window

Counts requests in windows aligned to the clock: each second from :00 to :01, and so on. When a window's count reaches the limit, everything else waits for the next window.

State per key
One counter and its window
Weak spot
A full quota at the end of one window plus a full quota at the start of the next: twice the limit within moments.

Sliding window log

Remembers when every admitted request happened and counts the ones in the last window, whatever the clock says. It is exact: no window of that length ever holds more than the limit.

State per key
One timestamp per admitted request in the window
Weak spot
Memory grows with the limit, which hurts at high limits and many keys.

Sliding window counter

Keeps the counts of the current and previous fixed windows, and estimates the sliding count by weighting the previous window by how much of it still overlaps.

State per key
Two counters
Weak spot
An estimate: it assumes last window's requests were spread evenly, so it can be stricter or looser than the true count.

Token bucket

A bucket holds up to burst tokens and refills at the limit's rate. Each request takes a token; with none left, it is rejected. Short bursts pass, the long-run rate holds.

State per key
Token count and last refill time
Weak spot
Over any window it admits up to burst plus the refill, more than the limit alone suggests.

Leaky bucket (meter)

A bucket of size burst leaks at the limit's rate. Each request pours in one unit, and one that would overflow is rejected. It is the token bucket seen from the other side: water level instead of tokens left.

State per key
Water level and last update time
Note
The queueing leaky bucket delays requests instead of rejecting them; this is the metering one.

GCRA

The Generic Cell Rate Algorithm, from ATM networks. It stores when the next request is due if the client sent at exactly the allowed rate, and rejects requests that arrive too far ahead of that schedule.

State per key
One timestamp
Why use it
Token-bucket behaviour with less state and no refill step. The live API runs it.

Token bucket, leaky bucket and GCRA are three descriptions of one policy: for the same limit, period and burst they make identical decisions, which the property tests check on generated traffic. They differ only in what they store.