Redis Sorted Sets are similar to Redis Sets with the unique feature of values stored in a set. The difference is, every member of a Sorted Set is associated with a score, that is used in order to take the sorted set ordered, from the smallest to the greatest score.
In Redis sorted set, add, remove, and test for the existence of members in O(1) (constant time regardless of the number of elements contained inside the set). Maximum length of a list is 232 – 1 elements (4294967295, more than 4 billion of elements per set).
Example
redis 127.0.0.1:6379> ZADD Adglob 1 redis (integer) 1 redis 127.0.0.1:6379> ZADD Adglob 2 mongodb (integer) 1 redis 127.0.0.1:6379> ZADD Adglob 3 mysql (integer) 1 redis 127.0.0.1:6379> ZADD Adglob 3 mysql (integer) 0 redis 127.0.0.1:6379> ZADD Adglob 4 mysql (integer) 0 redis 127.0.0.1:6379> ZRANGE Adglob 0 10 WITHSCORES 1) "redis" 2) "1" 3) "mongodb" 4) "2" 5) "mysql" 6) "4"
In the above example, three values are inserted with its score in Redis sorted set named ‘Adglob’ by the command ZADD.
Redis Sorted Sets Commands
Following table lists some basic commands related to sorted sets.
1 | ZADD key https://adglob.in/blog/redis-sorted-set-zadd-command/ Adds one or more members to a sorted set, or updates its score, if it already exists |
2 | ZCARD keyhttps://adglob.in/blog/redis-sorted-set-zcard-command/ Gets the number of members in a sorted set |
3 | ZCOUNT key https://adglob.in/blog/redis-sorted-set-zcount-command/ Counts the members in a sorted set with scores within the given values |
4 | ZINCRBY keyhttps://adglob.in/blog/redis-sorted-set-zincrby-command/ Increments the score of a member in a sorted set |
5 | ZINTERSTORE https://adglob.in/blog/redis-sorted-set-zinterstore-command/ Intersects multiple sorted sets and stores the resulting sorted set in a new key |
6 | ZLEXCOUNT keyhttps://adglob.in/blog/redis-sorted-set-zlexcount-command/ Counts the number of members in a sorted set between a given lexicographical range |
7 | ZRANGE key https://adglob.in/blog/redis-sorted-set-zrange-command/ Returns a range of members in a sorted set, by index |
8 | ZRANGEBYLEX keyhttps://adglob.in/blog/redis-sorted-set-zrangebylex-command/ Returns a range of members in a sorted set, by lexicographical range |
9 | ZRANGEBYSCORE keyhttps://adglob.in/blog/redis-sorted-set-zrangebyscore-command/ Returns a range of members in a sorted set, by score |
10 | ZRANK keyhttps://adglob.in/blog/redis-sorted-set-zrank-command/ Determines the index of a member in a sorted set |
11 | ZREM keyhttps://adglob.in/blog/redis-sorted-set-zrank-command-2/ Removes one or more members from a sorted set |
12 | ZREMRANGEBYLEX key https://adglob.in/blog/redis-sorted-set-zremrangebylex-command/ Removes all members in a sorted set between the given lexicographical range |
13 | ZREMRANGEBYRANK key https://adglob.in/blog/redis-sorted-set-zremrangebyrank-command/ Removes all members in a sorted set within the given indexes |
14 | ZREMRANGEBYSCORE key https://adglob.in/blog/redis-sorted-set-zremrangebyscore-command/ Removes all members in a sorted set within the given scores |
15 | ZREVRANGE key https://adglob.in/blog/redis-sorted-set-zrevrange-command/ Returns a range of members in a sorted set, by index, with scores ordered from high to low |
16 | ZREVRANGEBYSCORE keyhttps://adglob.in/blog/redis-sorted-set-zrevrangebyscore-command/ Returns a range of members in a sorted set, by score, with scores ordered from high to low |
17 | ZREVRANK keyhttps://adglob.in/blog/redis-sorted-set-zrevrank-command/ Determines the index of a member in a sorted set, with scores ordered from high to low |
18 | ZSCORE keyhttps://adglob.in/blog/redis-sorted-set-zscore-command/ Gets the score associated with the given member in a sorted set |
19 | ZUNIONSTORE https://adglob.in/blog/redis-sorted-set-zunionstore-command/ Adds multiple sorted sets and stores the resulting sorted set in a new key |
20 | ZSCAN key cursor https://adglob.in/blog/redis-sorted-set-zscan-command/ Incrementally iterates sorted sets elements and associated scores |