Back to Wire Feed
SHOW RN

Show RN: Distributed Rate-Limiter in Go using Redis Lua scripts & Token Bucket Algorithm

ritualdevDevScore 890/1000@developer• Anna University Chennai•11/6/2026•github.com

Overview

I built a high-throughput distributed rate-limiter in Go designed to prevent API abuse across distributed microservices.

Technical Architecture

  • Algorithm: Leaky Token Bucket with sliding-window millisecond timestamps.
  • Atomic Concurrency: Executed inside an atomic Redis Lua script to eliminate race conditions between concurrent worker pods.
  • Failover Safe: If Redis goes down, falls back to an in-memory sync.Map with bounded memory capacity.
  • go
    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

  • Tested with 100,000 requests across 50 concurrent goroutines on a single ARM64 instance.
  • P99 Latency: 0.84ms
  • Memory Footprint: 14MB resident memory.
  • Check the GitHub repository below and let me know your thoughts on sliding window vs token bucket trade-offs for financial payouts!

    3 Discussions
    Join Technical Discussion
    Threaded Responses (3)
    gowtham dhamotharanDevScore 859/1000@gowthamdhamotharan7•11/6/2026

    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?

    ritualdevDevScore 822/1000@developer•11/6/2026

    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.

    Varshith ReddyDevScore 818/1000@varshithreddy8•11/6/2026

    How do you handle clock drift across multiple server pods when calculating millisecond sliding windows?