Back to results for “Rate Limiting with Redis”
Redis › Rate Limiting with Redis

How would you implement a distributed rate limiter using Redis that works correctly across multiple application servers handling requests for the same user?

Advanced

Answer

You centralize all rate limit counting in Redis rather than in each individual application server's local memory, using an atomic Lua script to check and increment the shared counter, ensuring that no matter which specific application server receives a given request, they are all checking against the exact same shared, consistent count for that user.
-- All application servers call the same atomic Lua script
-- against the same shared Redis key for a given user
EVALSHA rate_limit_script_sha 1 'user:123:limit' 100 60
Real-world example A company running dozens of application servers behind a load balancer uses a shared Redis based rate limiter, ensuring a user cannot bypass their rate limit simply by having requests routed to different application servers.

Common follow-ups: What happens to rate limiting accuracy if the Redis instance itself becomes temporarily unavailable?;How do you handle rate limiting during a Redis failover event?