Sorted sets

Sorted sets #

15 commands. Scores are 64-bit integers, not doubles — the one difference that changes query results, not just the wire format.

CmdRWSyntaxReturnsExample → reply
ZADDWZADD KEY score MEMBER [score MEMBER ...]integer members addedZADD board 100 ada 90 bob → 2
ZINCRBYR ⚠ZINCRBY KEY increment MEMBERbulk new scoreZINCRBY board 5 ada → 105
ZREMWZREM KEY MEMBER [MEMBER ...]integer members removedZREM board bob → 1
ZCARDRZCARD KEYinteger cardinalityZCARD board → 1
ZCOUNTRZCOUNT KEY min maxinteger members within the score rangeZCOUNT board 0 200 → 1
ZSCORERZSCORE KEY MEMBERbulk score, null if absentZSCORE board ada → 105
ZRANKRZRANK KEY MEMBERinteger rank (0-based), null if absentZRANK board ada → 0
ZREVRANKRZREVRANK KEY MEMBERinteger reverse rank, null if absentZREVRANK board ada → 0
ZRANGERZRANGE KEY start stop [WITHSCORES]array members (and scores)ZRANGE board 0 -1 WITHSCORES → [ada, 105]
ZREVRANGERZREVRANGE KEY start stop [WITHSCORES]array members (and scores)ZREVRANGE board 0 0 → [ada]
ZRANGEBYSCORERZRANGEBYSCORE KEY min max [WITHSCORES]array members (and scores)ZRANGEBYSCORE board 100 200 → [ada]
ZREVRANGEBYSCORERZREVRANGEBYSCORE KEY max min [WITHSCORES]array members (and scores)ZREVRANGEBYSCORE board 200 100 → [ada]
ZREMRANGEBYRANKR ⚠ZREMRANGEBYRANK KEY start stopinteger members removedZREMRANGEBYRANK board 0 0 → 1
ZREMRANGEBYSCORER ⚠ZREMRANGEBYSCORE KEY min maxinteger members removedZREMRANGEBYSCORE board 0 50 → 0
ZCLEARWZCLEAR KEYinteger members removedZCLEAR board → 1

Notes #

  • ⚠ ZINCRBY, ZREMRANGEBYRANK and ZREMRANGEBYSCORE are registered as read commands even though they mutate. Locally that is harmless; in a cluster their effect is not replicated and does not survive a leader change. Until that is fixed: compute scores client-side and write them with ZADD, and remove members with ZREM.
  • No TTL for sorted sets. There is no ZEXPIRE/ZTTL; clear and recreate the key (ZCLEAR), or keep the expiry bookkeeping in a string key.
  • Scores parse as int64. A float score such as 1.5 is rejected; WITHSCORES returns integers.
  • ZRANGEBYSCORE min max is inclusive on both ends; ZREVRANGEBYSCORE max min takes the arguments in reverse order, as in Redis.
  • ZRANK/ZREVRANK answer null for a missing member, ZSCORE likewise.
  • All range reads are ordered by score, then by member for equal scores.