Table of contents
1.
Introduction
2.
Command-line usage
3.
Host, port, password, and database 
4.
SSL/TLS 
5.
Getting input from other programs
6.
CSV output
7.
Preferences 
8.
Frequently Asked Questions
8.1.
What does the Redis-cli command do?
8.2.
How to use the Redis command line?
9.
Conclusion
Last Updated: Mar 27, 2024

Redis-CLI

Author GAZAL ARORA
0 upvote

Introduction

Redis is an in-memory data structure used as a database, cache, message broker, and streaming engine. It is open-source (BSD license). Redis allows data structures like strings, hashes, lists, sets, sorted sets with range searches, bitmaps, hyperloglogs, geographic indexes, and streams. 

Redis offers built-in replication, Lua scripting, LRU eviction, transactions, several levels of on-disk persistence, Redis Sentinel for high availability, and Redis Cluster for automatic partitioning. You can use Redis from most programming languages.

A TCP socket and a Redis-specific protocol are used by external programs to communicate with Redis. The Redis client libraries for many programming languages implement this protocol. On the other hand, Redis provides a command-line utility that can be used to send commands to the database. This program is known as Redis-CLI.

The Redis command-line interface (Redis-CLI) is a terminal tool that allows you to send instructions to the Redis server and get responses from it.

It has two modes: an interactive REPL (Read Eval Print Loop) mode in which the user enters Redis commands and receives responses. 

The other is a command mode in which Redis-CLI is run with additional arguments the response is sent to standard output.

Redis uses an in-memory dataset to attain high performance. Redis may save your data in one of two ways, depending on your needs: periodically dumping the dataset to disk or adding the commands to a disk-based log. But if you only need a feature-rich, networked, in-memory cache, you can turn off persistence.

Command-line usage

Redis-CLI includes basic line editing capabilities in interactive mode, giving you a typing-like experience.

You have several options for starting the software in unique modes. You can, among other things, simulate a replica and print the replication stream it receives from the main, examine a Redis server's latency and show statistics, or request an ASCII-art spectrogram of latency samples and frequencies.

Include this command to execute as separate arguments of redis-cli to run a Redis command and receive its response as standard output to the terminal:

$ redis-cli INCR mycounter
(integer) 7

The command's response is "7." Because Redis responses are typed (strings, arrays, integers, nil, errors, and so on), the type of the response is displayed within parenthesis. 

When redis-cli detects that the standard output is a tty or terminal, it only displays additional information for human readability. It will automatically enable the raw output mode for all other outputs, as shown:

$ redis-cli INCR mycounter > /tmp/output.txt
$ cat /tmp/output.txt
8

Because redis-cli identified that the output was no longer written to the terminal, (integer) was excluded from the output. With the —raw option, you can force raw output even on the terminal:

$ redis-cli --raw INCR mycounter
9

When writing to a file or via a pipe to other commands, use —no-raw to force human-readable output.

Host, port, password, and database 

Redis-cli, by default, connects to the server at 127.0.0.1 on port 6379. Several command line parameters can be used to adjust this. Use the -h option to specify a different hostname or IP address. Use -p to specify a different port.

If your instance is password-protected, the -a <password> option will perform authentication, avoiding the need to use the AUTH command explicitly:

$ redis-cli -a myUnguessedPassword PING
PONG

It is recommended that the password be sent to redis-cli automatically via the REDISCLI AUTH environment variable for security reasons.

SSL/TLS 

By default, redis-cli connects to Redis via a standard TCP connection. The —tls option, coupled with —cacert or —cacertdir to configure a trusted root certificate bundle or directory, can be used to enable SSL/TLS.

If the target server requires client-side certificate authentication, use —cert and —key to specify a certificate and accompanying private key.

Getting input from other programs

You can use redis-cli in two ways to receive input from other commands through the standard input. The target payload can be used as the last parameter from stdin. Use the -x option, for example, to set the Redis key net services to the contents of the file /etc/services from a local file system:

$ redis-cli -x SET net_services < /etc/services
OK
$ redis-cli GETRANGE net_services 0 50
"#\n# Network services, Internet style\n#\n# Note that"

Redis-cli was run with the -x option in the first line of the above session, and a file was routed to the CLI's standard input as the value to meet the SET net services command phrase. This comes in handy when scripting.

CSV output

To send data from Redis to an external program, redis-cli has a CSV (Comma Separated Values) exporting function.

$ redis-cli LPUSH mylist a b c d e
(integer) 5
$ redis-cli --csv LRANGE mylist 0 -1
"e", "d","c","b","a"

Preferences 

Redis-cli behaviour can be customized in two ways. The CLI loads the file.redisclirc in the home directory when it starts up. You can modify the file's default location by setting the REDISCLI RCFILE environment variable to a different path. Preferences can also be set during a CLI session, but they will only be active for the duration of that session.

Use the special: set command to define preferences. The following preferences can be set either using the CLI or the .redisclirc file:

set nohints - disables syntax hints:

set hints - allows syntax hints

Frequently Asked Questions

What does the Redis-cli command do?

Redis-cli is a terminal tool that allows you to send instructions to the Redis server and get responses.

How to use the Redis command line?

To launch the Redis client, open a terminal and type redis-cli. This will establish a connection to your local server, from which you can issue any command. We connect to a Redis server running on the local machine and run the command PING to see if the server is up and running.

Conclusion

In this article, we learned about Redis-cli. We learned that Redis-cli (Redis command line interface) is a terminal tool that allows you to send instructions to the Redis server and get responses.

Click here to learn about RedisGraph.

You can use Coding Ninjas Studio to practice various DSA questions asked in different interviews. It will assist you in mastering effective coding techniques, and you will also get interview experiences with people working in big companies.

Live masterclass