top of page
Writer's pictureNikhil Upadhyay

Redis CLI: A Comprehensive Guide to Redis-CLI and Redis Commands


Redis (Remote Dictionary Server) is an open-source, in-memory data structure store that can be used as a database, cache, and message broker. Redis-CLI (Command-Line Interface) is a powerful tool that allows you to interact with Redis servers directly from the command line. In this article, we will delve into the world of Redis CLI and explore various Redis commands to manage and manipulate data effectively.


Redis CLI: Getting Started


To begin using Redis CLI, you need to have Redis installed on your system.

Please follow this link from official documentation to install Redis Install


Once installed, you can access Redis CLI by opening a terminal window and typing the command:

redis-cli

This will establish a connection to the default Redis server running on your local machine. If your Redis server is hosted on a different host or port, you can specify the host and port using appropriate flags. By default, it runs on port no - 6379


Redis Data Types


Redis is a magical box for storing data of different types. In this section, we'll cover the different types it supports with an underlined example.


Strings - Your Basic Text Magic: Imagine a box where you can put in any word or sentence. That's a Redis "string." It's like a label you can stick on a box with your favorite word or phrase. Some popular commands to manipulate string values are below:


This will set the string value to a variable and get the value in return

127.0.0.1:6379> SET string_var "Priheni"
OK
127.0.0.1:6379> GET string_var
"Priheni"

This command will append a string to the existing string_var variable in our case

127.0.0.1:6379> APPEND string_var " Blogs"
(integer) 13

The beauty of the APPEND command is that if the variable does not exist it will create the variable and assign a value

127.0.0.1:6379> APPEND new_variable "New String"
(integer) 10
127.0.0.1:6379> GET new_variable
"New String"

This command will return the length of a string variable.

127.0.0.1:6379> STRLEN string_var
(integer) 13

This command will place a new string inside an existing string variable in the specified index position and replace the current string of the same length.

127.0.0.1:6379> SET long_string "This is really long string"
OK
127.0.0.1:6379> SETRANGE long_string 14 " and really "
(integer) 26
127.0.0.1:6379> GET long_string
"This is really and really "

The below command will return values of multiple string variables at once.

127.0.0.1:6379> MGET string_var new_variable long_string some_other
1) "Priheni Blogs"
2) "New String"
3) "This is really and really "
4) (nil)

If you notice in the above command, I added a variable that was not defined earlier. The beauty of this command is it will not return an error as with some programming languages and databases, instead, it will return the value as (nil) which is similar to null or Null or None in other languages.


The other command is MSET which is similar to MGET but instead, it can set multiple variables at once.

127.0.0.1:6379> MSET var1 "first var" var2 "second var" var3 "third var"
OK

Lists - Making To-Do Lists for Redis: Think of a list of your chores for the day. Each chore is a task, and you can add new tasks or remove completed ones. Redis lists work similarly – you can add items or take them out in a jiffy. Some common commands to work with lists in Redis are as below:


LPUSH will add values to the variable. If the variable does not exist, it will create the variable and add the value to it.

127.0.0.1:6379> LPUSH pr_list "first value"
(integer) 1
127.0.0.1:6379> LPUSH pr_list 2
(integer) 2
127.0.0.1:6379> LPUSH pr_list 3.0
(integer) 3

LPUSHX will also add a value similar to LPUSH but it requires the list to exist and have some values in it. If the variable does not exist, instead of returning an error it will simply return (empty array).

127.0.0.1:6379> LPUSHX pr_list 4
(integer) 4
127.0.0.1:6379> LPUSHX pr_list_new 5
(integer) 0
127.0.0.1:6379> LRANGE pr_list 0 0
1) "4"
127.0.0.1:6379> LRANGE pr_list_new 0 0
(empty array)

Next is, you guessed it right -> LRANGE. This command will return the elements inside the list in the specified index range. It accepts 3 parameters - key (which is variable), start index, and stop index as shown in the example above.


The next command is LPOP. This command will remove the first element from the list and print it. This command additionally accepts the count parameter, which when passed will remove the count of elements from the beginning of the list and print them.

127.0.0.1:6379> LPOP pr_list
"4"
127.0.0.1:6379> LPOP pr_list 2
1) "3.0"
2) "2"
127.0.0.1:6379> LRANGE pr_list 0 -1
1) "first value"

