Skip to content
LimitYourAPI
DocsPricingSolutionsLearnBlogCompare About Security Status Privacy Terms Get Started Free
Engineering Blog

Token Bucket Algorithm Explained

Deep dive into the token bucket rate limiting algorithm. Refill rates, burst capacity, Redis implementation, and production tuning.

Deep Dive: The Token Bucket Algorithm

The Token Bucket algorithm is the most widely deployed rate limiting strategy in production APIs because it balances average rate enforcement with a tolerance for sudden, natural traffic bursts.

1. How the Algorithm Works

Imagine a bucket with a maximum capacity $C$ that holds tokens.

  1. Token Refill: Tokens are added to the bucket at a constant refill rate $r$ (tokens per second) up to capacity $C$.
  2. Token Consumption: When a request arrives, the limiter checks if the bucket contains enough tokens. If yes, it consumes the tokens (usually 1 token per request) and allows the request. If not, the request is rejected (HTTP 429).
  3. Timestamp Refills: Instead of running a background loop that constantly increments tokens, modern limiters calculate refills lazily on request arrival:

$$\text{New Tokens} = \min(C, \text{Current Tokens} + (\Delta t \times r))$$

where $\Delta t$ is the elapsed time since the last request.

2. Redis Lua Script Implementation

To execute this logic atomically in Redis without race conditions, developers use Lua scripts:

`lua

local key = KEYS[1]

local capacity = tonumber(ARGV[1])

local refill_rate = tonumber(ARGV[2])

local now = tonumber(ARGV[3])

local requested = tonumber(ARGV[4])

local state = redis.call('HMGET', key, 'tokens', 'last_refill')

local tokens = tonumber(state[1])

local last_refill = tonumber(state[2])

if tokens == nil then

tokens = capacity

last_refill = now

end

local elapsed = (now - last_refill) / 1000.0

local refilled = math.min(capacity, tokens + (elapsed * refill_rate))

if refilled >= requested then

redis.call('HMSET', key, 'tokens', refilled - requested, 'last_refill', now)

return 1 -- Allowed

end

return 0 -- Blocked

`

3. Benefits and Drawbacks

4. Common Execution Pitfalls

Next Steps

Ready to protect your API with production-grade rate limiting? Here is the recommended path for Token Bucket Algorithm Explained:

  1. Create a free account at [limityourapi.tech/login](/login) — no credit card required for the Hobby tier
  2. Generate an API key in the dashboard under API Keys
  3. Install the SDK: Run go get github.com/trynayash/limityourapi-go and follow the [Go](/sdk/go) guide
  4. Follow the quick start guide at [/quickstart](/quickstart) for a 2-minute integration
  5. Configure rules in the dashboard for your highest-risk endpoints first
  6. Monitor analytics to tune limits based on real traffic patterns

Questions? Read the [documentation](/docs) or explore the [rate limiting education hub](/learn) for deep technical guides on algorithms, architecture, and production patterns.

Frequently Asked Questions

What is the difference between leaky bucket and token bucket?

The token bucket allows sudden bursts of traffic up to the bucket capacity, while the leaky bucket enforces a steady, constant flow rate by queueing requests.

What is API rate limiting?

API rate limiting controls how many requests a client can make in a given time window. It protects backends from abuse, ensures fair usage across tenants, and prevents cost overruns from traffic spikes or malicious bots.

Why use Redis for rate limiting?

Redis provides sub-millisecond latency, atomic operations via Lua scripts, and horizontal scalability. Centralized state ensures consistent limits across distributed application servers.

How fast is LimitYourAPI?

LimitYourAPI delivers rate limit decisions in under 15ms globally using atomic Redis Lua scripts. This is fast enough for inline middleware without adding perceptible latency to API responses.

Does LimitYourAPI support token bucket and sliding window?

Yes. LimitYourAPI supports token bucket, sliding window, fixed window, and cost-aware algorithms. You can configure per-route strategies without changing infrastructure.

Protect your API in minutes

Join developers using LimitYourAPI for sub-millisecond Redis-backed rate limiting.