Ch.17: Algorithms: Why Token Buckets Permit Bursts
Outline
Transcript
0:00 Imagine a payments on-call engineer watching a queue set to two jobs per second. It sat idle, then a backlog arrived and six jobs hit the database together. Connections filled and checkout became slow. The engineer says, "It says two a second. Set it to one and the spike should stop." They do. After the next quiet spell, another six-job clump lands. Is the limiter broken? No. The key is that "two per second" is not an instantaneous ceiling. A token bucket stores permissions up to a capacity while refilling at a rate.
0:33 Cloud Tasks warns that an idle queue may dispatch faster than its configured rate; I linked the docs below. Our six-job toy keeps its capacity at six when the rate changes. Hold on. We lowered the rate and still got six jobs together. Where were those six permissions hiding? Now trace the stored permissions. Suppose our toy bucket holds six tokens and gains two at each 1-second tick. One token lets one job start. For now, assume eight jobs can run at once; we'll test that separate cap later. While the queue sits idle, tokens accumulate to six, then stop.
1:08 The tick is just our drawing; real queues can refill differently. Ah, the dashboard shows zero recent jobs while the bucket is full. Quiet does not mean unarmed; the next arrival can spend the savings instantly. Next the backlog spends those stored tokens. Eight waiting jobs arrive together. Six start now; two wait. That's the database spike, and the limiter has followed its own rule. I think that rate graph would've fooled me. If this were an HTTP request limiter, would the extra requests wait too?
1:39 They might receive a 4:20-nine, meaning too many requests, instead of waiting. The admission rule is similar, but the fate of excess work depends on the system. Same rule, different fate. Now the bucket is empty; what happens to our two waiting jobs? With the bucket empty, the next refill tick returns two tokens and the last two jobs start. If new jobs keep arriving, this becomes the sustained pace: two more permissions each second in our toy. The bucket cannot repeat the initial burst immediately because it just spent its savings. So the timeline has a cliff and a slope.
2:16 The first jump came from capacity; the later two-at-a-time pace comes from refill. The operator changed the slope. Let's see if that touched the cliff. Now consider a second quiet spell in our toy with capacity held at 6: lower refill from two to one per second, then wait for the backlog. Six jobs start together again. So we made the queue slower and kept the spike. If the first hit hurts, shrink the burst or cap running jobs; if sustained load hurts, lower refill. But can a real queue change those independently?
2:50 My first thought was yes, but Cloud Tasks' API computes burst size from rate, so that six-job replay might change there. Its YAML configuration lets you set bucket size directly. Our toy holds it at six. What if those jobs each take 5 seconds? Then six database connections could stay occupied. I had pictured each token as a worker slot held until the job finishes, so those connections would at least stop growing. But if tokens come back before the original six finish, can two more start? Yes. A token is spent when work starts; it is not held until completion.
3:27 With the slower refill, another job begins at the first tick and the 8th at the second. Those first six are still busy for five full seconds, so all eight overlap by that second tick. If a separate concurrency limit were four, only four could start initially despite six ready tokens. That limit caps simultaneous work. Oh, that's the second trap. Now eight overlap even though the bucket released only two after the initial six. Refilling controls starts, not completions. I'd check for a concurrency cap before changing the rate again.
4:00 So the overlap completes the incident story: stored permissions created the first clump; long jobs kept the database crowded. Neither means the token bucket was broken. I'd put job starts beside active work and the connection-pool graph. Then let the queue go idle before sending the backlog; a steady-load test keeps draining the bucket and may miss the spike. Thanks for listening to Learning Podcasts.