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
More System Design, Redis & Distributed Systems Interview Questions
How does the Redis Redlock Algorithm provide Fault-Tolerant Distributed Locking across independent Redis nodes?
Read →
Explain the 3 High-Scale Cache Failure Patterns: Cache Avalanche, Cache Breakdown (Dog-Piling), and Cache Penetration, with mitigations.
Read →
How do you design a Real-Time Notification System delivering to 10M concurrent users with WebSockets and Redis Pub/Sub?
Read →
How do you design an Idempotent Payment Processing Architecture with the Saga Pattern across Microservices?
Read →
How does the Circuit Breaker Pattern prevent cascading failures in Microservice architectures?
Read →