Distributed rate limiter cần shared atomic counter — Redis (với Lua scripts) là giải pháp tiêu chuẩn, với local-global hybrid như là lựa chọn lower-latency.
Approach 1 – Centralized Redis: dùng Redis Lua script (atomic operations) để implement sliding window counter hoặc token bucket. Redis INCR + EXPIRE cho fixed window; sorted set với ZADD/ZREMRANGEBYSCORE cho sliding window log. Ưu: centralized, chính xác; Nhược: Redis là single point of failure (giải quyết bằng Redis Cluster), mỗi request phải gọi Redis (+latency). Approach 2 – Token Bucket với Redis: lưu (tokens, last_refill_time) per user trong Redis; mỗi request atomic update via Lua script; refill tokens theo elapsed time. Approach 3 – Local + Global: mỗi API server có local counter, sync với Redis định kỳ – giảm Redis calls nhưng ít chính xác hơn (có thể vượt limit tạm thời). Response headers: X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, Retry-After. Multi-level rate limiting: per IP, per user, per endpoint, per API key – với different limits. Distributed Rate Limiting phức tạp hơn vì không có shared memory – Redis là giải pháp thực tế nhất; nếu Redis down, circuit breaker để decide fail-open (allow all) hay fail-closed (block all).