1. Home
  2. Computer Networks
  3. Token Bucket (Traffic Shaping)

Token Bucket (Traffic Shaping)

Limit how fast a sender may transmit while still allowing short bursts. Tokens drip into a bucket and each packet spends one.

Interactive 3DIntermediate11 min readCNUpdated

Drag to rotate · Right-drag to pan · Click, then scroll to zoom · Space play · ←→ step

What's happening

Pseudocode

    Try this in the 3D model

    • Run Bursty traffic with the default bucket (size 5, rate 1). At which tick are packets dropped, and why?
    • Increase the bucket size to 8 and run again. How many fewer packets are dropped?
    • Switch to Steady traffic. With rate 1, what is the average rate the bucket can sustain?
    • Run Overload with rate 3. Is anything dropped now?

    Controlling the flow into the network

    If every sender transmitted as fast as it liked, routers would overflow. Networks therefore shape or police traffic. The token bucket is the most widely used method, found in routers, API rate limiters and cloud gateways.

    The algorithm

    • A bucket holds up to B tokens (the bucket size) and starts full.
    • Every tick, r new tokens are added. If the bucket is full the extra tokens are lost.
    • To send a packet, take one token out. If there is none, the packet is dropped (or queued).
    tokens ← B
    every tick:
        tokens ← min(B, tokens + r)
        for each arriving packet:
            if tokens ≥ 1:  tokens ← tokens − 1;  send
            else:            drop

    Worked example

    Bucket size 5, rate 1, arrivals per tick: 4, 4, 0, 0, 1, 0, 5, 0, 0, 1.

    Tick Arrivals Sent Dropped Tokens left
    1 4 4 0 1
    2 4 2 2 0
    3 0 0 0 1
    4 0 0 0 2
    5 1 1 0 2
    6 0 0 0 3
    7 5 4 1 0
    8–10 0, 0, 1 1 0 2

    Total: 12 sent, 3 dropped. The idle ticks 3–6 let the bucket refill, which paid for the burst at tick 7.

    Token bucket vs leaky bucket

    Token bucket Leaky bucket
    Output Bursts allowed, up to the bucket size Constant rate
    Idle time Saved as tokens Wasted
    Typical use Rate limiting APIs and routers Smoothing traffic into a fixed pipe

    Tuning

    • Rate r sets the long-run average the sender may use.
    • Size B sets how large a burst is tolerated. A big bucket is friendly to bursty applications but can hit the network with a large spike.

    Compare this with TCP congestion control, where the sender slows itself down after losses instead of being limited by a router.

    Code

    class TokenBucket:
        def __init__(self, size, rate):
            self.size, self.rate, self.tokens = size, rate, size
    
        def tick(self):
            self.tokens = min(self.size, self.tokens + self.rate)
    
        def allow(self):
            if self.tokens >= 1:
                self.tokens -= 1
                return True
            return False

    Common mistakes

    • Thinking the bucket holds packets. It holds permission tokens.
    • Forgetting the cap: tokens beyond the bucket size are discarded, so idle time cannot be saved forever.
    • Confusing the two buckets: only the leaky bucket forces a constant output rate.

    Complexity at a glance

    Case / operationTimeWhy
    Per packetO(1)Check and decrement a counter.
    Maximum burstbucket sizeA full bucket lets that many packets go at once.
    Long-run average ratetoken rateNever more than the tokens added per tick.
    Extra spaceO(1) — one counter and a timestamp

    Quick check

    Test yourself — pick an answer to see if you got it.

    1. What happens to a packet when the bucket has no tokens (in the policing version)?

    2. What does the bucket size control?

    3. How is a token bucket different from a leaky bucket?

    4. A bucket of size 5 is full and 7 packets arrive in one tick (refill rate 1). How many are sent?

    Saved only in this browser — no account needed.
    Spotted a mistake or a bug in the 3D model?

    Report a mistake

    in Token Bucket (Traffic Shaping). Thank you — every report makes the lesson better for the next reader.

    We'll also include a link to the step of the 3D model you're on and your browser type, so we can reproduce it.