Go Academy · Lesson

Token Bucket Concept

Limit request rates.

Lesson 1 of 413 steps

Token Bucket Concept is a free Go Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, and your progress syncs across the web and the CoddyKit app. The Go Academy course includes 4 lessons in total.

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
Free to start

Learn Go with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “Token Bucket Concept” lesson free?

Yes — any 3 lessons of the Go Academy learning path, including “Token Bucket Concept”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Go Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

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