Starting with Redis 6.2.0, the ZRANGE
command added the REV
, BYSCORE
, BYLEX
and LIMIT
options. The addition of the first three means that the ZRANGE
command can now do what the ZREVRANGE
, ZRANGEBYSCORE
, ZREVRANGEBYSCORE
, ZRANGEBYLEX
and ZREVRANGEBYLEX
commands can do.
As a result, those commands are now deprecated (as of Redis 6.2.0).
Therefore, we should no longer use the ZREVRANGE
command when we need to return a sorted set in descending order. Instead, we should use the ZRANGE
command with the REV
argument.
Example
Here’s an example of the replacement for the ZREVRANGE
command:
ZRANGE scores 0 -1 REV WITHSCORES
Result:
1) "user:6" 2) "660" 3) "user:4" 4) "550" 5) "user:5" 6) "440" 7) "user:3" 8) "330" 9) "user:1" 10) "220" 11) "user:2" 12) "110"
We can see that the elements are ordered from the highest to the lowest score.
That has the exact same effect as using the (deprecated) ZREVRANGE
command like this:
ZREVRANGE scores 0 -1 WITHSCORES
Result:
1) "user:6" 2) "660" 3) "user:4" 4) "550" 5) "user:5" 6) "440" 7) "user:3" 8) "330" 9) "user:1" 10) "220" 11) "user:2" 12) "110"
So in summary, instead of using the ZREVRANGE
command, use the ZRANGE
command with the REV
option.