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:
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:
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.
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:
Or perhaps you’d like to execute the same command n number of times:
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.
This adds a one-second sleep between each PING command. You can also supply subseconds to this option by using an a float:
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.
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:
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.
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.