We use Redis built-in command line tool Redis – CLI every day, for a long time that it is a simple interactive Redis data structure manual operation procedures, but the vast majority of students behind the powerful functions may have never heard of. In this section, we take a look at some of these lesser-known and interesting uses.
Run a single command
When accessing the Redis server, we usually use Redis – CLI to enter the interactive mode, and then ask and answer to read and write the server. In this case, we use its “interactive mode”. There is another “direct mode” where commands are executed and output is obtained by passing command parameters directly to the Redis-CLI.
$ redis-cli incrby foo 5
(integer) 5
$ redis-cli incrby foo 5
(integer10)Copy the code
If the output is large, you can also redirect it to an external file
$ redis-cli info > info.txt
$ wc -l info.txt
120 info.txt
Copy the code
The above command points to the default server address, if you want to point to a specific server
Select 1 $redis-cli -h localhost -p 6379 -n 2 ping PONGCopy the code
Batch Command Execution
In normal online development, sometimes we have to manually create data and import it into Redis. Usually we write a script to do this. However, there is another convenient way to batch execute a series of instructions directly using redis-CLI.
$ cat cmds.txt
set foo1 bar1
set foo2 bar2
set foo3 bar3
......
$ cat cmds.txt | redis-cli
OK
OK
OK
...
Copy the code
The above instruction uses Unix pipes to connect the standard output of the CAT instruction to the standard input of the Redis-CLI. You can also batch execute instructions directly using input redirection.
$ redis-cli < cmds.txt
OK
OK
OK
...
Copy the code
Set a multi-line string
If a string has more than one line and you want to pass it into the set directive, what does redis-cli do? You can use the -x option, which takes the standard input as the last argument.
$ 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"
Copy the code
Repeat instruction
Redis-cli also supports repeated execution of the command multiple times, and an interval is set between the execution of each command, so that the output content of a command can be observed over time.
// The interval is 1s, execute 5 times, Observe the change of QPS $redis - cli - r 5-1 the info I | 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:47216Copy the code
If you set the number to -1 you’re going to repeat it an infinite number of times forever. If the -i parameter is not provided, the execution is repeated without interval. It is also possible to repeat instructions in interactive mode, in a strange form, increasing the number of times before the instructions
127.0.0.1:6379> 5 Ping PONG PONG PONG PONG PONG PONG# The following command is scary, your screen is going to be angry127.0.0.1:6379 > 10000 info...Copy the code
Export CSV
Redis-cli cannot export the contents of the entire library to CSV at once, but can export the output of a single instruction to CSV format.
$ 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"
$ redis-cli hmset hfoo a 1 b 2 c 3 d 4
OK
$ redis-cli --csv hgetall hfoo
"a"."1"."b"."2"."c"."3"."d"."4"
Copy the code
Of course, this export function is weak, just a bunch of strings separated by commas. However, you can see the effect of exporting multiple commands in conjunction with batch execution of commands.
$ redis-cli --csv -r 5 hgetall hfoo
"a"."1"."b"."2"."c"."3"."d"."4"
"a"."1"."b"."2"."c"."3"."d"."4"
"a"."1"."b"."2"."c"."3"."d"."4"
"a"."1"."b"."2"."c"."3"."d"."4"
"a"."1"."b"."2"."c"."3"."d"."4"
Copy the code
The reader should understand that the CSV argument does nothing more than convert the output, separated by commas.
Execute the Lua script
In the Lua scripting section, we used the Eval directive to execute script strings. Each time we had to compress the script into a single-line string, the eval directive was called, which was tedious and unreadable. Redis -cli takes this into account and can execute script files directly.
127.0.0.1:6379 >eval "return redis.pcall('mset', KEYS[1], ARGV[1], KEYS[2], ARGV[2])" 2 foo1 foo2 bar1 bar2
OK
127.0.0.1:6379> eval "return redis.pcall('mget', KEYS[1], KEYS[2])" 2 foo1 foo2
1) "bar1"
2) "bar2"
Copy the code
We execute the above instructions as a script with different arguments, separated by commas between KEY and ARGV, and without providing the number of arguments for KEY
$ cat mset.txt
return redis.pcall('mset', KEYS[1], ARGV[1], KEYS[2], ARGV[2])
$ cat mget.txt
return redis.pcall('mget', KEYS[1], KEYS[2])
$ redis-cli --eval mset.txt foo1 foo2 , bar1 bar2
OK
$ redis-cli --eval mget.txt foo1 foo2
1) "bar1"
2) "bar2"
Copy the code
If your Lua script is too long, –eval will be useful.
Monitoring server Status
We can use the –stat parameter to monitor the status of the server in real time, every 1s in real time.
$ redis-cli --stat
------- data ------ --------------------- load -------------------- - child -
keys mem clients blocked requests connections
2 6.66M 100 0 11591628 (+0) 335
2 6.66M 100 0 11653169 (+61541) 335
2 6.66M 100 0 11706550 (+53381) 335
2 6.54M 100 0 11758831 (+52281) 335
2 6.66M 100 0 11803132 (+44301) 335
2 6.66M 100 0 11854183 (+51051) 335
Copy the code
If you think the interval is too long or too short, you can adjust the output interval using the -i parameter.
Scan the big KEY
This feature is so useful that I’ve tried it countless times online. Every time we encounter the problem of Redis occasionally stuck, the first thought is whether there is a large KEY in the instance. The memory expansion and release of large keys will lead to the main thread stuck. If you know if there’s a big KEY in there, you can write your own program to scan it, but it’s too tedious. Redis – CLI provides –bigkeys parameter can quickly scan out large keys in memory, use -i parameter control scan interval, avoid scan instructions caused by the server OPS spike alarm.
$./redis-cli --bigkeys -i 0.01# 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 zset found so far'hist:aht:main:async_finish:20180425:17'With 1440 members Biggest zset found so far'hist:qps:async:authorize:20170311:27'With 2465 members [00.00%] Biggesthash found so far 'job:counters:6ya9ypu6ckcl'With 3 fields [00.01%] Biggest string found so far'rt:aht:main:device_online:68:{-4}'With 4 bytes [00.01%] Biggest zset found so far'machine:load:20180709'With 2879 members [00.02%] Biggest string found so far'6y6fze8kj7cy:{-7}' with 90 bytes
Copy the code
Redis – CLI records the KEY with the maximum length for each object type. For each object type, a new record is immediately output. It can ensure the output of keys with Top1 length, but Top2 and Top3 keys cannot be guaranteed to be scanned. The general processing method is to scan several times, or wipe out the Top1 KEY and then scan to check whether there are secondary keys.
Sample server instruction
At present, there is a Redis server whose OPS is too high, and many business modules are using this Redis. How can we determine which business causes the abnormally high OPS? At this time, the instructions of the online server can be sampled, and the business points with high OPS proportion can be analyzed by observing the sampled instructions. Use the Monitor directive, which displays all of the commands executed by the server in an instant. Be careful to use it even if you interrupt with CTRL + C, or your monitor will snap too many instructions at once.
$ redis-cli --host 192.168.x.x --port 6379 monitor
1539853410.458483 [0 10.100.90.62:34365] "GET" "6yax3eb6etq8:{-7}"
1539853410.459212 [0 10.100.90.61:56659] "PFADD" "growth:dau:20181018" "2klxkimass8w"
1539853410.462938 [0 10.100.90.62:20681] "GET" "6yax3eb6etq8:{-7}"
1539853410.467231 [0 10.100.90.61:40277] "PFADD" "growth:dau:20181018" "2kei0to86ps1"
1539853410.470319 [0 10.100.90.62:34365] "GET" "6yax3eb6etq8:{-7}"
1539853410.473927 [0 10.100.90.61:58128] "GET" "6yax3eb6etq8:{-7}"
1539853410.475712 [0 10.100.90.61:40277] "PFADD" "growth:dau:20181018" "2km8sqhlefpc"
1539853410.477053 [0 10.100.90.62:61292] "GET" "6yax3eb6etq8:{-7}"
Copy the code
Diagnostic server latency
We usually use the Unix ping command to diagnose the delay between the two machines. Redis also provides delay diagnosis instructions, but its principle is not quite the same, it is to diagnose the current machine and Redis server instruction (PING instruction) delay, it is not only the physical network delay, but also the Redis main thread is busy. If you find that Unix ping commands have very low latency and Redis has very high latency, then the Redis server is slow to execute commands.
$ redis-cli --host 192.168.x.x --port 6379 --latency
min: 0, max: 5, avg: 0.08 (305 samples)
Copy the code
The delay unit is ms. Redis – CLI can also display the distribution of delay, and is a graphical output.
$ redis-cli --latency-dist
Copy the code
The meaning of this figure is not described by the author, so readers can try to decipher it.
Remote RDB backup
To back up a remote Redis instance to the local machine, the remote server performs a BGSave operation and then transfers the RDB file to the client. Remote RDB backup gives us the feeling that “the scholar stays in the house and knows everything in the world”.
$./redis-cli --host 192.168.x --port 6379 -- RDB./user. RDB SYNC sent to master, writing 2501265095 bytes to'./user.rdb'
Transfer finished with success.
Copy the code
Simulation from the library
If you want to see what data is synchronized between the master and slave servers, you can emulate the slave library using Redis-CLI.
$./redis-cli --host 192.168.x.x --port 6379 --slave SYNC with master, Discarding 51778306 bytes of bulk transfer... SYNC done. Logging commands from master. ...Copy the code
The first thing you do when you connect from the main library to the main library is full synchronization, so it’s not unusual to see the above instructions stall. After the first full synchronization is complete, an incremental AOF log is printed.
To read more in-depth technical articles, scan the QR code above to follow the wechat public account “Code Hole”.