Token Bucket algorithm is a widely used method in computer networks for traffic shaping and rate limiting. It controls the amount of data transmitted over time, ensuring that traffic follows a specified rate while allowing flexibility for bursts when tokens are available.
Need for Token Bucket Algorithm
- Video and audio conferencing require a bounded delay and loss rate.
- Video and audio streaming requires a bounded packet loss rate, it may not be so sensitive to delay.
- a -critical applications (real-time control) in which bounded delay is considered to be an important factor.
- Valuable applications should provide better services than less valuable applications.
Flow Characteristics of Token Bucket Algorithm
Four types of characteristics are attributed to a flow: reliability, delay, jitter, and bandwidth.
Types of Characteristics for Quality of Service- Reliability: Ensures data is delivered without loss. Needed in email, file transfer, web access. Voice/video calls can handle small losses.
- Delay: Time taken for data to reach the destination. Real-time apps (calls, video meetings) need low delay. Emails/files can tolerate delay.
- Jitter: Variation in packet arrival times. Low jitter = smooth playback, high jitter = choppy audio/video. Important for calls and streaming.
- Bandwidth: Data capacity per second. Video calls need high bandwidth; email needs very little.
Techniques to Improve QoS
There are several ways to improve QoS like Scheduling and Traffic shaping ,We will see each and every part of this in brief.
Scheduling: Packets from different flows arrive at a switch or router for processing. A good scheduling technique treats the different flows in a fair and appropriate manner. Three scheduling techniques are:
- FIFO Queuing
- Priority Queuing
- Weighted Fair Queuing
To learn more about the scheduling techniques visit this article on packet queuing and dropping.
Traffic Shaping: It is a mechanism to control the amount and the rate of the traffic sent to the network. The techniques used to shape traffic are: leaky bucket and token bucket.
Difference Between Token Bucket Algorithm and Leaky Bucket Algorithm
The differences between leaky and token bucket algorithm are:
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. |
Working of Token Bucket Algorithm:
It allows bursty traffic at a regulated maximum rate. It allows idle hosts to accumulate credit for the future in the form of tokens. The system removes one token for every cell of data sent. For each tick of the clock the system send n tokens to the bucket. If n is 100 and host is idle for 100 ticks, bucket collects 10000 tokens. Host can now consume all these tokens with 10 cells per tick.
Token bucket can be easily implemented with a counter. The token is initiated to zero. Each time a token is added, counter is incremented to 1. Each time a unit of data is sent, counter is decremented by 1. When the counter is zero, host cannot send data.
Process depicting how token bucket algorithm worksSteps Involved in Token Bucket Algorithm
Step 1: Bucket Creation: A logical bucket with a fixed capacity (rate limit) is defined, representing the maximum number of tokens it can hold.
Step 2: Token Refill: Tokens are added to the bucket at a constant rate until the bucket reaches its capacity.
Step 3: Request Arrival: When a request (or packet) arrives, the system checks whether tokens are available.
Step 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.
Step 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.
Advantage of Token Bucket over Leaky Bucket
- Token Handling: When the bucket is full, excess tokens are discarded, not packets. In contrast, leaky bucket discards packets when overflow occurs.
- Support for Bursts: Token bucket allows large bursts of data at high speed, while leaky bucket enforces a constant transmission rate.
- Traffic Shaping Control: It offers predictable shaping by adjusting token arrival rate and maximum token count, providing finer control than leaky bucket.
- Quality of Service (QoS): Different token rates can prioritize various traffic types, enabling better QoS, which leaky bucket lacks.
- Efficiency for High-Speed Data: Suitable for streaming and high-throughput transfers, as bursts are transmitted efficiently.
Disadvantages of Token Bucket Algorithm
- Token Accumulation: Tokens are generated at a fixed rate even when traffic is absent, leading to buildup and inefficient usage.
- Increased Latency: If the bucket is empty, packets must wait for new tokens, causing delay and possible packet loss.
- Complex Implementation: Managing different token generation rates for multiple traffic types increases configuration difficulty.
- Inefficient with Large Bursts: Allowing sudden bursts of data may waste bandwidth and cause congestion, unlike the leaky bucket which enforces smoother traffic flow.
Explore
Computer Network Basics
Physical Layer
Data Link Layer
Network Layer
Transport Layer
Session Layer & Presentation Layer
Application Layer
Advanced Topics
Practice