The Token Bucket Algorithm
1. Motivation: Overcoming Leaky Bucket Rigidity
The Leaky Bucket algorithm enforces a rigid constant output rate, dropping packets if a burst arrives even if the network was completely idle for the preceding hour.
The Token Bucket Algorithm resolves this by allowing controlled traffic bursts:
- If a host is idle, it saves up "credits" in the form of Tokens.
- When a burst of data arrives, the host can consume its accumulated tokens to transmit at the maximum physical link rate () for a brief period before settling back to the sustained average rate ().
2. Operating Mechanism of Token Bucket
Operational Rules:
- Token Generation: Tokens are added to the bucket at a constant rate of tokens per second.
- Bucket Capacity (): The bucket has a finite capacity of tokens. If the bucket is full, newly generated tokens overflow and are discarded.
- Transmission Condition:
- To transmit a packet of length bytes, the sender must remove tokens from the bucket.
- If Tokens : The tokens are removed, and the packet is transmitted immediately at full link speed .
- If Tokens : The packet must wait in a queue until sufficient tokens accumulate, or be dropped.
3. Mathematical Formula for Maximum Burst Duration
When a full bucket of capacity encounters a massive burst of incoming traffic, the transmitter empties the bucket at maximum rate while new tokens continue to flow into the bucket at sustained rate ().
Let be the maximum duration of the burst at rate :
Equating the two:
- Maximum Burst Duration (): The longest time the system can continuously transmit at peak link rate .
- Total Burst Volume ():
4. Solved Numerical Example
Problem:
A token bucket regulator has a bucket capacity of () and a token generation rate of . The maximum physical link transmission speed is .
- Calculate the maximum burst duration () at peak rate .
- Calculate the total data volume transmitted during this peak burst.
Solution:
- Maximum Burst Duration ():
- Total Burst Volume:
Analysis: In seconds, the bucket delivered of data at peak speed: came from accumulated stored tokens, and () came from new tokens generated during the burst itself!
5. Comprehensive Comparison: Leaky Bucket vs. Token Bucket
| Parameter | Leaky Bucket Algorithm | Token Bucket Algorithm |
|---|---|---|
| Bucket Contents | Stores actual Packets / Data | Stores abstract Tokens (Credits) |
| Output Rate | Strictly Constant (Fixed leak rate ) | Variable / Bursty (Up to maximum link rate ) |
| Burst Handling | Smooths bursts into a constant trickle | Allows controlled bursts up to capacity |
| Token Generation | None (Direct packet queue) | Generated at fixed periodic rate |
| Idle Behavior | Discards empty ticks | Accumulates tokens up to capacity |
| Packet Dropping | Drops packets when bucket buffer is full | Drops tokens when full; delays or drops packets |
| Implementation | FIFO Queue + Rate limiter | Counter + Token Generator |
Frequently Asked Questions
QWhat is the fundamental difference between Leaky Bucket and Token Bucket?
The Leaky Bucket stores packets and enforces a strict, constant output rate regardless of bursts or link capacity. The Token Bucket stores tokens and allows idle nodes to save tokens, enabling them to transmit legitimate bursts at peak link speed when traffic arrives.
QState the formula for maximum burst time in Token Bucket.
, where is bucket token capacity, is maximum physical link rate, and is continuous token arrival rate.
QCan Token Bucket and Leaky Bucket be combined?
Yes. A common architecture places a Token Bucket before a Leaky Bucket: the Token Bucket allows controlled bursts into the node, and the Leaky Bucket caps the absolute peak rate sent into the core network.
QWhat happens to tokens when the Token Bucket is full?
Newly generated tokens overflow and are discarded, so an idle host can save credits only up to the bucket capacity .