Tutorials Courses Interview Prep (100 Q&A) AI Tools (50) Developer Tools (50) Tracks Pricing Plans Blog & Articles Contact Us
System Design, Redis & Distributed Systems 8-10 Years (Tech Lead) Hard Production Scenario

Compare Rate Limiting Algorithms: Token Bucket, Leaky Bucket, Fixed Window Counter, and Sliding Window Log/Counter.

Real-World Scenario / Incident Description

Designing an enterprise API gateway protecting downstream microservices from traffic spikes while allowing legitimate burst traffic.

472 views • Updated 1 month ago

Comparison Matrix

Algorithm Pros Cons Ideal For
Token Bucket Allows bursts up to bucket capacity, smooth refills Memory state tracking required API Gateways (Stripe / GitHub style)
Leaky Bucket Guarantees constant, smooth output rate Discards bursts if queue full Egress traffic shaping
Fixed Window Low memory Boundary spikes (2x burst at window edges) Basic IP throttling
Sliding Window Counter Prevents boundary spikes, memory efficient Approximate calculation High-scale tier limiting

Sliding Window Lua Script in Redis

local key = KEYS[1]
local now = tonumber(ARGV[1])
local window = tonumber(ARGV[2])
local limit = tonumber(ARGV[3])

-- Remove timestamps older than current sliding window
redis.call('ZREMRANGEBYSCORE', key, '-inf', now - window)
local current_count = redis.call('ZCARD', key)

if current_count < limit then
    redis.call('ZADD', key, now, now)
    redis.call('EXPIRE', key, window)
    return 1 -- Allowed
else
    return 0 -- Blocked (HTTP 429)
end
Related Concepts: #Rate Limiting #Token Bucket #Sliding Window #Redis Lua #API Gateway