A token bucket holds 20 tokens and refills at 5 a second. A client has been idle for a minute, then sends 30 requests at once. How many get through, and how many can it send each second after that?
Sketch your own answer first, then compare it with the one below.
A strong answer
After a minute idle the bucket is full, so it holds 20 tokens. The 30 requests arrive in the same instant. The first 20 each take a token and are admitted. The other 10 find the bucket empty and are rejected.
From then on the refill sets the pace. At 5 tokens a second the client can send 5 requests a second, forever. If it sends 5 a second the bucket stays near empty. If it sends fewer, say 2 a second, the unused tokens pile up again at 3 a second, and after about 7 seconds the bucket is full and the client can burst 20 once more.
So the two settings do different jobs. The bucket size of 20 is the largest burst the service has agreed to take. The refill of 5 is the average rate it can sustain. If 20 at once is too much for the backend, the fix is a smaller bucket. If 5 a second is too much, the fix is a lower refill.
One detail: the 10 rejected requests got an answer immediately. They did not wait for tokens. Waiting for tokens is the leaky bucket queue, a different design.
Sources
- Juha Heinanen, Roch Guerin (1999). A Single Rate Three Color Marker.