How to List Redis Keys

Introduction

Redis is a data structure server that supports various values, however, it is not a simple key-value store. String keys are typically associated to string values in traditional key-value stores. However, the value is not limited to a simple string in Redis, but is able to hold more complex data structures. This tutorial will cover the basics of how to list Redis keys and list all of the key/value pairs.

Prerequisites to List Redis Keys

  • Redis must be properly installed and configured

  • A basic understanding of Redis is required

An Explanation of What Redis is and What it Does

Redis is an in-memory Database, or IMDB, that uses the system’s memory to store entire data sets. This allows the system to expedite quicker response times.

Due to its volatile characteristics, Redis is commonly used for caching. This means that all of the data is erased whenever the Redis server is restarted, unless Redis persistence is enabled.

Redis is considered non-relational database and the entire system is designed with primary key/value pairs at the primitive degree.

It can be and extremely complex task to manage all of the information that exist within a database. Therefore, knowing how to recall information with just a few basic commands is incredibly helpful.

How to Store Information in Redis Keys

Most of the data stored within Redis is stored in a simple key/value pair. A demonstration of how this works can be seen via a command line demonstration or redis-cli using the SET and GET commands.

Below is an example on how to store the details of a laptop, such as the brand and the model. Execute the following command in sequence:

SET brand "DELL"
Ok
SET model "Vostro 5471"
Ok

The above example simply sets a value of “DELL” in the brand key and “Vostro 5471” in the model key respectively, and Ok is just a confirmation of the process.

To view the details, use the GET command in the following format:

GET brand"DELL"

for the Model.

GET model
"Vostro 5471"

As shown above, this retrieves all the values against the keys that were specified previously using the GET command.

How to Use NAMESPACES in Redis

With a basic understanding of how the key/value pair works in Redis, the tutorial will now cover how to use NAMESPACES as another means of storing data in Redis.

To use the namespace statement, a colon ( : ) separator must be followed with a numeric key as shown in the following command sequence:

SET brand:1 "TOSHIBA"
OK
SET model:1 "Portege Z20t"
OK
SET brand:2 "APPLE"
OK
SET model:2 "Macbook Pro 2019"
OK

How to Retrieve Entire Existing Redis Keys

With an understanding how Redis key/value pair works in reference to SET and GET commands, this section will cover how to retrieve, or list, all of the keys that occur in the Redis database.

Executing SET command in Redis, such a those explained the previous section, are performed by creating a distinct key within Redis.

Execute the command KEYS in the following script to retrieve all the present existing keys:

127.0.0.1:6379> KEYS *
1) "brand:1"
2) "model:2"
3) "model"
4) "model:1"
5) "brand"
6) "brand:2"

NOTE: The 127.0.0.1 is the localhost connecting in port :6379

The KEYS followed by an asterisk (*) instructs Redis to find all keys in the system. That is why the result displays the model and brand keys that were set up in the previous section.

How to Retrieve Specific Existing Redis Keys

The previous section explained how to list all existing Redis keys using the command KEYS followed by the wildcard asterisk (*). This section will cover how to search for a specific argument or expression in the Redis key, or even find a specific word.

To look for a specific expression, use the KEYS command followed by the word model or enclose the word model with asterisks at each end as shown in the following example:

127.0.0.1:6379> KEYS *model*
1) "model:2"
2) "model"
3) "model:1"

Conclusion

This tutorial explained how to list Redis keys and all the key/value pairs. The article specifically covered how to store information in Redis keys, how to use NAMESPACES and how to retrieve existing Redis keys. The tutorial also explained the prerequisites to list Redis keys and an explanation of what Redis is and what it does. Remember that a colon ( : ) separator must be followed with a numeric key in order to use the namespace statement.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.