Rate Limiter Algorithms (Token Bucket, Leaky Bucket) 00:00:01 A ten-token bucket refills by the second A partner may burst with ten saved tokens and earns two more each second. Watch the balance between arrivals, including fractional earnings that cannot yet pay for a whole request. The bucket starts with 10 tokens. One token is taken. 9 are left. One tenth of a second earns two tenths of a token. The balance reaches nine point two, and admitting five requests leaves four point two. After the intervening group spends three tokens, another seven tenths of a second brings the balance to two point eight. At one point one seconds the balance reaches three whole tokens. Three requests consume them and leave the bucket empty. At one point two seconds only two tenths of a token are available. The missing eight tenths require four tenths of a second at this refill rate. After a balance of one fifth of a token, four tenths of a second at two tokens per second supplies the missing four fifths. Capacity limits the savings after a long idle period. 00:01:08 A queue shapes the output rhythm A downstream machine can accept one item per second. Give its waiting area five places and trace a six-item burst before the next scheduled departure. Nothing waits. Call 1 joins. Calls 2 to 5 join. The queue is full and call 6 is refused. Call 7 joins. Full again. Call 8 comes 0.1 s later and is refused. One more leaves at 2 s and one at 3 s. The service never sees more than one a second. A leaky queue can smooth departures while increasing waiting time. Bound the queue and the caller's deadline, and define what response an item receives when no waiting place is available. 00:01:55 A minute boundary hides a double burst A caller sends one hundred requests at second fifty-nine, then waits across the fixed minute boundary. Predict how many may pass over the two-second span. One hundred calls arrive at second fifty-nine. The bucket reaches one hundred and they all pass. One more in the same minute is refused. At the start of the next minute the counter resets to zero. One second into the next minute, another hundred pass. Across two seconds, two hundred requests were admitted. Each individual fixed window respects its hundred-request cap, yet two hundred requests can cross in two seconds. The chosen window boundary is part of the algorithm's observable behavior. 00:02:37 Retire one ten-second slice Divide the trailing minute into six count buckets. As time advances, expiring buckets leave the sum and new accepted requests enter it. Keep the running total beside the buckets. All parts are 0. In the first ten seconds. 20, 25, 15 and 10 follow. Together 95. The oldest part, 10, drops out. 5 new calls: 90. 20 drops out, leaving 75. 30 calls come: 25 pass and 5 are refused. 25, 15 and 10 have dropped out. 10, 5 and 8 came in: 73. Subwindow counting uses bounded storage. Near a bucket boundary the approximation depends on how events were distributed within that bucket, so choose the granularity for the required accuracy. 00:03:31 Estimate how much of the previous minute remains The previous minute contains seventy requests and the current minute contains thirty. At fifteen seconds into the current minute, calculate the weight assigned to the previous count. 70 calls in the previous minute, 30 in this one. 15 seconds have passed. 75% of the previous minute lies inside the last 60 seconds: 70 times 0.75 equals 52.5. 52.5 plus 30 equals 82.5. Below 100, so the call passes. With one hundred calls at second fifty-nine, one second into the next window their weighted contribution is about ninety-eight point three. The trace illustrates the configured estimated-count admission rule. Three quarters of seventy is fifty-two and a half. Adding thirty yields an estimated eighty-two and a half, which can differ from the true count if earlier arrivals were clustered.