-
Notifications
You must be signed in to change notification settings - Fork 957
Description
The Problem/Use-Case This Feature Addresses
When using counters, you often need to set them to expire after a certain period. Currently, the only known solution is:
- Call
INCRBY. - If the returned result equals the initial value (indicating a new record was created), call
EXPIRE.
This issue is also discussed in the following resources:
To avoid the race condition where INCRBY executes but EXPIRE does not, the recommended approach is to use a Lua script via EVAL. However, this method negatively impacts performance.
Alternatives Considered
It would be beneficial to have a single command that combines both operations—only applying expiration when the record is initially created. Using a transaction isn't ideal because EXPIRE will always execute, unnecessarily updating the TTL.
A clean solution would be to introduce an additional parameter, EX, to the INCR family of commands, similar to how it's used in SET.
Another alternative is to enhance the EXPIRE command by adding an NX option. With NX, EXPIRE would only apply if the key does not already have an expiration, making it more flexible.