Skip to main content

Designing a High-Throughput Distributed Rate Limiter: From Token Bucket to Redis Lua Scripts

Updated 7 min readMarkdown

Rate limiting is a foundational building block for modern web scale architectures. Whether you are protecting an internal service from cascading failures, enforcing API monetization tiers, or mitigating distributed denial-of-service (DDoS) attacks, a robust rate limiter is essential.

In this deep dive, we will explore the core mechanics of rate limiting algorithms, the trade-offs of distributed implementations, and how to build a production-ready, race-condition-free distributed rate limiter using Go, Redis, and atomic Lua scripts.

  1. Why Naive Rate Limiting Fails in Distributed Systems In a single-instance architecture, rate limiting is straightforward. You can keep an in-memory counter using atomic operations or a localized mutex. However, modern infrastructure relies on horizontal scaling: multiple API gateways or microservices behind a load balancer.

[ Client Request ] ───> [ Load Balancer ] │ ┌────────────────┴────────────────┐ ▼ ▼ [ API Gateway Instance A ] [ API Gateway Instance B ] │ │ └───────────────┬─────────────────┘ ▼ [ Shared Storage / Redis ] When rate limiting state is distributed across multiple gateway nodes, two main problems arise:

Inconsistent State: If Instance A tracks requests independently of Instance B, a client can easily bypass the limit by hitting different instances.

Race Conditions (Check-Then-Set Bug): If instances fetch a counter from a shared cache (like Redis), increment it in memory, and write it back, concurrent requests will lead to race conditions where requests are under-counted.

To solve this, we need atomic execution and centralized state management.

  1. Comparing Core Rate Limiting Algorithms Before implementing our distributed solution, let's compare the four primary algorithms used in system design:

A. Token Bucket Mechanism: Tokens are added to a "bucket" at a constant fill rate. Each incoming request consumes one token. If the bucket is empty, the request is rejected.

Pros: Handles bursts of traffic gracefully up to the maximum bucket capacity.

Cons: Memory state requires tracking both timestamps and token counts.

B. Leaky Bucket Mechanism: Requests enter a FIFO queue (the bucket) and leave at a fixed, constant rate. If the queue overflows, new requests are dropped.

Pros: Smooths out traffic spikes; guarantees a uniform outflow rate.

Cons: Bursts are delayed rather than processed immediately; can increase latency.

C. Fixed Window Counter Mechanism: Time is divided into fixed windows (e.g., 1-minute intervals). A simple counter increments for each request in the current window.

Pros: Memory efficient and easy to implement.

Cons: Traffic spikes at the window boundaries (e.g., 59th second and 1st second) can allow up to 2x the allowed requests in a short burst.

D. Sliding Window Counter (Best Balance) Mechanism: Combines the current window counter with the weighted counter of the previous window to estimate the current rate.

Pros: Extremely memory-efficient with high accuracy; prevents boundary bursts.

Cons: Slightly more complex math computation.

  1. Solving Race Conditions with Redis & Lua Scripts To implement a Sliding Window Counter or Token Bucket across multiple API gateway nodes without locking overhead, we can offload the rate limiting logic directly into Redis using a Lua script.

Redis guarantees that Lua scripts execute atomically. No other Redis command will run while a script is executing, eliminating race conditions entirely without requiring distributed locks (like Redlock).

The Atomic Token Bucket Lua Script Here is an optimized Lua script implementing the Token Bucket algorithm in Redis:

Lua -- KEYS[1]: Rate limit key (e.g., "rate_limit:user_123") -- ARGV[1]: Max bucket capacity (burst limit) -- ARGV[2]: Refill rate per second -- ARGV[3]: Current Unix timestamp (in seconds) -- ARGV[4]: Requested tokens (usually 1)

local key = KEYS[1] local capacity = tonumber(ARGV[1]) local fill_rate = tonumber(ARGV[2]) local now = tonumber(ARGV[3]) local requested = tonumber(ARGV[4])

-- Fetch current state from Redis local data = redis.call("HMGET", key, "tokens", "last_updated") local tokens = tonumber(data[1]) local last_updated = tonumber(data[2])

if tokens == nil then -- First request, initialize bucket at full capacity tokens = capacity last_updated = now else -- Calculate leaked/refilled tokens since last request local delta = math.max(0, now - last_updated) tokens = math.min(capacity, tokens + (delta * fill_rate)) last_updated = now end

-- Check if enough tokens exist if tokens >= requested then tokens = tokens - requested redis.call("HMSET", key, "tokens", tokens, "last_updated", last_updated) -- Set TTL to auto-cleanup inactive keys (capacity / fill_rate) redis.call("EXPIRE", key, math.ceil(capacity / fill_rate) * 2) return {1, math.floor(tokens)} -- Allowed: 1, Remaining tokens else redis.call("HMSET", key, "tokens", tokens, "last_updated", last_updated) return {0, math.floor(tokens)} -- Denied: 0, Remaining tokens end 4. Go Implementation Now let's wrap this in a clean, idiomatic Go package using go-redis.

Go package main

import ( "context" "context" "fmt" "time"

"github.com/redis/go-redis/v9"

)

