Transport Layer · Computer Networks

QoS Improving Techniques — Leaky Bucket and Token Bucket Algorithm

Watch lectures

QoS Improving Techniques: Leaky Bucket and Token Bucket Algorithms


1. Traffic Shaping Fundamentals

Computer data traffic is inherently bursty: applications alternate between long periods of transmission silence and sudden, high-volume packet bursts. If bursty traffic enters an unmanaged network, intermediate router queues instantly overflow, resulting in buffer drops, excessive retransmissions, and network congestion.

Traffic Shaping is a traffic management technique that regulates the rate and volume of packets entering a network, smoothing bursty traffic into a predictable, well-behaved profile.

                            TRAFFIC SHAPING TECHNIQUES
                                        |
         +------------------------------+------------------------------+
         |                                                             |
   LEAKY BUCKET ALGORITHM                                    TOKEN BUCKET ALGORITHM
 (Strictly constant output rate)                           (Allows controlled traffic bursts)

2. The Leaky Bucket Algorithm

The Leaky Bucket uses the physical analogy of a bucket with a small hole in the bottom:

  Bursty Inflow (Packets arrive in bursts)
       | | |    | |
       v v v    v v
   +--------------------+
   |  LEAKY BUCKET      |  <-- Capacity C (Buffer size)
   |  ~~~~~~~~~~~~~~~~~ |
   |  ~~~~~~~~~~~~~~~~~ |
   +---------+----------+
             |
             v
   Constant Outflow Rate R (Fixed leak: drops at steady pace)
   (Smooth, non-bursty output stream)

Operational Rules:

  1. FIFO Buffer of Capacity CC: Stores incoming packets up to limit CC.
  2. Constant Leak Rate RR: Packets are dequeued and transmitted onto the network at a strictly constant rate of RR bytes/sec, regardless of the arrival rate.
  3. Packet Dropping: If a burst arrives when the bucket is full (>C> C), excess packets overflow and are discarded.
  4. Limitation: Inflexible. It cannot burst at higher speeds even when the communication link is completely idle, introducing artificial transmission delays for bursty applications.

3. The Token Bucket Algorithm

The Token Bucket Algorithm overcomes the inflexibility of the Leaky Bucket by allowing controlled bursts of traffic up to the maximum physical link rate (MM):

  Tokens generated at constant rate R (tokens/sec)
                      |
                      v
            +--------------------+
            |    TOKEN BUCKET    |  <-- Capacity C tokens (Max credits saved)
            |   (•)  (•)  (•)    |
            +---------+----------+
                      |
                      | 1 Byte consumes 1 Token
                      v
  Packets Arrive ──> [ Gate ] ───> Transmitted into Network
  (Bursty Queue)    (Consumes tokens)(Can burst at full link speed M)

Operational Rules:

  1. Token Generation: Abstract tokens ("credits") are added to the bucket at a constant rate of RR tokens per second.
  2. Bucket Capacity (CC): The bucket can hold at most CC tokens. When full, newly generated tokens are discarded.
  3. Transmission Rules:
    • To transmit a packet of bb bytes, bb tokens must be removed from the bucket.
    • If Tokens ≥b\geq b: The packet is transmitted immediately at the maximum link rate MM.
    • If Tokens <b< b: The packet must wait for more tokens to generate, or be dropped.

4. Mathematical Derivation of Maximum Burst Time

When a full bucket of capacity CC encounters a massive burst of incoming data, the sender transmits at the peak link rate MM while new tokens continue to arrive at rate RR (M>RM > R).

Let TT be the maximum duration of the burst:

Data Transmitted during Burst=M×T\text{Data Transmitted during Burst} = M \times T
Tokens Available during Burst=C+(R×T)\text{Tokens Available during Burst} = C + (R \times T)

Equating data transmitted to available tokens:

M⋅T=C+R⋅TM \cdot T = C + R \cdot T
M⋅T−R⋅T=CM \cdot T - R \cdot T = C
(M−R)⋅T=C(M - R) \cdot T = C
T=CM−R\mathbf{T = \frac{C}{M - R}}
  • Maximum Burst Volume (Vmax⁡V_{\max}):
Vmax⁡=M×T=C⋅MM−RV_{\max} = M \times T = \frac{C \cdot M}{M - R}

5. Solved Step-by-Step Numerical

Problem:

A token bucket regulator has a bucket capacity of C=1 MegabyteC = 1\text{ Megabyte} (8 Megabits8\text{ Megabits}), a token arrival rate of R=2 MbpsR = 2\text{ Mbps}, and the physical network link operates at a maximum transmission speed of M=10 MbpsM = 10\text{ Mbps}.

  1. Calculate the maximum burst duration (TT) at peak rate 10 Mbps10\text{ Mbps}.
  2. Calculate the total data volume transmitted during this peak burst.

Solution:

  • C=8 MegabitsC = 8\text{ Megabits}
  • R=2 MbpsR = 2\text{ Mbps}
  • M=10 MbpsM = 10\text{ Mbps}
  1. Maximum Burst Duration (TT):
T=CM−R=8 Megabits10 Mbps−2 Mbps=88=1.0 secondT = \frac{C}{M - R} = \frac{8\text{ Megabits}}{10\text{ Mbps} - 2\text{ Mbps}} = \frac{8}{8} = \mathbf{1.0\text{ second}}
  1. Total Transmitted Burst Volume:
V=M×T=10 Mbps×1.0 s=10 Megabits=1.25 MegabytesV = M \times T = 10\text{ Mbps} \times 1.0\text{ s} = \mathbf{10\text{ Megabits} = 1.25\text{ Megabytes}}

6. 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 Data Rate Strictly Constant (Fixed rate RR) Variable / Bursty (Up to link rate MM)
Traffic Bursts Smooths bursts into a constant trickle Allows controlled bursts up to capacity CC
Idle Periods Discards idle ticks Accumulates tokens up to capacity CC
Packet Loss Occurs when buffer queue fills (>C> C) Packets wait or drop when tokens run out
Hardware Core FIFO Queue + Rate limiter Simple counter register

7. Key Exam Questions & Takeaways

  1. Why does the Token Bucket algorithm handle bursty traffic better than the Leaky Bucket algorithm?
    • The Leaky Bucket forces packets out at a strictly constant rate even when the network has idle bandwidth. The Token Bucket accumulates credits during idle periods, allowing the sender to burst at maximum wire speed when needed.
  2. What happens to generated tokens when the Token Bucket is full?
    • Newly generated tokens overflow and are discarded. The bucket capacity CC sets the upper bound on the maximum burst credit.
  3. State the formula for maximum burst time in a Token Bucket.
    • T=CM−RT = \frac{C}{M - R}, where CC is bucket capacity, MM is peak transmission speed, and RR is continuous token generation rate.
Budget AppAndroid App

Pocketly on Android

Log daily food, travel, and hostel expenses instantly. Set monthly spending limits and see where your money goes without complex spreadsheets.

← All Computer Networks modules