LESSON 04 · Window algorithms

Pick the accuracy and memory tradeoff

Counting requests by time is simple until traffic lands on a boundary.

SYSTEM VIEW

The decision path

Diagram view
Swipe or scroll to explore the full diagram
The boundary changes the answerA fixed counter resets at the minute boundary, while a rolling window still includes recent requests from the previous minute.The boundary changes the answerresetper blocklook backMinute A100 near its endMinute B100 near its startFixed counterallows both blocksSliding windowsees the combined burstTwo adjacent full fixed windows can create a short, oversized burst.
A fixed counter resets at the minute boundary, while a rolling window still includes recent requests from the previous minute.
Trace the quota decision before a request reaches the API.

Which rate limiter lets the burst through?

Four common algorithms, all given the same limit and the same traffic. Every request below is really run through each algorithm, so what you see is what it would do.

Traffic
100 requests
Per window of
Requests arriving 200 requests: a full allowance just before a window boundary, then another just after it
Which algorithm lets the most requests through inside any one 60-second span?

Commit to an answer first. Being wrong here is the useful part.

What this model assumes
  • Each request costs one unit and arrives at an exact, repeatable time, with no randomness.
  • A single limiter sees every request. A real distributed limiter also has to keep shared counters correct under concurrency.
  • The token bucket holds as many tokens as the limit and refills at the limit divided by the window. Other sizes change how large a burst it allows.
  • “Peak in any window” is the most requests admitted in any span one window long, which is what the protected service experiences.
THE IDEA

How it works

A fixed window counter increments one value per identity per time block. It is cheap, but a client can send a full quota just before a boundary and another just after it. With a limit of 100 per minute, 100 requests at 12:00:59 and 100 at 12:01:00 all pass: 200 in about a second. That is harmless for a monthly plan quota, and a real problem when the limit protects a small backend.

A sliding window log checks timestamps over the true rolling period and is more precise, but stores many timestamps and costs more memory. For one million active clients at that limit, a log holds up to 100 timestamps each: at 8 bytes per timestamp, 800 MB before overhead. Two counters per client take about 16 MB.

A sliding window counter combines counts from the current and previous blocks, weighting the previous block by how much of it overlaps the rolling period. Suppose the previous minute saw 80 requests, the current one has 30, and we are 15 seconds in. The rolling minute still covers 75% of the previous block, so the estimate is 80 × 0.75 + 30 = 90, under the limit. It uses little memory and softens boundary spikes, but assumes requests in the previous block were spread evenly. Cloudflare has written about using this approximation in its rate limiting. Choose it when approximation is acceptable; choose a stricter method when a hard rolling-window cap is essential.

DECISION POINT

A strict rule permits at most 100 requests in every rolling minute. Is a fixed-minute counter enough?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • Fixed windows can admit boundary bursts.
  • Sliding logs favor precision; sliding counters favor lower memory.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
DiagnoseQUESTION 01

Logs show 190 requests from one key between 09:59:50 and 10:00:10, under a fixed-window limit of 100 per minute. What explains it?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.