Try
↑ ↓ move↵ openesc close

Interview prep / Rate limiting algorithms

Mid · Rate limiting algorithms · System design interview question

How do the sliding window log and the sliding window counter differ, and what does each cost?

Sketch your own answer first, then compare it with the one below.

A strong answer

The sliding window log stores a timestamp for every admitted request. To decide, it drops timestamps older than one window and counts the rest. It is exact: in any span of one window it never admits more than the limit. Its cost is memory and work. State grows with the limit for every caller, and each decision trims and scans a list.

The sliding window counter keeps just two counts, for the current and the previous fixed window. It estimates the count over the last window as the previous count times the share of that window still in view, plus the current count. It assumes the previous window's requests were spread evenly. If they were bunched, the estimate is off, in either direction.

So the log trades memory for exactness, and the counter trades a small error for a few numbers per caller. For a very large number of callers the counter or a token bucket is usually the practical choice. For a hard cap, such as a billed quota, the log is the safer one.

Per caller, the log stores up to the limit in timestamps. The counter stores two counts and a window start. Its error comes entirely from the even-spread assumption.

Sources

Learn it by running it

The rate limiting algorithms lesson explains the mechanism behind this question, and its lab lets you change the parameters and watch the effect.

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms