25 redis commands you need to know


Redis is one of the fastest key-value pair and in-memory databases that we can make use of for our applications. It is mostly preferred by the developers due to its speed, replication facility that is present built-in, and the persistence of data on-disk. All the operations of Redis are carried out in memory which results in fast performance. We can make the use of String, List, hash, set, and sorted set that are the data structures in Redis.

In this article, we will be having a look at 25 commands of Redis that you need to know which we will be executing on the Redis command line. Also, if you want to get the latest Redis installation on your device, please feel free to visit this site, download the file necessary and install it.


Ping the Redis server

In order to confirm if Redis is present in your system, you will firstly need to navigate to the folder where you have installed it. In windows, it is by default, C:\Program Files\Redis. Using the below cd command, you can do that
cd C:\Program Files\Redis

The output of the execution of the above command results in the following

 

Now, in order to ping Redis, we can type in the command on the command line shown below –

redis-cli ping

After the execution of the above command, we get the following output provided if we are able to get in connection with the Redis server –

 


Open Redis client and its connection

In order to connect to the Redis server, we will require a Redis client which can be connected by using command
redis-cli
When we execute this command, we will be connected with the local Redis server where we can further execute any required command. The output of the above command on the command line is as shown in the below image –

As we can see the default host address of the local server is 127.0.0.1 while the default port address used for establishing the connection with the server of Redis is 6379.

 


Connecting with the remote server.

When your Redis server is present remotely that is not in your network then, you can make use of the below syntax of redis-cli command –
redis-cli -h host_address_to_connect -p port_address -a corresponding_password
In the above syntax, the host address to connect to is the host on which our Redis server is present and the port address will be the port number on which the connection is to be established. For example, if my Redis host is present on the host with address 127.0.0.1 and the port number set for it is 6379 using which I can establish a connection having a password of root then my command to connect to the remote server would be –

redis-cli -h 127.0.0.1 -p 6379 -a "root"

The output of the execution of the above command is as shown below connecting us to our Redis server where we are free to run other commands –

 


Adding key

Redis contains various key commands that are useful for the management of keys. The syntax for using the keys command is Redis is as shown below –
“Name of command” “name of key”
Let us understand how we can add and set the value to a key in Redis. For this, we can make use of SET as the name of the command, and the name of the key can be anything whichever key we want to set. For example, we can set the “bitarraykey” key name and value as “sampleTutorial”, then we can make use of the below command –

SET bitarraykey sampleTutorial

Which gives the following output –

 


Retrieve the key-value

To fetch the value of the key present in the Redis server, we can make use of GET command as shown below –
GET name of the key
For example, to retrieve the value of “bitarraykey”, we can run the below command –
GET bitarraykey

Which gives the following output –

 


Substitute the existing value of key

When we have to set a new string value to the key and remove the older one, we can make use of the below command syntax –

GETSET name of key value to be assigned
For example, if we have to change the value of “bitarraykey” key from “sampleTutorial” to “sampleRedisTutorial”, then we can execute the below command –

GETSET bitarraykey samplRedisTutorial

Which gives the following output –

It returns the older value of the key. Let us confirm if the new value is set to it properly or not by using the get command –
GET bitarraykey

This results in the following outdated value containing output –

 


Deleting the existing key

For doing so, we can make the use of syntax –

DEL
For example, to delete the “bitarraykey” key that we created, we can execute the following command –

DEL bitarraykey

Which gives the following output

If the key is present, it returns integer 1 else it returns integer 0 as shown below when tried to delete one more time

 


Increment the key value.

When we have a key storing integer value in it. We can increase its value by using INCR name of key
Let us create one key as “articleCount” using the below command –

SET articleCount 5

Which give the following output

Now, to increase its value, we can execute the command –

INCR articleCount

That gives the below output containing and increased value by one which is 6 as shown below

Decrement the value of key

For keys containing integer values, we can use this to decrease its value by one. For example, right now, the value of “articleCount” is 6, when we use the below command –

DECR articleCount

We can see the below output containing one value less than is 5 for “articleCount”

 


Increase the key value by a particular integer value.

Suppose, we want to increase the value of “articleCount” by 10 then we can execute the below command –
INCRBY articleCount 10

This results in the following output with the increased value of article count by 10

 


Decrease the value by a certain integer.

If we want the “articleCount” to decrease by 7 then we can execute the below command –

DECRBY articleCount 7

Which gives the following output


Add the content to the value of key

In order to append additional value to the key value, we can use the APPEND command. For example to add “ForBeginners” to “bitarraykey” value then we can execute the below command –

APPEND bitarraykey ForBeginners

This results in the following output

Now to check the contents of “bitarraykey”, lets execute –

GET bitarraykey

Which gives the output

 


To retrieve the length of the string stored in key

In order to get the length of the value of a particular key, we can execute the STRLEN command in Redis. For example, for “bitarraykey” we can execute the below command –

STRLEN bitarraykey

This results in the following output as it contains 30 characters

 


Create hash and add multiple fields and values pairs

HMSET command is used for creating HashSet containing multiple key-value pairs in it. For example, using the below command –

HMSET BitArray article_topic "25 Redis commands to know" written_by "bitarray user"

Which gives the following output


Retrieve the value of a specific field in the hash.

We can make use of the HGET name of the key name of the field command to retrieve the value of a particular field inside HashSet. For example, to get the value of “written_by” key in “BitArray” hash, we can execute the below command –

HGET BitArray written_by

Which gives the following output retrieving the value of field written_by


Get all the fields and value pairs

HGETALL name of key command helps to do that. For example, if we want to get all the filed value pairs of “BitArray” HashSet then we can execute the below command –

HGETALL BitArray

This results in the following output

 


Retrieve all the fields of the hash

We can make use of the HKEYS command here. In the case of our hash “BitArray”, we can execute the below command –

HKEYS BitArray

This results in the following output –


Retrieve all the values present in the hash.

For this, we will make use of the HVALS  command. For “BitArray”, we can execute the below command –

HVALS BitArray

This results in listing all the values present in it as shown below –


Retrieve count of fields present in Hash

HLEN name of key command can be used for this purpose. For “bitarray”, if we implement this command then our command would be –

HLEN BitArray

This results in the following output containing 2 as only two fields are present in our hash.


Delete the existing single or multiple fields from the hash.

HDEL name of the key name of field1 name of field2 … can be used for this. When we have to delete “written_by” key from “BitArray” hash, the command used will be –

HDEL BitArray written_by

This results in the following output as one key was deleted

If the key would not have existed then it would have returned integer 0.


Add values in a list

LPUSH name of the list name of value can be used to do this. Suppose, we will create one list named “bitarrayList” where we will add the value Redis then our command will be –

LPUSH bitarrayList Redis

Which gives the following output

 


Retrieve the element at a specific index

LINDEX is used for getting the element inside a list present at a specific index. For example, to retrieve the element present at 1 index in “bitarrayList”, we can execute the following command –

LINDEX bitarrayList 1

This results in the following output –

 


Retrieve the length of the particular list

LLEN is used for fetching the count of elements present inside a particular list. For example, for “bitarrayList”

LLEN bitarrayList

Gives the following output as it contains 2 elements


Remove the element and fetch the first element of the list

LPOP name of the list command is used for this purpose. For example, for “bitarrayList” command will be –

LPOP bitarrayList

This results in the following output

The first element of Redis got removed and now the first element will be the next one which was a database and hence it returned the new first element.

 


Close the existing connection with the Redis server

QUIT command is used for this purpose which results in exiting from the Redis prompt and server we were connected to. For example, in our local connection case, executing these commands results in following

Categories

+ There are no comments

Add yours