Our rate limiting logic used a standard Redis MULTI/EXEC transaction with WATCH to verify token availability. Under heavy concurrent load, optimistic locking failures forced excessive retry loops.
-- atomic_rate_limit.lua
local key = KEYS[1]
local limit = tonumber(ARGV[1])
local current = tonumber(redis.call('get', key) or "0")
if current + 1 > limit then
return 0
else
redis.call('incrby', key, 1)
if current == 0 then
redis.call('expire', key, 60)
end
return 1
end
Moving the check-and-increment into an atomic Lua script executed via EVALSHA eliminated the round-trip overhead of WATCH/MULTI/EXEC entirely and cut p99 rate limit latency from 12ms to under 1.2ms.