← back

Distributed Rate Limiter

2024 · Go · API Design

Go Redis Lua Middleware

A rate limiting library and middleware for Go services. Implements multiple algorithms (token bucket, sliding window, fixed window) backed by Redis for distributed coordination across service instances.

Algorithms Supported

Design

All state lives in Redis. Each algorithm is implemented as an atomic Lua script to avoid race conditions across concurrent check-and-decrement operations. The middleware layer is pluggable — works with net/http, Gin, and Echo.

Design Decisions

Tradeoffs

Every rate limit check is a Redis round-trip (~0.5–1ms). For high-throughput services this is acceptable. For sub-millisecond requirements, a local approximate counter with periodic Redis sync is a better fit — a pattern used by Cloudflare's rate limiter.