Compare the token bucket and the leaky bucket. When would you choose each?
Sketch your own answer first, then compare it with the one below.
A strong answer
A token bucket fills with tokens at a steady rate up to a maximum. Each request takes a token. If there is none, the request is rejected. The refill rate sets the long-run rate. The bucket size sets how large a burst can be, because a full bucket lets that many requests through at once.
A leaky bucket, used as a queue, puts requests in a queue that drains at a fixed rate. A burst is not rejected but delayed, and what comes out is smooth. Requests are rejected only if the queue is full. Used as a meter, it counts room left in the bucket, which is the same arithmetic as the token bucket.
So the real difference is what happens to a request that arrives early. The token bucket answers yes or no at once, and passes bursts straight through. The leaky bucket queue answers wait, and shapes traffic for whatever is downstream.
Choose the token bucket for an API where clients may legitimately burst and a prompt answer matters. Choose the leaky bucket when the thing behind it cannot take bursts, such as a connection to a fragile backend, and delay is acceptable. State is small for the token bucket and grows with queue size for the leaky one.
Sources
- Juha Heinanen, Roch Guerin (1999). A Single Rate Three Color Marker.
- Jonathan S. Turner (1986). New directions in communications (or which way to the information age?).