Open In App

Token Bucket Algorithm

The Token Bucket algorithm is a popular and simple method used in computer networking and telecommunications for traffic shaping and rate limiting. It is designed to control the amount of data that a system can send or receive in some sort of period, ensuring that the traffic conforms to a specified rate.

It refers to traffic control mechanisms that seek to either differentiate performance based on application or network-operator requirements or provide predictable or guaranteed performance to applications, sessions, or traffic aggregates. It is something that data flow seeks to attain.



Need for Token Bucket Algorithm

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



It implies packet reached or not, information lost or not. Lack of reliability means losing a packet or acknowledgement, which entails re-transmission. Reliability requirements may differ from program to program. For example, it is more important that electronic mail, file transfer and internet access have reliable transmissions than telephony or audio conferencing.

Delay

It denotes source-to-destination delay. Different applications can tolerate delay in different degrees. Telephony, audio conferencing, video conferencing, and remote log-in need minimum delay, while delay in file transfer or e-mail is less important.

Jitter

Jitter is the variation in delay for packets belonging in same flow. High jitter means the difference between delays is large; low jitter means the variation is small. For example, if packets 0,1,2,3s arrive at 6,7,8,9s it represents same delay. Jitter would signify that packets departed at 0,1,2,3s reach destination at 4,6,10,15s. Audio and video applications don’t allow jitter.

Bandwidth

Different applications need different bandwidths. In video conferencing we need to send millions of bits per second to refresh a color screen while the total number of bits in an e-mail may not reach even a million.

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:

  1. FIFO Queuing
  2. Priority Queuing
  3. 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.

Leaky bucket algorithm shapes bursty traffic into fixed-rate traffic by averaging the data rate. It may drop the packets if the bucket is full. But this technique is very restrictive. It does not credit an idle host. For example, if a host is not sending for a while, its bucket becomes empty. If the host has bursty data, the leaky bucket allows only an average rate. The time when the host is idle is not take into account. On the other hand, token bucket algorithm allows idle hosts to accumulate credit for the future in the form of tokens. And that is how it overcomes the shortcoming of leaky bucket algorithm.

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 works

Steps Involved in Token Bucket Algorithm

Step 1: Creation of Bucket: An imaginative bucket is assigned a fixed capacity, known as “rate limit”. It can hold up to a certain number of tokens.

Step 2: Refill the Bucket: The bucket is dynamic; it gets periodically filled with tokens. Tokens are added to the bucket at a fixed rate.

Step 3: Incoming Requests: Upon receiving a request, we verify the presence of tokens in the bucket.

Step 4: Consume Tokens: If there are tokens in the bucket, we pick one token from it. This means the request is allowed to proceed. The time of token consumption is also recorded.

Step 5: Empty Bucket: If the bucket is depleted, meaning there are no tokens remaining, the request is denied. This precautionary measure prevents server or system overload, ensuring operation stays within predefined limits.

Advantage of Token Bucket over Leaky Bucket

Disadvantages of Token Bucket Algorithm

Frequently Asked Questions on Token Bucket Algorithm

What is leaky bucket algorithm?

Leaky bucket algorithm converts bursty traffic into a fixed rate traffic by maintaining a constant data output rate. Similar to water leaking from a bucket at a consistent rate, bursty data is stored in the bucket and sent out at an average rate. However, a drawback is that the leaky bucket imposes a constant data flow, disregarding the idle periods of the host and limiting adaptability to bursty data patterns.

Where is token bucket algorithm used?

Token bucket algorithm is generally used in packet-switched and telecommunication networks. It can be used to filter the requests depending on the levels of traffic currently present. It can find usage in functionalities like Spike Arrest and Rate Limiting operations.

What are the disadvantages of token bucket?

The main drawback of the token bucket algorithm is that it can be exploited by malicious or greedy clients. Since the client can make requests as fast as it wants, it can consume all the tokens in the bucket and starve other clients from accessing the API.


Article Tags :