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.
A strict rule permits at most 100 requests in every rolling minute. Is a fixed-minute counter enough?
No. A client can use 100 requests at the end of one minute and 100 at the start of the next. Use a rolling-window algorithm with the precision needed for the strict guarantee.
Use this as a guide for self review; written answers are not graded.Keep in mind
- Fixed windows can admit boundary bursts.
- Sliding logs favor precision; sliding counters favor lower memory.