How we scaled the GitHub API with a sharded, replicated rate limiter in Redis
github.blog
github.blog
local expires_at = tonumber(redis.call("get", expires_at_key))
if not expires_at or expires_at < current_time then
-- this is either a brand new window,
-- or this window has closed, but redis hasn't cleaned up the key yet
-- (redis will clean it up in one more second)
-- initialize a new rate limit window
redis.call("set", rate_limit_key, 0)
redis.call("set", expires_at_key, next_expires_at)
Between the time the `get` is called and the `set` is called there might parallel call that might create the key and the `set` will override it.https://redis.io/commands/eval#atomicity-of-scripts
> Redis guarantees that a script is executed in an atomic way: no other script or Redis command will be executed while a script is being executed.