Sorted Sets
What is a Redis Sorted Set?
Section titled “What is a Redis Sorted Set?”A Sorted Set is like a Set but each member carries a floating-point score. Redis keeps the members sorted by score at all times, with O(log N) insertions and range queries. If two members share the same score, they are sorted lexicographically.
ZADD — adding members with scores
Section titled “ZADD — adding members with scores”ZADD key score member [score member ...] adds members. If a member already exists its score is updated. Returns the number of new members added.
127.0.0.1:6379> ZADD game:scores 4200 "alice" 3800 "bob" 5100 "carol"(integer) 3127.0.0.1:6379> ZADD game:scores 4700 "alice"(integer) 0127.0.0.1:6379> ZSCORE game:scores "alice""4700"ZADD game:scores 4200 "alice" 3800 "bob" 5100 "carol"
ZADD game:scores 4700 "alice"
ZSCORE game:scores "alice"ZRANGE and ZREVRANGE — range by rank
Section titled “ZRANGE and ZREVRANGE — range by rank”ZRANGE key start stop [WITHSCORES] returns members in ascending score order (lowest first). ZREVRANGE returns them in descending order (highest first). Indices are 0-based; -1 means the last member.
127.0.0.1:6379> ZADD leaderboard 900 "carol" 1500 "alice" 2100 "bob" 750 "dave"(integer) 4127.0.0.1:6379> ZRANGE leaderboard 0 -1 WITHSCORES1) "dave"2) "750"3) "carol"4) "900"5) "alice"6) "1500"7) "bob"8) "2100"127.0.0.1:6379> ZREVRANGE leaderboard 0 2 WITHSCORES1) "bob"2) "2100"3) "alice"4) "1500"5) "carol"6) "900"ZADD leaderboard 900 "carol" 1500 "alice" 2100 "bob" 750 "dave"
ZRANGE leaderboard 0 -1 WITHSCORES
ZREVRANGE leaderboard 0 2 WITHSCORESZRANK and ZSCORE — finding a member
Section titled “ZRANK and ZSCORE — finding a member”ZRANK key member returns the 0-based rank of a member in ascending order. ZREVRANK gives rank in descending order. ZSCORE returns the score as a bulk string.
127.0.0.1:6379> ZADD players 300 "x" 600 "y" 900 "z"(integer) 3127.0.0.1:6379> ZRANK players "y"(integer) 1127.0.0.1:6379> ZREVRANK players "y"(integer) 1127.0.0.1:6379> ZSCORE players "z""900"ZADD players 300 "x" 600 "y" 900 "z"
ZRANK players "y"
ZREVRANK players "y"
ZSCORE players "z"ZRANGEBYSCORE — range by score value
Section titled “ZRANGEBYSCORE — range by score value”ZRANGEBYSCORE key min max [WITHSCORES] fetches all members whose score falls between min and max (inclusive). Use -inf and +inf for open bounds.
127.0.0.1:6379> ZADD temps 18.5 "london" 32.1 "bangkok" 24.0 "berlin" 28.7 "tokyo"(integer) 4127.0.0.1:6379> ZRANGEBYSCORE temps 20 30 WITHSCORES1) "berlin"2) "24"3) "tokyo"4) "28.7"ZADD temps 18.5 "london" 32.1 "bangkok" 24.0 "berlin" 28.7 "tokyo"
ZRANGEBYSCORE temps 20 30 WITHSCORESZINCRBY, ZCARD, and ZREM
Section titled “ZINCRBY, ZCARD, and ZREM”ZINCRBY key increment member atomically adds increment to a member’s score.
ZCARD key returns the total number of members.
ZREM key member [member ...] removes one or more members.
127.0.0.1:6379> ZADD votes 10 "post:1" 5 "post:2" 8 "post:3"(integer) 3127.0.0.1:6379> ZINCRBY votes 3 "post:2""8"127.0.0.1:6379> ZCARD votes(integer) 3127.0.0.1:6379> ZREM votes "post:3"(integer) 1127.0.0.1:6379> ZCARD votes(integer) 2ZADD votes 10 "post:1" 5 "post:2" 8 "post:3"
ZINCRBY votes 3 "post:2"
ZCARD votes
ZREM votes "post:3"
ZCARD votes