+31
−15
Loading
Both Lua scripts read the key's TTL twice: once before mutating, to decide
whether to EXPIRE, and once after, to return the window's remaining time.
The pre-read is redundant. INCRBYFLOAT and SADD both preserve an existing
key's TTL and create a missing key with no expiry, so reading the TTL after
the mutation gives what the pre-read gave. It still tells apart the two
cases the script has to handle: -2 for a missing key, -1 for a key left
without an expiry by some earlier bug.
So move the read after the mutation and drop the second one. INCR_SCRIPT
goes from four unconditional Redis calls to three, SADD_SCRIPT from five to
four. Atomicity is unchanged, TTL-less keys still self-heal, and the
{count, ttl} return contract is the same, so the existing specs for the
three TTL states of each script pass without modification.
One thing does change shape. On the first write of a window the script now
returns the period it just set rather than reading it back from Redis. Same
value, and reset_at still derives from it.
Worth being honest about the size of this. TTL is the most-called command
on gprd's redis-cluster-ratelimiting, roughly 115k/s out of 566k/s, since
the rack shadow went to 100% on 2026-07-13 and every check spends two.
Halving the count only buys about 1% of node CPU though: command execution
is some 18% of process CPU on the busiest primary, and this removes no
round trips. Dropping whole EVALSHA calls is what would matter for
capacity, and that is what :skip does.
Related to gitlab-com/gl-infra/production-engineering#28807