← Lessons

quiz vs the machine

Gold1420

System Design

The Sliding Window Counter

A hybrid that blends two fixed windows to approximate a sliding window cheaply.

5 min read · core · beat Gold to climb

The middle path

The sliding window counter approximates the exact log while keeping only two integers. It blends the current fixed window count with a weighted share of the previous window count, based on how far the clock has moved into the current window.

The estimate is the current window count plus the previous window count multiplied by the fraction of the previous window still inside the rolling view. As time advances, the previous window weight shrinks toward zero.

Why it works

  • It smooths the edge burst of the fixed window because the prior window still contributes weight near the seam.
  • It needs only two counters per client, not a full timestamp list.
  • The error versus an exact log is small under steady traffic and acceptable for most public APIs.

The tradeoff

It assumes requests are spread evenly across the previous window. Heavily skewed traffic inside that window makes the linear estimate slightly off, but rarely enough to matter.

Key idea

The sliding window counter weights the previous window into the current count to approximate a sliding window with only two integers.

Check yourself

Answer to earn rating on the learn ladder.

1. What does the sliding window counter store per client?

2. What assumption introduces its small error?