how toredis

Top 10 Redis CLI commands

By April 14, 2015 August 18th, 2022 No Comments

Redis uses a very straightforward command line interface. Though it’s relatively simple, it does provide some interesting features that one might not expect. Let’s go over some of the basics and work our way around most of the client’s functionality and features.

To start, we have a simple connection:

cweid@strange:~$ redis-cli -h 127.0.0.1 -p 6379 -a mysupersecretpassword
127.0.0.1:6379> PING
PONG

Alright! We’ve connected to our very own Redis server and authenticated using our super secret password.

Alternatively, you can omit the -a option and authenticate after you connect:

cweid@strange:~$ redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> AUTH mysupersecretpassword
OK
127.0.0.1:6379> PING
PONG

If you have your Redis server and client running on the same machine, you might choose to connect via a Unix socket.

Note: If you still provide a hostname and port as well as a socket, redis-cli will still connect via the Unix socket.

cweid@strange:~$ redis-cli -s /tmp/redis.sock
127.0.0.1:6379> AUTH mysupersecretpassword
OK
127.0.0.1:6379> PING
PONG

Okay, now that we understand how to connect and authenticate to our Redis instance via the command line, let’s see some examples of useful things we can do with it.

Let’s say you want to execute a command via the command line and have only its output be returned to standard out:

cweid@strange:~$ redis-cli -h 127.0.0.1 -p 6379 -a mysupersecretpassword PING
PONG

Or perhaps you’d like to execute the same command n number of times:

cweid@strange:~$ redis-cli -h 127.0.0.1 -p 6379 -a mysupersecretpassword -r 4 PING
PONG
PONG
PONG
PONG

Notice that we added a -r to our command to supply the “repeat” option. Alternatively, we can add a delay using -i in conjunction with -r.

cweid@strange:~$ redis-cli -h 127.0.0.1 -p 6379 -a mysupersecretpassword -i 1 -r 4 PING
PONG
PONG
PONG
PONG

This adds a one-second sleep between each PING command. You can also supply subseconds to this option by using an a float:

cweid@strange:~$ redis-cli -h 127.0.0.1 -p 6379 -a mysupersecretpassword -i 0.1 -r 4 PING
PONG
PONG
PONG
PONG

This would run the PING command every 10th of a second.

To generate some simple diagnostic information about the Redis instance you are connected to, simply run redis-cli with the –stat option.

cweid@strange:~$ redis-cli -h 127.0.0.1 -p 6379 -a mysupersecretpassword --stat
------- data ------ --------------------- load -------------------- - child -
keys       mem      clients blocked requests            connections          
0          790.80K  1       0       122 (+0)            16          
0          790.80K  1       0       123 (+1)            16          
0          790.80K  1       0       124 (+1)            16          
0          790.80K  1       0       125 (+1)            16          
0          790.80K  1       0       126 (+1)            16          

Here we can see:

Here we can see:

  • How many keys are set on the server.
  • The server’s total memory usage.
  • The total number of clients connected or blocked.
  • The total number of requests the server has served.
  • The total current number of connections.

This command is useful to get an overview of the Redis server as a whole. Think of it like stating a file.

Now that you know how to generate some simple stats about a Redis server, let’s check the latency of Redis commands coming in. This is super simple and can be done via the command line:

cweid@strange:~$ redis-cli -h 127.0.0.1 -p 6379 -a mysupersecretpassword --latency
min: 0, max: 1, avg: 0.13 (763 samples)

Here we see the minimum, maximum, and average request time, as well as the number of samples taken.

Note: These are recorded in microseconds. For more info about Redis latency, take a look at documentation for latency monitoring.

To analyze your keyspace in search of large strings or other data structures, run the –bigkeys option. This is good to use to find large keys in our keyspace, as well as to get a count of the overall distribution of key types.

cweid@strange:~$ redis-cli -h 127.0.0.1 -p 6379 -a mysupersecretpassword --bigkeys
# Scanning the entire keyspace to find biggest keys as well as
# average sizes per key type.  You can use -i 0.1 to sleep 0.1 sec
# per 100 SCAN commands (not usually needed).

[00.00%] Biggest string found so far 'user:paul' with 4 bytes
[00.00%] Biggest string found so far 'barrrr' with 19612 bytes

-------- summary -------

Sampled 4 keys in the keyspace!
Total key length in bytes is 29 (avg len 7.25)

Biggest string found 'barrrr' has 19612 bytes

4 strings with 19624 bytes (100.00% of keys, avg size 4906.00)
0 lists with 0 items (00.00% of keys, avg size 0.00)
0 sets with 0 members (00.00% of keys, avg size 0.00)
0 hashs with 0 fields (00.00% of keys, avg size 0.00)
0 zsets with 0 members (00.00% of keys, avg size 0.00)

This gives us a lot of useful information back about different keys, including their types and sizes.

Overall, the Redis CLI is a powerful tool to help you manage your Redis instance. The ability to use its built in options can really help in analzying a problematic Redis server.