Redis Sorted Sets
--- Home
- HTML
- JavaScript
- CSS
- Vue
- React
- Python3
- Java
- C
- C++
- C#
- AI
- Go
- SQL
- Linux
- VS Code
- Bootstrap
- Git
- Local Bookmarks
Redis Tutorial
Redis Tutorial Redis Introduction Redis Installation Redis Configuration Redis Data TypesRedis Commands
Redis Commands Redis Keys Redis Strings Redis Hashes Redis Lists Redis Sets Redis Sorted Sets Redis HyperLogLog Redis Publish Subscribe Redis Transactions Redis Scripting Redis Connection Redis Server Redis GEO Redis StreamAdvanced Redis Tutorial
Redis Backup and Recovery Redis Security Redis Performance Testing Redis Client Connection Redis Pipelining Redis Partitioning Java Using Redis PHP Using Redis Redis Sets Redis HyperLogLogRedis Sorted Sets
Redis sorted sets are similar to sets in that they are collections of string type elements and do not allow duplicate members.
The difference is that each element is associated with a double type score. Redis sorts the members in the set based on these scores.
The members in a sorted set are unique, but the scores can be repeated.
Sets are implemented using hash tables, so the complexity for adding, deleting, and finding elements is O(1). The maximum number of members in a set is 2^32 - 1 (4294967295, each set can store over 4 billion members).
Example
redis 127.0.0.1:6379> ZADD tutorialkey 1 redis (integer) 1 redis 127.0.0.1:6379> ZADD tutorialkey 2 mongodb (integer) 1 redis 127.0.0.1:6379> ZADD tutorialkey 3 mysql (integer) 1 redis 127.0.0.1:6379> ZADD tutorialkey 3 mysql (integer) 0 redis 127.0.0.1:6379> ZADD tutorialkey 4 mysql (integer) 0 redis 127.0.0.1:6379> ZRANGE tutorialkey 0 10 WITHSCORES 1) "redis" 2) "1" 3) "mongodb" 4) "2" 5) "mysql" 6) "4"
In the above example, we used the command ZADD to add three values to the Redis sorted set and associate 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 Add one or more members to an ordered set, or update the score of existing members |
| 2 | ZCARD key Get the number of members in an ordered set |
| 3 | ZCOUNT key min max Count the number of members within a specified score range in an ordered set |
| 4 | ZINCRBY key increment member Increment the score of a member in an ordered set |
YouTip