Redis ZRANGEBYLEX 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 ZRANGEBYLEX command. Instead, we should use the ZRANGE command with the BYLEX argument.

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 ZRANGEBYLEX command:

ZRANGE testzset - [d BYLEX

Result:

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

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

Result:

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

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

ZRANGEBYLEX testzset - [d

Result:

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

And here’s the exclusive equivalent:

ZRANGEBYLEX testzset - (d

Result:

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

So in summary, instead of using the ZRANGEBYLEX command, use the ZRANGE command with the BYLEX option.