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.
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> 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 command returns an integer of the number of keys that exists in the database.
127.0.0.1:6379> 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:
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> 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
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,
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.
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
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!!!