Redis

Redis Range Queries

Redis has various data types to store and manage multiple data types in your applications. This tutorial provides you with fundamental range commands to retrieve values of a specific type within a range.

Keep in mind that this tutorial is not an introduction to Redis data types. Check our tutorial on the topic to learn more.

We have tested the commands and examples on the latest version of Redis. We recommend replicating a similar environment to get maximum compatibility. For example, commands such as GETRANGE might not work in Redis version 2.0 and lower.

GETRANGE Command

The first range command we will look at is the GETRANGE command. The command takes a key with a string value and returns a substring of the value as determined by start and end indexes.

The syntax of the command is as shown:

GETRANGE key start end

Start by creating a simple key and value in a Redis database as shown in the command below:

127.0.0.1:6379> SET mystring "Hello, world from Redis!"

Next, to get a substring starting from index 3 to index 10, we can do:

127.0.0.1:6379> GETRANGE mystring 3 10

"lo, worl"

Note that the index of the string at index 0 forward. Whitespace characters are also included as valid characters.

You can also negative range to get the last elements in the string. For example, to get the last three indexes, we can do.

127.0.0.1:6379> GETRANGE mystring -3 -1

"is!"

The above syntax gets the last three characters in the string value.

LRANGE Command.

You will often come across this command when working with lists in Redis. The LRANGE command returns a specified number of elements in a Redis list.

It takes the key of the list and a start and stop value. Similarly, the indexing of elements in a Redis list starts at index 0.

Start by creating a simple list as shown in the example command below:

LPUSH databases MongoDB MySQL PostgreSQL Redis Firestore CockroachDB FaunaDB DocumentDB

(integer) 8

The command above creates a Redis list called databases and adds the values specified to it.

You can use the LRANGE command to get elements within a specific range. For example, to get elements from index 0 to 5, we can do.

127.0.0.1:6379> LRANGE databases 0 5

1) "DocumentDB"

2) "FaunaDB"

3) "CockroachDB"

4) "Firestore"

5) "Redis"

6) "PostgreSQL"

You will notice that the elements start with the last inserted element to the first. The list is simply a collection of items sorted by the insertion order.

You can also use a negative index to get the last element from a list.

127.0.0.1:6379> LRANGE databases -5 -1

1) "Firestore"

2) "Redis"

3) "PostgreSQL"

4) "MySQL"

5) "MongoDB"

The command returns the elements from the tail of the list.

If you fetch a range where there are no elements available, the command will return an empty list as:

127.0.0.1:6379> LRANGE databases 10 20

(empty list or set)

ZRANGE Command

The next range command you need to be familiar with is the ZRANGE command. Using the ZRANGE command, you can fetch items within a specific range from a sorted set.

The command takes the sorted set key, a start and stop index value. It returns all the elements within the specified range sorted by the score from the lowest to the highest.

The syntax of the command can be expressed as:

ZRANGE key start stop

To see how the ZRANGE command works, start by creating a sorted list using commands as shown:

127.0.0.1:6379> ZADD languages 1 Rust 2 Python 3 Go 5 C 4 C++ 6 JavaScript 10 Java

(integer) 7

The above command adds the values and their corresponding scores to the sorted set “languages”.

To fetch items in the set within a specific score range, use the ZRANGE command as shown:

127.0.0.1:6379> ZRANGE languages 1 5

1) "Python"

2) "Go"

3) "C++"

4) "C"

5) "JavaScript"

The command returns all elements within that range sorted by score value.

To show the elements and their corresponding score value, use the WITHSCORES option as:

127.0.0.1:6379> ZRANGE languages 0 5 WITHSCORES

1) "Rust"

2) "1"

3) "Python"

4) "2"

5) "Go"

6) "3"

---TRUNCATED---

The command should return the element and its score.

ZREVRANGE

The ZREVRANGE command works similarly to the ZRANGE command. However, it returns the values in reverse order, i.e., the score range starts from the highest to the lowest.

Example

127.0.0.1:6379> ZREVRANGE languages 0 5

1) "Java"

2) "JavaScript"

3) "C"

4) "C++"

5) "Go"

6) "Python"

In this example, the results start from the highest score to the lowest value. You can enable scores using the WITHSCORES option.

127.0.0.1:6379> ZREVRANGE languages 0 5 WITHSCORES

1) "Java"

2) "10"

3) "JavaScript"

4) "6"

5) "C"

6) "5"

7) "C++"

8) "4"

9) "Go"

10) "3"

11) "Python"

12) "2"

Notice how the scores are in reverse order compared to the ZRANGE command output.

Conclusion

This guide gives you a walkthrough of some of the fundamental range commands in Redis. Keep in mind that there are other range commands such as LTRIM, XRANGE, and more. Consider the documentation for commands not discussed in this guide.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list