Redis ZRANGEBYSCORE Replacement

Starting with Redis 6.2.0, the ZRANGE command added the REV, BYSCORE, BYLEX and LIMIT options. The addition of the first three options 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 ZRANGEBYSCORE command when we need to return a sorted set by score. Instead, we should use the ZRANGE command with the BYSCORE argument.

Example

Here’s an example of the replacement for the ZRANGEBYSCORE command:

ZRANGE scores 220 550 BYSCORE WITHSCORES

Result:

1) "user:1"
2) "220"
3) "user:3"
4) "330"
5) "user:5"
6) "440"
7) "user:4"
8) "550"

We can see that only those elements with a score between 220 and 550 are returned (inclusive).

That has the exact same effect as using the (deprecated) ZRANGEBYSCORE command like this:

ZRANGEBYSCORE scores 220 550 WITHSCORES

Result:

1) "user:1"
2) "220"
3) "user:3"
4) "330"
5) "user:5"
6) "440"
7) "user:4"
8) "550"

So in summary, instead of using the ZRANGEBYSCORE command, use the ZRANGE command with the BYSCORE option.