Skip to main content
PProfileMatch
← Path

Rate Limiting · Rate Limiting Algorithms: Token Bucket, Leaky Bucket and Sliding Window

0 XP
  1. 1 current stop: learning
  2. 2 upcoming
  3. 3 upcoming
  4. 4 upcoming
  5. 5 upcoming

Finish after stop 5

Stop 1 of 5 · Pump station at the trailhead

Trick to keep in mind+3 XP

Refill lazily: elapsed time times rate, capped, only when someone knocks.

A bucket does not need a clock ticking for every key. When a request arrives, work out how much time has passed since the last update, add that much refill, and clamp at capacity. This costs two numbers of state, cannot drift, and avoids a million timers. A periodic full reset would bring back the boundary problem.

tokens = min(capacity, tokens + (now - last) * rate)
last = now

Read it once, then a question tests whether the trick stuck.