Skip to content

Sorted Set Commands

Sorted set commands operate on Redis sorted sets — collections of unique strings, each associated with a score. Members are ordered by score, making sorted sets useful for leaderboards, priority queues, and range queries.

This page covers: ZADD, ZINCRBY, ZRANK, ZSCAN, ZSCORE


ZADD

Add one or more members with scores to a sorted set, with optional conditional flags.

📘 Redis Reference: ZADD

XML Example

xml
<!-- Add multiple members with scores -->
<lettuce-redis:zadd doc:name="Add leaderboard scores"
    config-ref="Redis_Config"
    key="leaderboard:global"
    memberScores="#[{'player:123': 1500.0, 'player:456': 1200.0, 'player:789': 1800.0}]"
    xx="false"
    nx="false"
    gt="false"
    lt="false"
    ch="false"/>

<!-- Only update existing members if new score is higher -->
<lettuce-redis:zadd doc:name="Update high scores"
    config-ref="Redis_Config"
    key="leaderboard:global"
    memberScores="#[{'player:123': 1600.0}]"
    xx="true"
    gt="true"
    ch="true"/>

Parameters

ParameterTypeRequiredDefaultDescription
keyStringYesThe sorted set key
memberScoresMap<String, Double>YesMember-to-score pairs. Payload by default via @Content.
xxbooleanNofalseOnly update existing members (do not add new)
nxbooleanNofalseOnly add new members (do not update existing)
gtbooleanNofalseOnly update if the new score is greater than the current score
ltbooleanNofalseOnly update if the new score is less than the current score
chbooleanNofalseChange the return value to count changed members instead of added members

Output

Type: Long

  • By default: the number of new members added (updates to existing members are not counted)
  • When ch=true: the number of members added or changed

Errors

Error TypeCondition
REDIS:COMMANDGeneral command execution error
REDIS:WRONG_TYPEKey exists but is not a sorted set

ZSCORE

Get the score of a member in a sorted set.

📘 Redis Reference: ZSCORE

XML Example

xml
<lettuce-redis:zscore doc:name="Get player score"
    config-ref="Redis_Config"
    key="leaderboard:global"
    member="player:123"/>

<!-- payload is now the player's score as a Double -->
<logger level="INFO" message="Player score: #[payload]"/>

Parameters

ParameterTypeRequiredDefaultDescription
keyStringYesThe sorted set key
memberStringYesThe member whose score to retrieve

Output

Type: Double

The score of the member.

Errors

Error TypeCondition
REDIS:COMMANDGeneral command execution error
REDIS:WRONG_TYPEKey exists but is not a sorted set

ZRANK

Get the rank (index) of a member in a sorted set, optionally with its score.

📘 Redis Reference: ZRANK

XML Example

xml
<!-- Get rank only -->
<lettuce-redis:zrank doc:name="Get player rank"
    config-ref="Redis_Config"
    key="leaderboard:global"
    member="player:123"
    withScore="false"/>
<!-- payload is a Long (the rank, 0-based) -->

<!-- Get rank and score together -->
<lettuce-redis:zrank doc:name="Get player rank and score"
    config-ref="Redis_Config"
    key="leaderboard:global"
    member="player:123"
    withScore="true"/>
<!-- payload is a Map with keys "rank" and "score" -->
<logger level="INFO" message="Rank: #[payload.rank], Score: #[payload.score]"/>

Parameters

ParameterTypeRequiredDefaultDescription
keyStringYesThe sorted set key
memberStringYesThe member to get the rank of
withScorebooleanNofalseIf true, return both rank and score together

Output

Type: Object (dynamic, resolved by metadata)

  • When withScore=false: Long — The rank (0-based index in sorted order)
  • When withScore=true: Map<String, Object> with keys:
    • "rank" → Long
    • "score" → Double

Errors

Error TypeCondition
REDIS:NILMember does not exist in the sorted set
REDIS:COMMANDGeneral command execution error
REDIS:WRONG_TYPEKey exists but is not a sorted set

ZINCRBY

Increment the score of a member in a sorted set.

📘 Redis Reference: ZINCRBY

XML Example

xml
<lettuce-redis:zincrby doc:name="Award points"
    config-ref="Redis_Config"
    key="leaderboard:global"
    increment="100.0"
    member="player:123"/>

<!-- payload is now the new score after increment -->
<logger level="INFO" message="New score: #[payload]"/>

Parameters

ParameterTypeRequiredDefaultDescription
keyStringYesThe sorted set key
incrementDoubleYesThe amount to add to the member's score (can be negative)
memberStringYesThe member whose score to increment

Output

Type: Double

The new score of the member after the increment.

Errors

Error TypeCondition
REDIS:COMMANDGeneral command execution error
REDIS:WRONG_TYPEKey exists but is not a sorted set

ZSCAN

Incrementally iterate sorted set members and scores using a cursor.

📘 Redis Reference: ZSCAN

Automated Cursor Management

For most use cases, consider using Search Sorted Set Members instead, which handles cursor iteration automatically and streams all member-score pairs. Use ZSCAN directly only when you need fine-grained control over pagination.

XML Example

xml
<!-- Start a new scan (cursor 0) -->
<lettuce-redis:zscan doc:name="Scan leaderboard"
    config-ref="Redis_Config"
    key="leaderboard:global"
    cursor="0"
    match="player:*"
    count="50"/>

<!-- payload is a List<Map<String, Double>> -->
<!-- Each element is a single-entry map: {member: score} -->
<foreach collection="#[payload]">
    <logger level="INFO"
        message="Member: #[payload.keys()[0]], Score: #[payload[payload.keys()[0]]]"/>
</foreach>

<!-- attributes.cursor contains the next cursor value -->
<logger level="INFO" message="Next cursor: #[attributes.cursor]"/>

Parameters

ParameterTypeRequiredDefaultDescription
keyStringYesThe sorted set key to scan
cursorIntegerYesCursor position. Use 0 to start a new scan.
matchStringNoGlob-style pattern to filter member names
countIntegerNoHint for the number of elements to return per iteration

Output

Type: List<Map<String, Double>>

A list where each element is a single-entry map representing one member-score pair:

json
[
  {"player:123": 1500.0},
  {"player:456": 1200.0},
  {"player:789": 1800.0}
]

Attributes: ScanAttributes — Contains the cursor field with the next cursor value. When cursor is 0, the scan is complete.

Errors

Error TypeCondition
REDIS:COMMANDGeneral command execution error
REDIS:WRONG_TYPEKey exists but is not a sorted set

Released under the MIT License.