Skip to content

ZADD option: Conditionally Update Score for Existing Keys  #7416

@alexronke-channeladvisor

Description

Summary
As of Redis 3.2, ZADD provides two options for adding key/score pairs to a sorted set:

XX: Only update elements that already exist. Never add elements.
NX: Don't update already existing elements. Always add new elements.

I would like to propose (and develop) two new options:
GX: Always add new elements, but only update elements when the new score for an existing key is greater than the old score.
LX: Always add new elements, but only update elements when the new score for an existing key is less than the old score.

Background
I currently perform this operation via Lua script, but I'm ultimately interacting with Redis on an item-by-item basis. It would be far better if this could be done in bulk as a part of the ZADD function.

We use this functionality in order to treat Redis sorted sets as a time-based priority queue, typically with the score being a converted timestamp. The problem with the current options is that they are essentially all-or-nothing: with NX we can ensure that an item's priority cannot be changed, but it prevents us from reliably increasing that item's priority (in our case by lowering its score).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions