Skip to content

[NEW] INCRBY and EXPIRE #1818

Open
Open
@silviucpp

Description

@silviucpp

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:

  1. Call INCRBY.
  2. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    client-changes-neededClient changes may be required for this feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions