27/32
Part 6 of 6 · Follow-upsLearn and do

What if the limit is too big to log every request?

The sliding log holds one timestamp per allowed request that still counts, so up to limit per key. With a limit of 1,000 a second over a 60-second window, that's 60,000 timestamps for one busy client.

When that's too much, approximate. Keep two counters per key: one for the previous fixed slot and one for the current slot. Count the previous slot in proportion to how much of it the window still covers. Say a client sent 84 requests in the previous minute and 30 so far in this one, and you're 36 seconds in:

Predict

The previous minute had 84 requests, this one has 30 so far, and you're 36 seconds in. Counting the previous minute in proportion, what's the estimate for the last 60 seconds?

Pick an answer above to see the rest