Show RN: Distributed Rate-Limiter in Go using Redis Lua scripts & Token Bucket Algorithm
Overview
I built a high-throughput distributed rate-limiter in Go designed to prevent API abuse across distributed microservices.
Technical Architecture
func (rl *RateLimiter) Allow(ctx context.Context, key string, limit int64, window time.Duration) (bool, error) {
now := time.Now().UnixNano()
res, err := rl.client.Eval(ctx, tokenBucketLuaScript, []string{key}, limit, window.Nanoseconds(), now).Result()
if err != nil {
return rl.fallbackMemory(key, limit)
}
return res.(int64) == 1, nil
}
Benchmarks
Check the GitHub repository below and let me know your thoughts on sliding window vs token bucket trade-offs for financial payouts!
Great writeup! Did you consider using Redis cell (CL.THROTTLE) module instead of custom Lua scripts? Or did you want zero external module dependencies on standard managed Redis instances?
Exactly! Most managed cloud Redis providers (AWS ElastiCache, Upstash, DigitalOcean Managed Redis) disable external modules like RedisBloom and RedisCell for security/isolation. Writing the Lua script ensures it runs on any standard vanilla Redis 6+ cluster out of the box.
How do you handle clock drift across multiple server pods when calculating millisecond sliding windows?