0Pricing
Go Academy · Lesson

Token Bucket Concept

Limit request rates.

Token Bucket Concept is a free Go Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Rate Limit

Rate limiting caps how many operations happen per unit of time. It protects APIs from abuse, respects third-party quotas, and keeps systems stable under load.

  • Prevents overload
  • Enforces fair usage
  • Honors upstream limits

The Token Bucket Model

Imagine a bucket that holds tokens. Each request must take a token to proceed. Tokens refill at a steady rate. If the bucket is empty, the request waits or is rejected.

Rate and Burst

Two parameters define a token bucket: the rate (tokens added per second) and the burst (bucket capacity). Burst allows short spikes above the steady rate.

Refill Over Time

If the rate is 5 tokens per second, one token appears roughly every 200 milliseconds. The bucket never holds more tokens than its burst capacity, so unused capacity does not accumulate forever.

Allowing Bursts

A burst of 10 means up to 10 requests can fire instantly if the bucket is full. After that, requests are paced at the steady rate. This balances responsiveness with control.

A Simple Time-Based Check

You can approximate rate limiting with a ticker that releases permission at fixed intervals. Each request waits for the next tick.

ticker := time.NewTicker(200 * time.Millisecond)
defer ticker.Stop()
for i := 0; i < 3; i++ {
    <-ticker.C
    fmt.Println("request", i)
}

Token Bucket vs Leaky Bucket

A leaky bucket drains at a constant rate and smooths output completely, forbidding bursts. A token bucket permits bursts up to capacity. Token bucket is more common for API limits.

A Minimal Bucket Simulation

This runnable example simulates a token bucket of capacity 3 refilling every 100ms, showing which requests are allowed.

package main

import (
    "fmt"
    "time"
)

func main() {
    tokens := 3
    last := time.Now()
    rate := 100 * time.Millisecond
    for i := 0; i < 6; i++ {
        elapsed := time.Since(last)
        refill := int(elapsed / rate)
        if refill > 0 {
            tokens += refill
            if tokens > 3 {
                tokens = 3
            }
            last = time.Now()
        }
        if tokens > 0 {
            tokens--
            fmt.Println("request", i, "allowed")
        } else {
            fmt.Println("request", i, "rejected")
        }
        time.Sleep(40 * time.Millisecond)
    }
}

Allow vs Wait vs Reserve

Limiters usually offer three behaviors: Allow returns immediately true or false, Wait blocks until a token is free, and Reserve tells you how long to wait. Pick by whether you can block.

Where Limits Live

Rate limits can be per-client, per-endpoint, or global. A map of limiters keyed by client IP enforces per-client quotas, while a single shared limiter caps total throughput.

Choosing Parameters

Match your rate to the downstream quota. If an API allows 600 requests per minute, set rate to 10 per second. Set burst to absorb expected spikes without exceeding the average.

Quick Check

Test your token bucket understanding.

Recap

You learned the token bucket concept:

  • Tokens are consumed per request and refill at a rate
  • Burst is the bucket capacity for spikes
  • Token bucket allows bursts; leaky bucket does not
  • Allow, Wait, and Reserve are the common APIs

Frequently asked questions

Is the “Token Bucket Concept” lesson free?

Yes — the full text of “Token Bucket Concept” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.

What will I learn in “Token Bucket Concept”?

Limit request rates. You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Token Bucket Concept” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Token Bucket Concept
  2. golang.org/x/time/rate
  3. Throttling Goroutines
  4. Backoff and Retry
← Back to Go Academy