How to Use the Redis CLI

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

Redis is an in-memory key-value store that can execute millions of requests per second for running real-time applications. While the program can be used as a cache or message broker, Redis is most often used as a database. It supports basic and advanced data structures from strings, list and sets to geospatial indexes. The Redis CLI, an acronym for command line interface, is a straightforward program that allows users to communicate with Redis directly from the terminal. This tutorial will provide an overview and instructions on how to use the Redis CLI.

Prerequisites

  • Redis must be properly installed and configured on the local device. Executing the sudo systemctl status redis command in Linux distros that use systemd will display the current status of the Redis database on the local machine.

Install Redis

Install Redis on machines having a Windows or Mac OS by downloading the file here. Homebrew on Mac OS can also be used to install Redis.

Install Redis on Linux

For Debian-based Linux distros, such as Ubuntu or Linux Mint, execute the following APT-GET Update command on the local machine to get the current version of Redis:

1
sudo apt-get update

Once updated, execute the following command to install Redis:

1
sudo apt-get install redis-server

Now execute the following command to set Redis to run automatically on the local machine:

1
sudo systemctl enable redis-server

Install Redis using Homebrew

To install Redis on a Mac OS with the Homebrew package manager, first update the package to get the latest version of the Redis database. Once the update is completed, then execute the following command to install the updated package:

1
brew update && brew install redis

After installation, execute the following command to start the database:

1
brew services start redis

Now execute the following command to have Redis continually run in the background:

1
redis-server /usr/local/etc/redis.conf

Now use the Redis-command line interface to confirm the program is working properly:

1
2
3
redis-cli
127.0.0.1:6379> ping
PONG

Redis command line usage

Redis use a straightforward command-line interface to provide features that can work around database functionality and other issues.

Begin by executing the following command to create a connection:

1
linux@linux-NECq:~$ redis-cli -h 127.0.0.1 -p 6379 -a mypassword

NOTE: The -a option will perform the authentication of the password.

The next two examples will cover two of the basic commands in the Redis command-line interface.

This first example uses the INCR operator:

1
2
linux@linux-NECq:~$ redis-cli incr count
(integer) 3

Here the function displays an integer value of 3, but it can also be used to list the returned value of a string, arrays, the null or any errors.

The next example will create an output of the Redis CLI in a file that is readable to humans:

1
2
3
linux@linux-NECq:~$ redis-cli incr count > /tmp/output.txt
linux@linux-NECq:~$ cat /tmp/output.txt
4

Here the INCR key operation causes the integer value to increase incrementally. Note the value is shown in the output, and not in the terminal. However, executing the following --raw option will force the output to display on the terminal:

1
2
linux@linux-NECq:~$ redis-cli --raw incr count
5

Redis host and password

The Redis command-line interface can be used to change the default value of Redis to the default host 127.0.0.1 and the default port 6379. This will permit an easier connection to the server to Redis.

The next example shows how to connect to and use the Redis instance in the command line:

1
2
linux@linux-NECq:~$ redis-cli -h 127.0.0.1 -p 6379 -a mypassword ping
PONG

Now execute the INCR key function, covered earlier, to produce the values that will be inserted into five rows of data:

1
2
3
4
5
6
linux@linux-NECq:~$ redis-cli -r 5 incr count
(integer) 6
(integer) 7
(integer) 8
(integer) 9
(integer) 10

Now execute the following operation to confirm the status of the Redis database was properly set:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
linux@linux-NECq:~$ redis-cli -h 127.0.0.1 -p 6379 -a mypassword --stat
------- data ------ --------------------- load -------------------- - child -
keys       mem      clients blocked requests            connections
36         887.05K  4       0       5315 (+0)           6
36         887.02K  4       0       5317 (+2)           6
36         887.02K  4       0       5321 (+4)           6
36         887.02K  4       0       5323 (+2)           6
36         887.05K  4       0       5326 (+3)           6
36         887.02K  4       0       5328 (+2)           6
36         887.05K  4       0       5331 (+3)           6
36         887.02K  4       0       5333 (+2)           6
36         887.05K  4       0       5336 (+3)           6
36         887.02K  4       0       5338 (+2)           6
36         887.05K  4       0       5341 (+3)           6
36         887.02K  4       0       5343 (+2)           6
36         887.05K  4       0       5347 (+4)           6
36         887.02K  4       0       5349 (+2)           6
36         923.04K  4       0       5352 (+3)           6
36         887.02K  4       0       5354 (+2)           6
36         923.02K  4       0       5356 (+2)           6
36         923.02K  4       0       5359 (+3)           6
36         887.02K  4       0       5360 (+1)           6
36         887.05K  4       0       5363 (+3)           6

This gives an overview of how many of the keys are set in the Redis database, the total memory usage and generates an overall view of Redis, as a whole.

Conclusion

This tutorial provided a basic overview of how to use the Redis CLI. The tutorial explained how to install Redis on machines with Windows and Mac OS, Debian-based Linux distros and how to use Homebrew to install Redis on Mac. The article also covered how to start the database and set Redis to continually run in the background. The tutorial then explained how to use the Redis command-line interface to confirm the program is working, how to change the default value of Redis for easier connection and then how to confirm that the status of the Redis database was properly set. Remember that the INCR key operation will causes the integer value to increase incrementally, but the value is not displayed in the terminal unless the --raw option is used.

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.