Redis

Redis CLI List Keys

Redis is an open-source in-memory data structure store. It stores the data in key-value pairs. Redis is high-performance and scalable, suitable for the implementation of different situationss like database, cache, message broker, and more.

In this guide, we will have a look at the listing keys on the Redis database.

Prerequisites

To perform the steps demonstrated in this guide, you will need the following components prepared:

Redis Keys

Redis stores the data in key-value pairs. While the key is an arbitrary string, the value it refers to can be a string, list, hash, stream, bit array, etc. It allows incredible flexibility to developers to implement Redis in whatever manner they see fit.

To work with keys, launch the Redis CLI first:

$ redis-cli

Use the SET command to create a key-value pair:

$ SET <key> <value>


To retrieve the stored value, use the GET command:

$ GET <key>

Listing Redis Keys

There are a couple of ways that we can inquire about all the keys stored currently on the Redis server. Either way, we’re going to use the Redis command-line tool (redis-cli) to interface with the Redis server.

Method 1 – From Within the Redis-Cli Shell

Launch the following Redis shell:

$ redis-cli

To search for keys, Redis comes with the KEYS command. It takes a regex pattern as the parameter.

To print all the keys matching a certain pattern, the KEYS command looks like this:

$ KEYS <regex_pattern>

Now, as for the pattern to match, we can use the asterisk (*) as a wildcard. Essentially, it matches with all the keys that are registered in the Redis server. Thus, the output is the list of all the keys. Try it out using the following command:

$ KEYS *

Note that depending on the number of keys registered in the server, this list length will easily reach very long. To keep things under control, it’s strongly recommended to set the keys in an organized manner. This way, you can scan a specific group of keys, reducing the size of the output.

$ KEYS hello_*

Method 2 – From Outside the Redis-Cli Shell

While we can check the list of keys from the redis-cli shell, it’s not a convenient method when you’re trying to implement the list in a custom program/script. To solve this issue, we can invoke the commands outside the redis-cli shell.

Have a look at the following command:

$ echo “KEYS hello_*| redis-cli

Here, we passed the desired command to redis-cli and the output is returned and stored at STDOUT. Then, we can manipulate the contents using awk, sed, etc. to get a cleaner look.

Conclusion

In this guide, we explored a couple of methods to retrieve the list of keys from the Redis server through the command-line interface (redis-cli). We showcased how to list the keys from within the Redis shell. We also learned how to do that from outside the shell for better integration with programs/scripts.

Interested in learning more about Redis? The Redis sub-section contains numerous guides on the various aspects of Redis. Learn more about using Redis with LUA scripts, Ruby, and C++, etc.

About the author

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.