Development

Redis Check If Key Exis

In this tutorial, you will discover how you can use the Redis built-in commands to check if a key exists in a target Redis database.
Captain Salem 3 min read
Redis Check If Key Exis

Redis is a fantastic free and open-source in-memory database that is widely adopted by high-performance enviroments. It is commonly used as a caching mechanism for large applications such as Twitter, Facebook, GitHub and many more.

Redis also provides a set of tools and simple and interactive command-line interface for interacting with your Redis cluster.

Without much further ado, let's dive in.

Redis Switch Database

The first step step before exploring how to verify if a key exists is selecting the database in which we wish to check for the key.

By default, Redis will selet database at index 0. Hence, any time you execute a command in the Redis CLI without changing the database, the operation is executed on database 0.

To swtich to a specific database, we can use the SELECT command followed by the index of the databas we wish to use.

In our example, we will the database an index 15, i.e. the last database.

SELECT 15

The command should return an output as show:

127.0.0.1:6379> SELECT 15
OK

Once you have selected the database, we can proceed.

Redis Create Key

The next step is to create a key in the Redis database. We will use this key to check learn how to veriy if a key exists.

If you already have a key you wish to use, feel free to skip this section.

Next, log into your Redis cluster and create a simple key as shown in the command:

SET "mykey" "myvalue"

The command should return an output as shown:

127.0.0.1:6379[15]> SET "mykey" "myvalue"
OK

In this case, we are creating a simple key called mykey and its corresponding value of myvalue. Feel free to change the key to any name or value you wish.

Redis Check If Key Exists

Once we have the key we wish to test created, we can use the Redis EXISTS command to check if the key exists.

The command syntax is as shown:

EXISTS [KEY] keys...

For exampe, to check if the key mykey exists in the target database, we can run the command:

EXISTS mykey

The EXISTS command returns an integer of the number of keys that exists in the database.

For example:

127.0.0.1:6379[15]> EXISTS mykey
(integer) 1

The command returns (integer) 1 indicating that the specified key exists in the database.

If you query a key that does not exists, for example:

EXISTS no_there

In this case, there is no key under the name no_there in the Redis server, hence, the command will return an output as shown:

127.0.0.1:6379[15]> EXISTS no_there
(integer) 0

In this case, we can see the command returns (integer) 0 indicating that the key is not found.

We can also check multiple keys by specifying them as a list. For example, let us start by creating other keys using the command:

MSET "key1" "value1" "key2" "value2" "key3" "value3"

In this case, we are using the MSET command to create multiple keys and values.

Now, we can check if multiple keys exists on the database as shown in the command below:

EXISTS key1 key2 key3 key4 key5

In the command above, we are check if key1, key2, key3, key4, and key5 exists in the database.

Running the command above should return:

127.0.0.1:6379> EXISTS key1 key2 key3 key4 key5
(integer) 3

This means that three out of the total specified keys exists in the database. In our case, key4 and key5 do not exist.

And with that, you have successfully learned how to check if a key exists within a Redis database.

Keep in mind that Redis will only check the selected database. Hence, Redis will not check the key's existence in other database indices.

Termination

And with that, we have come to end of this article. in this tutorial, you learned how to check if a key exist within a Redis database using the EXISTS command.

If you have any question, feel free to leave a comment down below.

And to learn more about Redis and how it can help you as a dev, check our Redis tutorial series to learn more.

Thanks for reading and Catch you in the next one!!!

If you enjoy our content, please consider buying us a coffee to support our work:

Share
Comments
More from GeekBits

Join us at GeekBits

Join our members and get a currated list of awesome articles each month.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to GeekBits.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.