Sorted sets are collections of unique strings ordered by an associated score. Each member has a floating-point score that determines its position in the set. Kora implements all standard Redis sorted set operations with identical semantics.
ZADD
Add one or more members to a sorted set, or update scores if they already exist.
Syntax
ZADD key score member [score member ...]
The key of the sorted set
The score associated with the member (used for ordering)
One or more members to add
Return value
The number of members added to the sorted set (excluding score updates to existing members)
Examples
redis-cli> ZADD leaderboard 100 "alice"
(integer) 1
redis-cli> ZADD leaderboard 85 "bob" 92 "charlie"
(integer) 2
redis-cli> ZADD leaderboard 95 "alice"
(integer) 0
redis-cli> ZRANGE leaderboard 0 -1 WITHSCORES
1) "bob"
2) "85"
3) "charlie"
4) "92"
5) "alice"
6) "95"
Time complexity: O(log(N)) for each item added, where N is the number of elements in the sorted set
Implementation details: Kora stores sorted sets as a HashMap mapping members to scores, maintaining efficient lookups while providing sorted access through on-demand sorting operations.
ZREM
Remove one or more members from a sorted set.
Syntax
ZREM key member [member ...]
The key of the sorted set
One or more members to remove
Return value
The number of members removed from the sorted set
Examples
redis-cli> ZADD myzset 1 "one" 2 "two" 3 "three"
(integer) 3
redis-cli> ZREM myzset "two"
(integer) 1
redis-cli> ZRANGE myzset 0 -1 WITHSCORES
1) "one"
2) "1"
3) "three"
4) "3"
Time complexity: O(M*log(N)) where N is the number of elements in the sorted set and M is the number of elements removed
ZSCORE
Get the score associated with a member in a sorted set.
Syntax
The key of the sorted set
Return value
The score of the member (as a string), or null if the member does not exist
Examples
redis-cli> ZADD myzset 1.5 "one"
(integer) 1
redis-cli> ZSCORE myzset "one"
"1.5"
redis-cli> ZSCORE myzset "two"
(nil)
Time complexity: O(1)
ZRANK
Get the rank (index) of a member in a sorted set, ordered from low to high scores.
Syntax
The key of the sorted set
Return value
The rank of the member (0-based), or null if the member does not exist
Examples
redis-cli> ZADD myzset 1 "one" 2 "two" 3 "three"
(integer) 3
redis-cli> ZRANK myzset "three"
(integer) 2
redis-cli> ZRANK myzset "one"
(integer) 0
redis-cli> ZRANK myzset "four"
(nil)
Time complexity: O(N) where N is the number of elements in the sorted set
Implementation details: Kora computes rank by sorting the entire set and finding the position. Members with equal scores are ordered lexicographically.
ZREVRANK
Get the rank (index) of a member in a sorted set, ordered from high to low scores.
Syntax
The key of the sorted set
Return value
The reverse rank of the member (0-based), or null if the member does not exist
Examples
redis-cli> ZADD myzset 1 "one" 2 "two" 3 "three"
(integer) 3
redis-cli> ZREVRANK myzset "one"
(integer) 2
redis-cli> ZREVRANK myzset "three"
(integer) 0
Time complexity: O(N) where N is the number of elements in the sorted set
ZCARD
Get the number of members in a sorted set.
Syntax
The key of the sorted set
Return value
The number of members in the sorted set, or 0 if the key does not exist
Examples
redis-cli> ZADD myzset 1 "one" 2 "two"
(integer) 2
redis-cli> ZCARD myzset
(integer) 2
Time complexity: O(1)
ZCOUNT
Count the members in a sorted set with scores within a given range.
Syntax
The key of the sorted set
Minimum score (inclusive)
Maximum score (inclusive)
Return value
The number of members with scores in the specified range
Examples
redis-cli> ZADD myzset 1 "one" 2 "two" 3 "three"
(integer) 3
redis-cli> ZCOUNT myzset -inf +inf
(integer) 3
redis-cli> ZCOUNT myzset 1 2
(integer) 2
Time complexity: O(N) where N is the number of elements in the sorted set
ZINCRBY
Increment the score of a member in a sorted set.
Syntax
ZINCRBY key increment member
The key of the sorted set
The amount to increment (can be negative)
The member whose score to increment
Return value
The new score of the member after incrementing
Examples
redis-cli> ZADD myzset 1 "one"
(integer) 1
redis-cli> ZINCRBY myzset 2 "one"
"3"
redis-cli> ZINCRBY myzset -1 "one"
"2"
redis-cli> ZINCRBY myzset 5 "two"
"5"
Time complexity: O(log(N)) where N is the number of elements in the sorted set
Implementation details: If the member does not exist, it is created with an initial score of 0 before applying the increment.
ZLEXCOUNT
Count the members in a sorted set within a lexicographical range.
Syntax
The key of the sorted set
Minimum lexicographical value. Use - for negative infinity, [value for inclusive, (value for exclusive
Maximum lexicographical value. Use + for positive infinity, [value for inclusive, (value for exclusive
Return value
The number of members in the specified lexicographical range
Examples
redis-cli> ZADD myzset 0 "a" 0 "b" 0 "c" 0 "d" 0 "e"
(integer) 5
redis-cli> ZLEXCOUNT myzset - +
(integer) 5
redis-cli> ZLEXCOUNT myzset [b [d
(integer) 3
Time complexity: O(N) where N is the number of elements in the sorted set
Implementation details: This command is useful when all members have the same score and you want to perform range queries by member name.
ZMSCORE
Get the scores of multiple members in a sorted set.
Syntax
ZMSCORE key member [member ...]
The key of the sorted set
One or more members to query
Return value
Array of scores (as strings) or null values, one for each member in the same order
Examples
redis-cli> ZADD myzset 1 "one" 2 "two"
(integer) 2
redis-cli> ZMSCORE myzset "one" "two" "three"
1) "1"
2) "2"
3) (nil)
Time complexity: O(N) where N is the number of members being looked up
ZPOPMAX
Remove and return members with the highest scores in a sorted set.
Syntax
The key of the sorted set
Number of members to pop (default: 1)
Return value
Array of member-score pairs (member, score, member, score, …) for the popped elements
Examples
redis-cli> ZADD myzset 1 "one" 2 "two" 3 "three"
(integer) 3
redis-cli> ZPOPMAX myzset
1) "three"
2) "3"
redis-cli> ZPOPMAX myzset 2
1) "two"
2) "2"
3) "one"
4) "1"
Time complexity: O(N*log(M)) where N is the number of elements to pop and M is the number of elements in the sorted set
ZPOPMIN
Remove and return members with the lowest scores in a sorted set.
Syntax
The key of the sorted set
Number of members to pop (default: 1)
Return value
Array of member-score pairs (member, score, member, score, …) for the popped elements
Examples
redis-cli> ZADD myzset 1 "one" 2 "two" 3 "three"
(integer) 3
redis-cli> ZPOPMIN myzset
1) "one"
2) "1"
redis-cli> ZPOPMIN myzset 2
1) "two"
2) "2"
3) "three"
4) "3"
Time complexity: O(N*log(M)) where N is the number of elements to pop and M is the number of elements in the sorted set
ZRANDMEMBER
Get one or multiple random members from a sorted set.
Syntax
ZRANDMEMBER key [count [WITHSCORES]]
The key of the sorted set
Number of members to return. Positive values return distinct members, negative values allow duplicates
Include scores in the output
Return value
Without count: a single random member, or null if empty. With count: array of members (and scores if WITHSCORES)
Examples
redis-cli> ZADD myzset 1 "one" 2 "two" 3 "three"
(integer) 3
redis-cli> ZRANDMEMBER myzset
"two"
redis-cli> ZRANDMEMBER myzset 2 WITHSCORES
1) "one"
2) "1"
3) "three"
4) "3"
redis-cli> ZRANDMEMBER myzset -5
1) "one"
2) "two"
3) "one"
4) "three"
5) "two"
Time complexity: O(N) where N is the number of members returned
ZRANGE
Return a range of members in a sorted set by index.
Syntax
ZRANGE key start stop [WITHSCORES]
The key of the sorted set
Start index (0-based, negative values count from the end)
Stop index (inclusive, negative values count from the end)
Include scores in the output
Return value
Array of members (and scores if WITHSCORES) in the specified range, ordered by score ascending
Examples
redis-cli> ZADD myzset 1 "one" 2 "two" 3 "three"
(integer) 3
redis-cli> ZRANGE myzset 0 -1
1) "one"
2) "two"
3) "three"
redis-cli> ZRANGE myzset 0 1 WITHSCORES
1) "one"
2) "1"
3) "two"
4) "2"
redis-cli> ZRANGE myzset -2 -1
1) "two"
2) "three"
Time complexity: O(log(N)+M) where N is the number of elements in the sorted set and M is the number of elements returned
Implementation details: Members with equal scores are ordered lexicographically.
ZRANGEBYLEX
Return a range of members in a sorted set by lexicographical order.
Syntax
ZRANGEBYLEX key min max [LIMIT offset count]
The key of the sorted set
Minimum value. Use - for negative infinity, [value for inclusive, (value for exclusive
Maximum value. Use + for positive infinity, [value for inclusive, (value for exclusive
Optional offset and count to limit results
Return value
Array of members in the specified lexicographical range
Examples
redis-cli> ZADD myzset 0 "a" 0 "b" 0 "c" 0 "d" 0 "e"
(integer) 5
redis-cli> ZRANGEBYLEX myzset - [c
1) "a"
2) "b"
3) "c"
redis-cli> ZRANGEBYLEX myzset [aaa (g
1) "b"
2) "c"
3) "d"
4) "e"
redis-cli> ZRANGEBYLEX myzset - + LIMIT 2 2
1) "c"
2) "d"
Time complexity: O(log(N)+M) where N is the number of elements in the sorted set and M is the number of elements returned
ZRANGEBYSCORE
Return a range of members in a sorted set by score.
Syntax
ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
The key of the sorted set
Minimum score (inclusive)
Maximum score (inclusive)
Include scores in the output
Optional offset and count to limit results
Return value
Array of members (and scores if WITHSCORES) with scores in the specified range, ordered by score ascending
Examples
redis-cli> ZADD myzset 1 "one" 2 "two" 3 "three"
(integer) 3
redis-cli> ZRANGEBYSCORE myzset -inf +inf
1) "one"
2) "two"
3) "three"
redis-cli> ZRANGEBYSCORE myzset 1 2
1) "one"
2) "two"
redis-cli> ZRANGEBYSCORE myzset 1 2 WITHSCORES
1) "one"
2) "1"
3) "two"
4) "2"
redis-cli> ZRANGEBYSCORE myzset 0 10 LIMIT 1 2
1) "two"
2) "three"
Time complexity: O(log(N)+M) where N is the number of elements in the sorted set and M is the number of elements returned
ZREVRANGE
Return a range of members in a sorted set by index, with scores ordered from high to low.
Syntax
ZREVRANGE key start stop [WITHSCORES]
The key of the sorted set
Start index (0-based, negative values count from the end)
Stop index (inclusive, negative values count from the end)
Include scores in the output
Return value
Array of members (and scores if WITHSCORES) in the specified range, ordered by score descending
Examples
redis-cli> ZADD myzset 1 "one" 2 "two" 3 "three"
(integer) 3
redis-cli> ZREVRANGE myzset 0 -1
1) "three"
2) "two"
3) "one"
redis-cli> ZREVRANGE myzset 0 1 WITHSCORES
1) "three"
2) "3"
3) "two"
4) "2"
Time complexity: O(log(N)+M) where N is the number of elements in the sorted set and M is the number of elements returned
ZREVRANGEBYLEX
Return a range of members in a sorted set by lexicographical order, in reverse.
Syntax
ZREVRANGEBYLEX key max min [LIMIT offset count]
The key of the sorted set
Maximum value. Use + for positive infinity, [value for inclusive, (value for exclusive
Minimum value. Use - for negative infinity, [value for inclusive, (value for exclusive
Optional offset and count to limit results
Return value
Array of members in the specified lexicographical range, in reverse order
Examples
redis-cli> ZADD myzset 0 "a" 0 "b" 0 "c" 0 "d" 0 "e"
(integer) 5
redis-cli> ZREVRANGEBYLEX myzset [c -
1) "c"
2) "b"
3) "a"
redis-cli> ZREVRANGEBYLEX myzset (g [aaa
1) "e"
2) "d"
3) "c"
4) "b"
Time complexity: O(log(N)+M) where N is the number of elements in the sorted set and M is the number of elements returned
ZREVRANGEBYSCORE
Return a range of members in a sorted set by score, with scores ordered from high to low.
Syntax
ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count]
The key of the sorted set
Maximum score (inclusive)
Minimum score (inclusive)
Include scores in the output
Optional offset and count to limit results
Return value
Array of members (and scores if WITHSCORES) with scores in the specified range, ordered by score descending
Examples
redis-cli> ZADD myzset 1 "one" 2 "two" 3 "three"
(integer) 3
redis-cli> ZREVRANGEBYSCORE myzset +inf -inf
1) "three"
2) "two"
3) "one"
redis-cli> ZREVRANGEBYSCORE myzset 2 1
1) "two"
2) "one"
redis-cli> ZREVRANGEBYSCORE myzset 2 1 WITHSCORES
1) "two"
2) "2"
3) "one"
4) "1"
redis-cli> ZREVRANGEBYSCORE myzset 10 0 LIMIT 1 2
1) "two"
2) "one"
Time complexity: O(log(N)+M) where N is the number of elements in the sorted set and M is the number of elements returned
ZSCAN
Incrementally iterate sorted set members and their scores.
Syntax
ZSCAN key cursor [MATCH pattern] [COUNT count]
The key of the sorted set
The cursor position (use 0 to start)
Glob pattern to filter members
Hint for how many members to return
Return value
Array with two elements: [0] next cursor, [1] array of member-score pairs
Examples
redis-cli> ZADD myzset 1 "a" 2 "b" 3 "c" 4 "d" 5 "e" 6 "f"
(integer) 6
redis-cli> ZSCAN myzset 0 MATCH a* COUNT 5
1) "0"
2) 1) "a"
2) "1"
redis-cli> ZSCAN myzset 0 COUNT 3
1) "3"
2) 1) "a"
2) "1"
3) "b"
4) "2"
5) "c"
6) "3"
Time complexity: O(1) for every call, O(N) for a complete iteration
Implementation details: The cursor is an offset into a lexicographically sorted list of members. When the returned cursor is 0, the iteration is complete.
Redis Compatibility
Kora implements all standard Redis sorted set commands with identical semantics. Sorted sets are stored using a HashMap for efficient score lookups, with sorting performed on-demand for range queries. Members with equal scores are ordered lexicographically for consistent ordering.