LLEN command will return the length of the variable.

127.0.0.1:6379> LLEN pr_list
(integer) 1

Sets - Collecting Unique Treasures: Picture a treasure chest where you collect special coins. Redis sets are like this chest – they store unique things without any repeats. If you try to put the same thing twice, Redis just keeps one. Some useful examples for this data type are as below:

SADD will add value or values to a set variable. It will return an integer specifying the number of elements added by the command.

127.0.0.1:6379> SADD new_set "HELLO" "GUYS" "HOW" "ARE" "YOU" 44
(integer) 6
127.0.0.1:6379> SADD new_set 55
(integer) 1

SCARD will return the number of elements inside a set variable.

127.0.0.1:6379> SCARD new_set
(integer) 7

SMEMBERS will print all the elements inside a set variable.

127.0.0.1:6379> SMEMBERS new_set
1) "ARE"
2) "GUYS"
3) "YOU"
4) "HOW"
5) "55"
6) "HELLO"
7) "44"

SPOP has very similar functionality as LPOP.

Compared to the list, the set has an additional command SREM. This command will remove the specified element from the set.

127.0.0.1:6379> SREM new_set "YOU"
(integer) 1
127.0.0.1:6379> SMEMBERS new_set
1) "GUYS"
2) "HOW"
3) "ARE"
4) "55"
5) "HELLO"
6) "44"
127.0.0.1:6379>

Hashes - Redis Remembers Like Sherlock: Hashes are like a memory game. You have a box for each person or thing, and inside, you remember details about them. Redis hashes do this too – they hold info like names, ages, and more in a neat way. These data types can store and handle values nested inside keys as well. Some commands to display the functionality of hashes are as follows:

HSET can store hash values as different fields inside a variable and HGET can retrieve the values inside fields.

127.0.0.1:6379> HSET pr_hash name "priheni" last_name "blogs"
(integer) 2
127.0.0.1:6379> HGET pr_hash name
"priheni"
127.0.0.1:6379> HGET pr_hash last_name
"blogs"

HGETALL will return everything inside the hash variable in a combined way.

127.0.0.1:6379> HGETALL pr_hash
1) "name"
2) "priheni"
3) "last_name"
4) "blogs"

If you want to return only the fields inside a hash variable, HKEYS will do it for you. Or if you want only the values HVALS is the command.

127.0.0.1:6379> HKEYS pr_hash
1) "name"
2) "last_name"
127.0.0.1:6379> HVALS pr_hash
1) "priheni"
2) "blogs"

HLEN will return the count of fields inside a hash variable.

127.0.0.1:6379> HLEN pr_hash
(integer) 2

HEXISTS is another interesting command. It will check whether a field exists in a hash. It will return 1 if the field exists and 0 if the field does not exist.

127.0.0.1:6379> HEXISTS pr_hash "name"
(integer) 1
127.0.0.1:6379> HEXISTS pr_hash "othername"
(integer) 0

HDEL will remove the field and its value from the hash. It will return 1 if the field is removed and 0 if the field is not found.

127.0.0.1:6379> HDEL pr_hash name
(integer) 1
127.0.0.1:6379> HDEL pr_hash other_name
(integer) 0

Sorted Sets - Arranging Scores Like a Game: Imagine a scoreboard where players get points. In Redis, sorted sets manage scores for players just like that. You can see who's winning and even add or change scores easily.


Bitmaps - Redis Draws with 0s and 1s: Think of a coloring book where you fill spaces with crayons. Redis bitmaps are like coloring books for computers. They use 0s and 1s to create pictures or remember things.

HyperLogLogs - Redis Counts Like Magic: HyperLogLogs are like a special counter. You use them to count a lot of things, but they don't take up much space. Redis uses its magic to give you close-to-perfect counts without using lots of memory.


Conclusion


Redis CLI is an indispensable tool for managing Redis databases efficiently. With a wide range of commands at your disposal, you can perform various operations on your data, whether it's setting values, retrieving information, or performing complex queries. By harnessing the power of Redis CLI, you can take complete control of your Redis server and make the most out of its capabilities.


Remember to explore the Redis documentation for a much more comprehensive list of commands and their usage. Happy Redis-CLI coding!


0 comments

Comments


bottom of page