Appearance
Exploring Redis CLI
We use the built-in command-line tool redis-cli
daily, often thinking of it as a simple interactive Redis data structure manipulation program. However, it has powerful features that many might be unaware of. In this section, we will uncover some lesser-known and interesting uses of redis-cli
.
Executing a Single Command
When accessing the Redis server, we typically use redis-cli
in interactive mode, where we ask questions and receive answers to read and write data. However, there is also a "direct mode" that allows us to execute commands by passing arguments directly to redis-cli
:
bash
$ redis-cli incrby foo 5
(integer) 5
$ redis-cli incrby foo 5
(integer) 10
If the output is large, it can be redirected to an external file:
bash
$ redis-cli info > info.txt
$ wc -l info.txt
120 info.txt
By default, redis-cli
connects to the local server, but you can specify a different server:
bash
$ redis-cli -h localhost -p 6379 -n 2 ping
PONG
Batch Executing Commands
When developing, we often need to create and import data into Redis. Instead of writing scripts, we can use redis-cli
to batch execute a series of commands:
bash
$ cat cmds.txt
set foo1 bar1
set foo2 bar2
set foo3 bar3
Then run:
bash
$ cat cmds.txt | redis-cli
OK
OK
OK
You can also use input redirection to batch execute commands:
bash
$ redis-cli < cmds.txt
OK
OK
OK
Setting Multi-line Strings
If you have a multi-line string to set, you can use the -x
option, which takes the content from standard input as the last argument:
bash
$ cat str.txt
Ernest Hemingway once wrote,
"The world is a fine place and worth fighting for."
I agree with the second part.
$ redis-cli -x set foo < str.txt
OK
$ redis-cli get foo
"Ernest Hemingway once wrote,\n\"The world is a fine place and worth fighting for.\"\nI agree with the second part.\n"
Repeating Commands
redis-cli
also supports repeating commands multiple times with a specified interval between executions, allowing you to observe how the output changes over time:
bash
$ redis-cli -r 5 -i 1 info | grep ops
instantaneous_ops_per_sec:43469
instantaneous_ops_per_sec:47460
instantaneous_ops_per_sec:47699
instantaneous_ops_per_sec:46434
instantaneous_ops_per_sec:47216
If you set the count to -1
, it will repeat indefinitely. In interactive mode, you can repeat a command by prefixing it with a number:
bash
127.0.0.1:6379> 5 ping
PONG
PONG
PONG
PONG
PONG
Exporting to CSV
redis-cli
can export the output of single commands in CSV format:
bash
$ redis-cli rpush lfoo a b c d e f g
(integer) 7
$ redis-cli --csv lrange lfoo 0 -1
"a","b","c","d","e","f","g"
While it can't export an entire database at once, you can combine it with batch execution for better output.
Executing Lua Scripts
Instead of converting script content into a single line for eval
, redis-cli
allows you to execute script files directly:
bash
$ cat mset.txt
return redis.pcall('mset', KEYS[1], ARGV[1], KEYS[2], ARGV[2])
$ redis-cli --eval mset.txt foo1 foo2 , bar1 bar2
OK
Monitoring Server Status
You can use the --stat
option to monitor the server's status in real-time, with an output interval of 1 second:
bash
$ redis-cli --stat
------- data ------ --------------------- load -------------------- - child -
keys mem clients blocked requests connections
2 6.66M 100 0 11591628 (+0) 335
Scanning for Large Keys
To quickly identify large keys that may cause Redis to lag, you can use the --bigkeys
option:
bash
$ ./redis-cli --bigkeys -i 0.01
This scans the keyspace for the largest keys, helping diagnose performance issues.
Sampling Server Commands
If you suspect high OPS is due to specific business modules, you can sample the commands being executed with the monitor
command:
bash
$ redis-cli --host 192.168.x.x --port 6379 monitor
Diagnosing Latency
To diagnose latency between your machine and the Redis server, use the following command, which measures the latency of PING commands:
bash
$ redis-cli --host 192.168.x.x --port 6379 --latency
Remote RDB Backup
You can back up a remote Redis instance to your local machine:
bash
$ ./redis-cli --host 192.168.x.x --port 6379 --rdb ./user.rdb
Simulating a Slave
To observe data synchronization between master and slave servers, you can simulate a slave:
bash
$ ./redis-cli --host 192.168.x.x --port 6379 --slave
This initiates a full synchronization process with the master.
With these tips, you can leverage redis-cli
more effectively, unlocking its full potential for managing and troubleshooting Redis servers.