DrawCode Algorithm Visualizer • Rate Limiting • Easy

Leaky Bucket Algorithm

Tags: Rate Limiting, Queue, Traffic Shaping, System Design, Networking

1. One-Liner

The Leaky Bucket algorithm queues incoming requests and processes them at a constant rate — like water leaking from a bucket at a steady drip.


2. The Problem It Solves

Token Bucket allows bursts — if the bucket is full, many requests can go through at once. But sometimes you need a perfectly smooth, constant output rate:

  • Video streaming — data must flow at a steady bitrate, no spikes
  • Network traffic shaping — packets must be sent at a fixed rate
  • API output smoothing — downstream services can only handle N requests/second, no more
  • Leaky Bucket guarantees that no matter how bursty the input is, the output is always smooth and constant.


    3. The Core Idea

    Imagine a real bucket with a hole at the bottom:

  • Water (requests) pours in from the top — at any rate, even a flood
  • Water leaks out from the hole at a constant, fixed rate
  • If you pour in too much and the bucket overflows, that water (request) is lost (rejected)
  • Key difference from Token Bucket:

  • Token Bucket controls the input rate (how fast you can send)
  • Leaky Bucket controls the output rate (how fast the server processes)

  • 4. How It Works (Step-by-Step)

    StepWhat Happens
    1. Request arrivesA new request comes to the rate limiter
    2. Drain firstCalculate how many items have leaked (been processed) since last check
    3. Remove drainedRemove those items from the front of the queue (FIFO)
    4. Check capacityIs there room in the queue? (queue_size < capacity)
    5a. YES → AcceptAdd the request to the back of the queue
    5b. NO → RejectQueue is full — overflow — reject the request
    6. ProcessingItems at the front are constantly being drained at the leak rate

    Key insight: The leak rate is constant. Even if 100 requests arrive at once, the server only processes them at the fixed leak rate. The queue absorbs the burst.


    5. Dry Run Example

    Setup: capacity = 4, leak_rate = 2/sec

    TimeActionQueue BeforeResultQueue After
    0.0sRequest0✅ Accept1
    0.1sRequest1✅ Accept2
    0.2sRequest2✅ Accept3
    0.3sRequest3✅ Accept4
    0.4sRequest4 (0 leaked)❌ Reject4
    0.5sRequest4 → 3 (1 leaked)✅ Accept4
    1.0sRequest4 → 3 (1 leaked)✅ Accept4
    2.0sRequest4 → 0 (4 leaked)✅ Accept1

    Notice: at t=0.4s the queue is full so the request is rejected. But by t=0.5s, one item has leaked out, making room for a new request. The output is always steady at 2/sec.


    6. Key Properties

    PropertyValue
    Allows burst traffic?No — output is always at constant rate
    Smooth output?Yes — perfectly constant, no spikes
    MemoryO(capacity) — stores the queue
    Background timer needed?No — uses lazy evaluation like Token Bucket
    Queue typeFIFO (First In, First Out)

    7. Where It Is Used

    Company / SystemHow They Use It
    ATM NetworksOriginal use case — smoothing bursty traffic to constant rate
    Linux tcTraffic shaping with tbf (Token Bucket Filter) uses leaky bucket concepts
    Cisco routersTraffic policing and shaping
    Video codecsEnsure constant bitrate output
    Message queuesRate-limited consumers process at fixed speed

    8. Interview Tips

    What interviewers want to hear:

    1. You understand the difference from Token Bucket (input vs output control)

    2. You know it guarantees constant output rate (no bursts)

    3. You can explain the bucket + hole analogy

    4. You know the trade-off: no burst tolerance vs smooth output

    5. You can discuss when to use Leaky vs Token Bucket

    Common follow-up questions:

  • "When would you choose Leaky Bucket over Token Bucket?" → When the downstream system needs a constant, smooth rate and cannot handle any bursts.
  • "What’s the main disadvantage?" → No burst tolerance. Even if the server can handle a short burst, Leaky Bucket won’t allow it. Requests queue up or get rejected.
  • "How is this implemented in practice?" → Usually as a FIFO queue with a timer or lazy drain. In networking, it’s often combined with Token Bucket (two-rate policing).
  • "Can you combine Token Bucket and Leaky Bucket?" → Yes! Token Bucket controls input rate, Leaky Bucket smooths output. Many real systems use both.

  • 9. Comparison with Other Algorithms

    AlgorithmBurst HandlingOutput RateMemoryBest For
    Leaky Bucket❌ No burstsConstantO(cap)Smooth, constant output
    Token Bucket✅ Allows burstsVariableO(1)APIs needing burst tolerance
    Fixed Window⚠️ Edge burstVariableO(1)Simple use cases
    Sliding Window Log✅ PreciseVariableO(n)High accuracy needed
    Sliding Window Counter✅ GoodVariableO(1)Balance of accuracy & efficiency

    10. Complexity

    MetricValue
    Time ComplexityO(1) per request (amortized drain)
    Space ComplexityO(capacity) for the queue
    Drain CalculationO(1) — simple multiplication

    Implementation Example (PYTHON)

    import time
    from collections import deque
    
    class LeakyBucket:
        def __init__(self, capacity, leak_rate):
            self.capacity = capacity
            self.leak_rate = leak_rate
            self.queue = deque()
            self.last_leak = time.monotonic()
    
        def _leak(self):
            now = time.monotonic()
            elapsed = now - self.last_leak
            leaked = int(elapsed * self.leak_rate)
            for _ in range(min(leaked, len(self.queue))):
                self.queue.popleft()
            self.last_leak = now
    
        def allow(self, request):
            self._leak()
            if len(self.queue) < self.capacity:
                self.queue.append(request)
                return True   # ACCEPT
            return False       # REJECT

    Interactive Visualizer Workspace

    Explore step-by-step interactive animations, memory state tracking, and live multi-language execution in DrawCode.

    Launch Interactive Visualizer