Redis ZREVRANGEBYLEX 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 ZREVRANGEBYLEX command. Instead, we should use the ZRANGE command with the BYLEX and REV arguments.

Example

Suppose we create the following sorted set:

ZADD testzset 0 a 0 b 0 c 0 d 0 e 0 f 0 g

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

ZRANGE testzset [d - BYLEX REV

Result:

1) "d"
2) "c"
3) "b"
4) "a"

When doing this the two arguments that specify the range must start with ( or [, in order to specify whether the range interval is exclusive or inclusive. The special values of + and - mean positive and negative infinite strings.

In the above example one of the arguments start with [, which makes it inclusive. In this case, it means that d is included.

In the following example, we use ( to make it exclusive:

ZRANGE testzset (d - BYLEX REV

Result:

1) "c"
2) "b"
3) "a"

The first (inclusive) example has the exact same effect as using the (deprecated) ZREVRANGEBYLEX command like this:

ZREVRANGEBYLEX testzset [d -

Result:

1) "d"
2) "c"
3) "b"
4) "a"

And here’s the exclusive equivalent:

ZREVRANGEBYLEX testzset (d -

Result:

1) "c"
2) "b"
3) "a"

So in summary, instead of using the ZREVRANGEBYLEX command, use the ZRANGE command with the BYLEX and REV options.