The Token Bucket Algorithm is a traffic control method where tokens are added to a bucket at a fixed rate (up to a maximum capacity), and packets can be transmitted only if enough tokens are available.
- Controls the average sending rate of traffic entering the network
- Supports burst transmission within the bucket capacity while keeping long-term limits
- Used in QoS, traffic shaping, and traffic policing
Need for the Token Bucket Algorithm
Token Bucket is needed because fixed-rate limiting forces traffic to move at a constant speed, while real network traffic is naturally bursty. It provides controlled flexibility so short spikes don’t get blocked, while long-term bandwidth limits are still enforced.
- Networks have limited bandwidth, so uncontrolled traffic can cause congestion and packet loss
- Real traffic (web, video, APIs) arrives in bursts, not at a steady rate
- Fixed-rate limiters may delay or drop packets even during short, legitimate spikes
- Applications often need brief high-speed sending to stay responsive
- Unrestricted bursts can overload the network if not controlled
- Token Bucket balances burst allowance with long-term average rate control
Key Terminology and Parameters
- Token: A permission unit required to transmit data; a packet can be sent only if enough tokens are available.
- Bucket: A logical storage that holds tokens until they are used.
- Token Generation Rate (r): Tokens added per second; controls the long-term average sending rate.
- Bucket Capacity (B): Maximum tokens the bucket can store; defines the maximum burst size.
- Token Consumption Rule: Tokens are deducted based on transmitted data, commonly 1 token per byte (or sometimes per packet), depending on configuration.
Working
- Tokens are added to the bucket at a constant rate (r) and stored up to the maximum capacity (B)
- Idle periods allow tokens to accumulate, enabling future burst transmission
- When a packet arrives, the system checks whether enough tokens are available
- If sufficient tokens exist, they are deducted and the packet is sent immediately
- If tokens are unavailable, the packet is delayed, queued, or dropped until new tokens are generated

Steps Involved in Token Bucket Algorithm
1. Bucket Creation: A logical bucket with a fixed capacity (rate limit) is defined, representing the maximum number of tokens it can hold.
2. Token Refill: Tokens are added to the bucket at a constant rate until the bucket reaches its capacity.
3. Request Arrival: When a request (or packet) arrives, the system checks whether tokens are available.
4. Token Consumption: If tokens are present, one token is removed, and the request is allowed to proceed. The time of consumption is also recorded.
5. Bucket Empty: If no tokens remain, incoming requests are denied or delayed. This prevents overload and keeps the system within the defined rate limit.
Flow Characteristics
Token Bucket primarily manages bandwidth and traffic rate, but it indirectly influences other flow characteristics that affect overall network performance.
.png)
- Reliability: Not directly controlled by Token Bucket; reliability is mainly handled by transport protocols like TCP.
- Delay: By regulating traffic, it reduces excessive queuing, which can help lower transmission delay.
- Jitter: Smoother traffic flow minimizes large timing variations, improving real-time application performance.
- Bandwidth: Controls the average transmission rate and allows limited bursts, helping prevent sudden network overload.
Token Bucket vs Leaky Bucket Algorithm
Token Bucket Algorithm | Leaky Bucket Algorithm |
|---|---|
It depends on tokens. | It does not depend on tokens. |
If bucket is full, token is discarded but not the packet. | If bucket is full, then packets are discarded. |
Packets can only transmit when there are enough tokens. | Packets are transmitted continuously. |
Allows large bursts to be sent at faster rate. Bucket has maximum capacity. | Sends the packet at a constant rate. |
The bucket holds tokens generated at regular intervals of time. | When the host has to send a packet , packet is thrown in bucket. |
If there is a ready packet , a token is removed from Bucket and packet is send. | Bursty traffic is converted into uniform traffic by leaky bucket. |
If there is no token in the bucket, then the packet cannot be sent. | In practice bucket is a finite queue outputs at finite rate. |
Advantages
- Supports burst traffic while maintaining a controlled long-term average rate
- Reduces sudden congestion by limiting how fast traffic can build up over time
- Uses bandwidth efficiently by allowing unused capacity to be used later as bursts
- Provides flexible rate control through tunable parameters like token rate (r) and bucket size (B)
- Commonly used in QoS, traffic shaping, and policing to manage different traffic classes
Disadvantages
- Does not guarantee zero packet loss, especially during heavy congestion
- Requires proper buffering/queue management to avoid drops when tokens are insufficient
- Bursts can still create short-term congestion or jitter if the network is already busy
- Needs correct tuning of token rate (r) and bucket size (B); wrong values can cause either poor performance or weak control
Applications
- Used in routers and switches to enforce QoS policies for different traffic classes
- Applied in traffic shaping to smooth outgoing traffic and reduce congestion
- Used by ISPs to enforce user bandwidth plans and prevent one user from dominating the link
- Common in video streaming and live media to handle short bitrate spikes without breaking average limits
- Used in cloud networking and APIs for rate limiting, fair usage control, and protection against traffic spikes
- Example: A token bucket is configured with a token rate (r) = 5 Mbps and a bucket capacity (B) = 10 MB. If the sender stays idle, tokens accumulate until the bucket becomes full. When a burst occurs, the sender can transmit up to 10 MB quickly (using the stored tokens), and once the stored tokens are consumed, transmission continues at the steady long-term rate of 5 Mbps.