Leaky Bucket and Token Bucket Algorithms for Traffic Shaping and Rate Limiting

Leaky Bucket and Token Bucket Algorithms for Traffic Shaping and Rate Limiting

Verified Sources
Nov 4, 2026

The Leaky Bucket and Token Bucket algorithms are widely used to control how data flows through networks. Both limit the average rate of sending/processing, but they differ in how they handle bursts.

At a high level:

  • Leaky Bucket enforces a steady “drip” (constant release) with optional buffering.
  • Token Bucket allows sending in bursts as long as tokens are available, while still limiting the long-term average rate.

A helpful mental model is:

  • With leaky bucket, packets “wait in a bucket” that drains at a fixed rate.
  • With token bucket, packets “spend tokens”; tokens regenerate at a fixed rate, enabling short-term bursts.

Key parameters you’ll see in both algorithms:

  • RR: allowed average rate (e.g., tokens added per second)
  • BB: bucket capacity (max queue size or max tokens)
  • Time unit: discretized intervals or continuous time (implementation choice)

Token Bucket vs Leaky Bucket (Traffic Shaping Overview)

Core idea and state representation

Leaky bucket (queue + constant drain)

In leaky bucket, you conceptually maintain a buffer of capacity BB:

  • When a packet/request arrives:
    • If buffer is not full, it is queued.
    • If buffer is full, the packet/request is dropped (or equivalently, not admitted).
  • A “leak” process continuously removes data from the buffer at a fixed rate RR.

This makes the output rate approximately constant, which is why it’s popular for traffic shaping.

Token bucket (tokens + spending)

In token bucket, you maintain:

  • A token counter TT with maximum capacity BB
  • Tokens are added at rate RR up to BB
  • When a packet/request arrives:
    • If T≥1T \ge 1 (or T≥T \ge packet cost), it is allowed and tokens are consumed.
    • Otherwise, it is delayed (wait for tokens) or rejected/dropped depending on policy.

This is why token bucket can allow bursts up to the bucket size.

Terminology keywords (for this section)

  • Rate limiting
  • Traffic shaping
  • Burst tolerance

Leaky Bucket Algorithm (Admission + Constant Output)

  1. 1
    Step 1

    Set average output rate RR and buffer capacity BB. Decide whether overflow causes drops or blocking.

  2. 2
    Step 2

    Start with an empty buffer (queue length q=0q=0).

  3. 3
    Step 3

    If q<Bq < B, enqueue the packet and set q←q+1q\leftarrow q+1 (or add packet size if variable). If q=Bq=B, drop the packet.

  4. 4
    Step 4

    Every time interval (or continuously), remove from the queue at rate RR: if q>0q>0, dequeue and set q←q−1q\leftarrow q-1 according to the drain schedule.

  5. 5
    Step 5

    The departure process is smoothed: long-term average outflow approaches RR, and instantaneous output is limited by the leak rate and available buffered data.

Token Bucket Algorithm (Token Generation + Spend to Transmit)

  1. 1
    Step 1

    Set token generation rate RR and maximum tokens BB. Decide whether you ‘delay’ or ‘drop’ when tokens are insufficient.

  2. 2
    Step 2

    Start with T=BT=B (common for immediate burst) or T=0T=0 (strict warm-up), depending on policy.

  3. 3
    Step 3

    Between arrivals, increase TT by R⋅ΔtR\cdot \Delta t, but clamp to BB: T←min⁡(B, T+R⋅Δt)T\leftarrow \min(B,\,T+R\cdot\Delta t).

  4. 4
    Step 4

    Let each packet require cc tokens (often c=1c=1). If T≥cT\ge c, admit and consume tokens T←T−cT\leftarrow T-c. Otherwise, either delay until tokens accumulate or drop/reject.

  5. 5
    Step 5

    If arrivals come in bursts, the bucket allows them as long as cumulative tokens permit. The long-term average send rate cannot exceed RR.

Side-by-side behavior (burst handling and smoothing)

AspectLeaky BucketToken Bucket
Long-term average rateEnforced by constant drain at RREnforced by token generation at RR
Burst handlingLimited by buffer then constant leak; burst is “smoothed”Naturally allows bursts up to token capacity BB
Queueing vs admissionTypically queues up to BB, drops on overflowCan admit immediately when tokens exist; else delay or drop
Output shapeMore constant/regular departure rateMore bursty (but bounded by tokens)
Parameters meaningBB is max queued backlog; RR is leak rateBB is max token storage; RR is refill rate

Practical interpretation

  • If your goal is strictly smooth output, leaky bucket is a strong fit.
  • If you want to permit occasional bursts while still capping the long-term rate, token bucket is typically preferred.

Keywords for this section

  • Bucket capacity
  • Token refill rate
  • Drain rate

Implementation detail: discrete vs continuous time

In real systems you’ll implement token generation/draining with a timer or on each request. Either way, ensure you cap at BB and compute available tokens/queue correctly from elapsed time.

Overflow policy changes the semantics

For leaky bucket, overflow usually drops or blocks. For token bucket, insufficient tokens can cause delay or rejection. Pick the policy to match your SLA and client behavior.

When to Choose Each Algorithm

Use Leaky Bucket

Constant-rate shaping goal

Prefer stable departure spacing to reduce jitter and smooth downstream load."

Use Token Bucket

Burst-tolerant admission goal

Prefer immediate allowance for short bursts while controlling long-term average rate."

Combine with per-flow buckets

Need bounded fairness under load

Apply buckets per user/flow to avoid one source consuming all capacity."

Select drop vs delay

Operational constraints

Delay increases latency; drop increases loss. Choose based on requirements."

Variable packet sizes (important extension)

Both algorithms can be generalized from “1 unit per packet” to packet sizes/costs:

  • In token bucket, each packet costs cc tokens proportional to its size.
  • In leaky bucket, each packet adds cc units to backlog, and draining removes tokens/units at rate RR.

Then:

  • Token check becomes: admit if TgecT \\ge c
  • Leaky bucket overflow becomes: queue if q+cleBq + c \\le B, else drop

This is common in routers and traffic engineering where “packet weight” matters.

Mathematical framing (conceptual)

Let token count evolve as: T(t)=minleft(B,,T(t0)+intt0tR,dtright)T(t) = \\min\\left(B,\\,T(t_0) + \\int_{t_0}^{t} R\\,dt\\right)

Admission with cost cc occurs only if T(t)gecT(t) \\ge c.

(Implementation usually discretizes the integral using elapsed time.)

Common FAQs and edge cases

Behavior Comparison Summary

Qualitative scoring: higher means more of that property in typical implementations.

Leaky vs Token Bucket—Self Check

1 / 5
Question · Term

Leaky bucket: what happens when the queue is full?

Click to reveal
Answer · Definition

The new arriving packet/request is typically dropped (or blocked, depending on policy).

Knowledge Check

Question 1 of 4
Q1Single choice

In a leaky bucket algorithm, what primarily determines the departure (output) rate when the bucket is not empty?