Easy Tutorial
❮ Hashes Hsetnx Keys Persist ❯

Redis Sorted Set

Redis Sorted Set, like a Set, is an in-memory data structure that stores a collection of unique string elements. However, each element in a Sorted Set is associated with a floating-point score, which is used to maintain the elements in a sorted order from the smallest to the largest score.

While the elements in the Sorted Set are unique, the scores can be repeated.

Sorted Sets are implemented using hash tables, which enable O(1) time complexity for operations like adding, removing, and looking up elements. The maximum number of elements in a Sorted Set is 2^32 - 1 (4,294,967,295).

Example

redis 127.0.0.1:6379> ZADD tutorialprokey 1 redis
(integer) 1
redis 127.0.0.1:6379> ZADD tutorialprokey 2 mongodb
(integer) 1
redis 127.0.0.1:6379> ZADD tutorialprokey 3 mysql
(integer) 1
redis 127.0.0.1:6379> ZADD tutorialprokey 3 mysql
(integer) 0
redis 127.0.0.1:6379> ZADD tutorialprokey 4 mysql
(integer) 0
redis 127.0.0.1:6379> ZRANGE tutorialprokey 0 10 WITHSCORES

1) "redis"
2) "1"
3) "mongodb"
4) "2"
5) "mysql"
6) "4"

In the above example, we used the ZADD command to add three values to the Redis Sorted Set and associated them with scores.


Redis Sorted Set Commands

The following table lists the basic commands for Redis Sorted Sets:

Number Command and Description
1 ZADD key score1 member1 [score2 member2] <br>Adds one or more members to a sorted set, or updates the score for members that already exist.
2 ZCARD key <br>Gets the number of members in a sorted set.
3 ZCOUNT key min max <br>Counts the members in a sorted set with scores within the given range.
4 ZINCRBY key increment member <br>Increments the score of a member in a sorted set by the given increment.
5 ZINTERSTORE destination numkeys key [key ...] <br>Computes the intersection of one or more sorted sets and stores the result in a new sorted set.
6 ZLEXCOUNT key min max <br>Counts the number of members in a sorted set within a specified lexicographical range.
7 ZRANGE key start stop [WITHSCORES] <br>Returns a range of members in a sorted set by index.
8 ZRANGEBYLEX key min max [LIMIT offset count] <br>Returns a range of members in a sorted set by lexicographical range.
9 ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT] <br>Returns a range of members in a sorted set by score.
10 ZRANK key member <br>Returns the index of a member in a sorted set.
11 ZREM key member [member ...] <br>Removes one or more members from a sorted set.
12 ZREMRANGEBYLEX key min max <br>Removes all members in a sorted set within the given lexicographical range.
13 ZREMRANGEBYRANK key start stop <br>Removes all members in a sorted set within the given index range.
14 ZREMRANGEBYSCORE key min max <br>Remove all members in a sorted set within the given score range
15 ZREVRANGE key start stop [WITHSCORES] <br>Return a range of members in a sorted set, by index, with scores ordered from high to low
16 ZREVRANGEBYSCORE key max min [WITHSCORES] <br>Return a range of members in a sorted set, by score, with scores ordered from high to low
17 ZREVRANK key member <br>Determine the index of a member in a sorted set, with scores ordered from high to low
18 ZSCORE key member <br>Get the score of a member in a sorted set
19 ZUNIONSTORE destination numkeys key [key ...] <br>Compute the union of one or more sorted sets and store the result in a new key
20 ZSCAN key cursor [MATCH pattern] [COUNT count] <br>Iterate elements in a sorted set (including element members and their scores)
❮ Hashes Hsetnx Keys Persist ❯