const rateLimitScript = ` local key = KEYS[1] local capacity = tonumber(ARGV[1]) local fill_rate = tonumber(ARGV[2]) local now = tonumber(ARGV[3]) local requested = tonumber(ARGV[4])

local data = redis.call("HMGET", key, "tokens", "last_updated") local tokens = tonumber(data[1]) local last_updated = tonumber(data[2])

if tokens == nil then tokens = capacity last_updated = now else local delta = math.max(0, now - last_updated) tokens = math.min(capacity, tokens + (delta * fill_rate)) last_updated = now end

if tokens >= requested then tokens = tokens - requested redis.call("HMSET", key, "tokens", tokens, "last_updated", last_updated) redis.call("EXPIRE", key, math.ceil(capacity / fill_rate) * 2) return {1, math.floor(tokens)} else redis.call("HMSET", key, "tokens", tokens, "last_updated", last_updated) return {0, math.floor(tokens)} end `

type Limiter struct { client *redis.Client sha string }

func NewLimiter(client *redis.Client) (*Limiter, error) { ctx := context.Background() sha, err := client.ScriptLoad(ctx, rateLimitScript).Result() if err != nil { return nil, fmt.Errorf("failed to load Lua script: %w", err) } return &Limiter{client: client, sha: sha}, nil }

func (l *Limiter) Allow(ctx context.Context, identifier string, capacity int, fillRate float64) (bool, int, error) { key := fmt.Sprintf("ratelimit:%s", identifier) now := time.Now().Unix()

res, err := l.client.EvalSha(ctx, l.sha, []string{key}, capacity, fillRate, now, 1).Result()
if err != nil {
	return false, 0, err
}

results, ok := res.([]interface{})
if !ok || len(results) < 2 {
	return false, 0, fmt.Errorf("invalid response from Redis")
}

allowed := results[0].(int64) == 1
remaining := int(results[1].(int64))

return allowed, remaining, nil

}

func main() { rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", })

limiter, err := NewLimiter(rdb)
if err != nil {
	panic(err)
}

ctx := context.Background()
userID := "user_42"

// Allow burst of 10 requests, refilling at 2 requests/sec
for i := 1; i <= 12; i++ {
	allowed, remaining, _ := limiter.Allow(ctx, userID, 10, 2.0)
	fmt.Printf("Request %2d: Allowed = %-5v | Remaining Tokens = %d\n", i, allowed, remaining)
}

} 5. Resilience Considerations and Best Practices Deploying a rate limiter into high-availability production environments introduces edge-case vulnerabilities that require careful architecting:

  1. Redis High Availability & Fail-Open Behavior If your Redis cluster experiences latency or a node failure, should your rate limiter fail open or fail closed?

Fail-Open (Recommended for Most APIs): If Redis times out, allow requests through and log a metric alert. This preserves user experience at the risk of brief backend overload.

Fail-Closed: Necessary for critical billing endpoints or non-scalable microservices where backend crash prevention is priority #1.

  1. Clock Synchronization The Token Bucket Lua script relies on Unix Timestamp. Ensure all app instances and Redis nodes use Network Time Protocol (NTP) to prevent clock drift from distorting the rate limiter calculations.

  2. HTTP Header Standards Always communicate limit status clearly to API consumers by returning standardized HTTP headers (RFC 6585):

HTTP HTTP/1.1 429 Too Many Requests Retry-After: 30 X-RateLimit-Limit: 100 X-RateLimit-Remaining: 0 X-RateLimit-Reset: 1672531200 Conclusion Building a high-performance distributed rate limiter requires balancing state centralization, execution speed, and resilience. By leveraging Redis Lua scripting, you achieve atomic execution and microsecond latency without distributed locking bottlenecks.恋みくじ

As your system scales, consider tiering your rate limits (e.g., IP-level limits at Cloudflare/Envoy CDN, and user-level business logic limits at the API Gateway layer) for multi-layered system defense.

65 views

About the author

  • 日本文化と日常の小さな楽しみを、Webで気軽に体験できるサイト 一禅堂 を個人で企画・開発しています。 一禅堂では、今の恋愛運や相手との関係を楽しく占える「恋みくじ」、毎日の運勢を気軽に楽しめる「おみくじ」、漢字の読み方・意味・筆順を調べられる「漢字辞典」など、実用性と日本らしい体験を大切にしたコンテンツを公開しています。 Hashnodeでは、サイト開発を通して得た知識を中心に、フロントエンド開発、UI/UX、多言語対応、プライバシーに配慮した設計、SEO、静的サイトの運用について発信します。
    一禅堂の 恋みくじ 公式サイト
    漢字辞典: 漢字書き順
    神社 おみくじ
Hhaleo2mo ago

A production rate limiter also needs an explicit failure policy. When Redis is slow or unavailable, should the service fail open, fail closed, or temporarily use a local fallback? The correct answer depends on whether availability, cost protection, or abuse prevention is the higher priority.

Ddozen2mo ago

The next scaling concern is hot-key distribution. A global limit stored under one Redis key can become a serialization point even when the cluster has plenty of total capacity. Per-tenant keys scale better, while truly global limits may require a hierarchical design.

Jjkwho2mo ago

Time handling is easy to underestimate in a distributed limiter. If application servers calculate refills using their own clocks, clock skew can produce inconsistent decisions. Keeping time calculation close to the authoritative Redis state makes the behavior easier to reason about.

Llove2mo ago

Token bucket is a practical choice because it separates the sustained rate from the permitted burst size. Those two values should be configured independently; otherwise, increasing throughput may accidentally allow much larger traffic spikes than the downstream service can absorb.

Jjohson2mo ago

Using a Lua script for refill, validation, and token consumption is the key step here. Without atomic execution, concurrent clients can read the same token count and both succeed, allowing the real request rate to exceed the configured limit.