Concept · Reliability

Rate-limiting algorithms

Fixed window, sliding log, sliding counter and token bucket, compared on the same traffic.

3 mintoken bucketleaky bucketsliding window429

A rate limiter caps how many requests a client may make per unit of time. Four algorithms dominate; all allow limit requests per window in the long run, but behave differently in the short run.

AlgorithmState per keyBehaviourWatch out for
Fixed windowOne counter per aligned windowCount requests in 12:00:00–12:00:59, reset at the boundaryUp to 2× the limit across a boundary
Sliding logA timestamp per accepted requestCount requests in the last window seconds — exactMemory grows with the limit
Sliding window counterTwo countersprevious × overlap + current — a close approximationSlight over-admission right after a boundary
Token bucketTokens and a timestampBucket of capacity tokens refilled at rate; each request takes oneAllows bursts up to capacity (by design)

A leaky bucket queues requests and releases them at a constant rate — it smooths output rather than rejecting bursts, and is common in traffic shaping.

Run the same traffic through all four and compare what each lets through:

InteractiveFour limiters, one stream of requestsOpen in Labs
Loading interactive…

The token bucket admits a full bucket at once and then follows its refill rate. Switch to window-edge traffic and look at the last column: the fixed window lets twice its limit through around the boundary.

Choosing

  • Token bucket for API rate limits: tolerate short bursts, cap the sustained rate. Two numbers of state.
  • Sliding window counter for high-volume limits where you want close-to-exact windows cheaply.
  • Sliding log for low limits that must be exact, such as login attempts.
  • Fixed window for daily and monthly quotas, where boundary bursts don't matter.

In a distributed system

Counters must be shared (usually in Redis) and updated atomically — a Lua script for read-decide-write logic — or each server must enforce a share of the limit locally. Decide whether to fail open or closed when the store is unreachable, and return 429 with Retry-After. The System Design roadmap's rate-limiting lesson builds a Redis token bucket; see also load shedding.

Learn it in depth