Back to the 2023 paper

Module 4: Transport Layer & Congestion Control

20237m

Explain leaky bucket and token bucket algorithm.

Worked SolutionAI Assisted

Both are traffic-shaping techniques used for congestion control. They regulate the rate at which packets enter the network.

1. Leaky bucket algorithm

Idea: a bucket with a small hole at the bottom. Water (packets) may arrive at any rate, but it leaks out at a constant rate. If the bucket is full, incoming water overflows and is discarded.

  bursty input  →  [ bucket (buffer) ]  →  constant-rate output
                          ↓ overflow
                       discarded

Working:

  1. Incoming packets are placed in a finite queue (the bucket).
  2. Every clock tick, packets are sent out at a fixed rate (e.g. one packet or n bytes per tick).
  3. If the queue is full, arriving packets are dropped.

Properties:

  • Converts bursty traffic into smooth, constant-rate traffic.
  • Output rate never exceeds the leak rate, however large the burst.
  • Simple to implement (a FIFO queue).
  • Drawback: cannot exploit idle time, so a host that was quiet for a long time still cannot send a burst, and it may drop packets even when the network is free.

Byte-counting version (variable-size packets): each tick, send packets while the total bytes sent ≤ n.

2. Token bucket algorithm

Idea: the bucket holds tokens, generated at a constant rate (one every ΔT seconds) up to the bucket capacity C. To send a packet, the host must remove a token (or tokens equal to the packet size). Unused tokens accumulate, so after an idle period the host can send a burst.

  tokens generated at rate r  →  [ bucket capacity C ]
  packet arrives → token available? yes: send, remove token / no: wait (or drop)

Working:

  1. Tokens are added at rate r; extra tokens beyond capacity C are discarded.
  2. An arriving packet needs a token to be sent; if none is available it waits (or is dropped/marked).
  3. Idle hosts save tokens, allowing bursts of up to C packets at full link speed.

Maximum burst length: S = C / (M − r), where M is the maximum output rate and r the token rate.

Example: r = 1 token/ms, C = 5. After 5 ms of idleness the host may send 5 packets back-to-back, and afterwards only 1 packet per ms.

Comparison

Basis Leaky bucket Token bucket
Output rate Constant (fixed) Variable; bursts allowed up to bucket size
Idle periods Not exploited Saved as tokens for later bursts
What is stored Packets Tokens
Bucket full Packets are discarded Tokens are discarded, not packets
Burst handling Smooths bursts completely Permits controlled bursts
Best for Strictly uniform traffic Bursty traffic with an average-rate limit

Simple JavaScript sketch of the token bucket

class TokenBucket {
  constructor(capacity, ratePerSec) {
    this.capacity = capacity; this.rate = ratePerSec;
    this.tokens = capacity;   this.last = Date.now();
  }
  allow(cost = 1) {
    const now = Date.now();
    this.tokens = Math.min(this.capacity, this.tokens + (now - this.last) / 1000 * this.rate);
    this.last = now;
    if (this.tokens >= cost) { this.tokens -= cost; return true; }   // send
    return false;                                                     // wait/drop
  }
}

Similar questions

AI LearningWeb Platform

Coursify — Personal AI Course Generator

Struggling with a difficult subject or concept? Turn any engineering topic into a structured course with interactive lessons, quizzes, and research-backed explanations.