当前位置:网站首页>A complete tutorial for getting started with redis: redis shell

A complete tutorial for getting started with redis: redis shell

2022-07-04 22:53:00 Gu Ge academic

Redis Provides redis-cli、redis-server、redis-benchmark etc. Shell Tools . they
Although it's relatively simple , But although the sparrow is small, it has all five internal organs , Sometimes you can solve some problems skillfully .

3.2.1 redis-cli Detailed explanation
The first 1 Zhang Zeng introduced redis-cli, Include -h、-p Parameters , But in addition to these parameters , And there's a lot more
What useful parameters , To understand redis-cli All the parameters of , It can be executed redis-cli-help Orders come in
Row view , The following will explain the meaning of some important parameters and the usage scenarios .
1.-r
-r(repeat) Option represents executing the command multiple times , For example, the following operations will be performed three times ping
command :
redis-cli -r 3 ping
PONG
PONG
PONG
2.-i
-i(interval) Option represents executing commands every few seconds , however -i The options must be with -r choose
Use with , The following operations will be performed every 1 Once per second ping command , A total of execution 5 Time :
$ redis-cli -r 5 -i 1 ping
PONG
PONG
PONG
PONG
PONG
Be careful -i In seconds , Milliseconds are not supported , But if you want to do it every 10 Execution in milliseconds
once , It can be used -i0.01, for example :
$ redis-cli -r 5 -i 0.01 ping
PONG
PONG
PONG
PONG
PONG

For example, the following operations utilize -r and -i Options , every other 1 Second output memory usage , Total output
100 Time :
redis-cli -r 100 -i 1 info | grep used_memory_human
used_memory_human:2.95G
used_memory_human:2.95G
......................
used_memory_human:2.94G
3.-x
-x The options represent input from the standard (stdin) Read data as redis-cli The last reference of
Count , For example, the following operation will string world As set hello Value :
$ echo "world" | redis-cli -x set hello
OK
4.-c
-c(cluster) The option is to connect Redis Cluster Nodes are used for ,-c Option can prevent
stop moved and ask abnormal , of Redis Cluster In the fourth 10 Chapter introduction .
5.-a
If Redis Configured password , It can be used -a(auth) Options , With this option, you don't need
Manual input auth command .
6.--scan and --pattern
--scan Options and --pattern Option is used to scan keys for the specified mode , It is equivalent to using scan life
Make .

7.--slave
--slave The option is to simulate the current client as the current Redis The slave node of the node , Can be used to
Get current Redis Node update operation , About Redis The copy will be made on 6 Chapter gives a detailed introduction
Shao . Reasonable use of this option can record the current connection Redis Some update operations of nodes , this
Some update operations are likely to be the data needed for the actual development of the business .
Now open the first client , Use --slave Options , See that synchronization is complete :
$ redis-cli --slave
SYNC with master, discarding 72 bytes of bulk transfer...
SYNC done. Logging commands from master.
Then open another client to do some update operations :
redis-cli
127.0.0.1:6379> set hello world
OK
127.0.0.1:6379> set a b
OK
127.0.0.1:6379> incr count
1
127.0.0.1:6379> get hello
"world"
The first client will receive Redis Node update operation :
redis-cli --slave
SYNC with master, discarding 72 bytes of bulk transfer...
SYNC done. Logging commands from master.
"PING"
"PING"
"PING"
"PING"
"PING"
"SELECT","0"
"set","hello","world"
"set","a","b"
"PING"
"incr","count"

Be careful
PING The command is generated by master-slave replication , The first 6 This chapter will introduce master-slave replication .
8.--rdb
--rdb The option requests Redis Instance generation and sending RDB Persistent files , Keep it locally .
It can be used for regular backup of persistent files . of Redis Persistence will occur in the 5 Chapter gives a detailed introduction
Shao .
9.--pipe
--pipe Option to encapsulate the command as Redis Data format defined by communication protocol , Bulk delivery
to Redis perform , of Redis The communication protocol will be in 4 Chapter gives a detailed introduction , For example, the following operation
At the same time set hello world and incr counter Two orders :
echo -en '*3\r\n$3\r\nSET\r\n$5\r\nhello\r\n$5\r\nworld\r\n*2\r\n$4\r\nincr\r\
n$7\r\ncounter\r\n' | redis-cli --pipe
10.--bigkeys
--bigkeys Option to use scan Command to Redis The key to sample , Find the memory usage ratio
Larger key values , These keys can be the bottleneck of the system .
11.--eval
--eval Option is used to perform the specified Lua Script , of Lua The use of scripts will be in 3.4 Section introduction .
12.--latency

latency There are three options , Namely --latency、--latency-history、--latency-dist.
They all detect network latency , about Redis It's very helpful to develop and maintain .
(1)--latency
This option can test client to target Redis Network delay for , For example, the current topology, such as
chart 3-4 Shown . client B and Redis In the computer room B, client A In the computer room A, Computer room A And the computer room B yes
Trans regional .

  client B:
redis-cli -h {machineB} --latency
min: 0, max: 1, avg: 0.07 (4211 samples)
client A:
redis-cli -h {machineB} --latency
min: 0, max: 2, avg: 1.04 (2096 samples)

You can see the client A Because of the distance Redis Far away , The average network latency will be slightly higher .
(2)--latency-history
--latency There is only one result , If you want to know the delay information in the form of time division ,
have access to --latency-history Options :
redis-cli -h 10.10.xx.xx --latency-history
min: 0, max: 1, avg: 0.28 (1330 samples) -- 15.01 seconds range …
min: 0, max: 1, avg: 0.05 (1364 samples) -- 15.01 seconds range
You can see the delay information every 15 Output once per second , Can pass -i Parameter control interval time .
(3)--latency-dist
This option outputs delay statistics from the console in the form of a statistical chart .
13.--stat
--stat Options are available in real time Redis Important statistics , although info The statistical letter in the command
The rest is more complete , But you can see some incremental data in real time ( for example requests) about Redis Operation and maintenance of
There is still some help , As shown below :
redis-cli --stat
------- data ------ --------------------- load -------------------- - child -
keys mem clients blocked requests connections
2451959 3.43G 1162 0 7426132839 (+0) 1337356
2451958 3.42G 1162 0 7426133645 (+806) 1337356 …
2452182 3.43G 1161 0 7426150275 (+1303) 1337356
14.--raw and --no-raw
--no-raw The option requires that the return result of the command must be in the original format ,--raw Just like
back , Returns the formatted result .
stay Redis Set a Chinese value:

$redis-cli set hello " Hello "
OK
If it works get Or use --no-raw Options , Then the returned result is binary lattice
type :
$redis-cli get hello
"\xe4\xbd\xa0\xe5\xa5\xbd"
$redis-cli --no-raw get hello
"\xe4\xbd\xa0\xe5\xa5\xbd"
If used --raw Options , Will return to Chinese :
$redis-cli --raw get hello Hello


3.2.2 redis-server Detailed explanation
redis-server In addition to starting Redis Outside , One more --test-memory Options .redis-server-
-test-memory Can be used to detect whether the current operating system can stably allocate the specified amount of memory to
Redis, Through this kind of detection can effectively avoid the memory problems caused by Redis collapse , For example, the following
Operation check whether the current operating system can provide 1G The memory for Redis:
redis-server --test-memory 1024
The whole memory detection takes a long time . When the output passed this test It means that the memory detection is completed
BI , At the end of the day --test-memory It's just a simple test , If there is doubt, you can use more professional
Memory detection tool based on :
Please keep the test running several minutes per GB of memory.
Also check http:// www.memtest86.com/ and http:// pyropus.ca/software/memtester/
................ Ignore detection details ................
Your memory passed this test.
Please if you are still in doubt use the following two tools:
1) memtest86: http:// www.memtest86.com/
2) memtester: http:// pyropus.ca/software/memtester/
Usually it doesn't have to be turned on every time Redis Instance --test-memory Options , This function is more inclined to
For debugging and testing , for example , Want to quickly fill up the memory of the machine to do some extreme condition tests , This achievement
Can be a good choice .

3.2.3 redis-benchmark Detailed explanation
redis-benchmark It can be for Redis Do benchmark performance testing , It provides many options to help open
Send and O & M personnel to test Redis Related performance of , Here are the options .
1.-c
-c(clients) Option represents the number of concurrent clients ( The default is 50).
2.-n<requests>
-n(num) Option represents the total number of client requests ( The default is 100000).
for example redis-benchmark-c100-n20000 representative 100 Each client requests Redis, One
In total 20000 Time .redis-benchmark Will test all kinds of data structure commands , And give
Performance index :
====== GET ======
20000 requests completed in 0.27 seconds
100 parallel clients
3 bytes payload
keep alive: 1
99.11% <= 1 milliseconds
100.00% <= 1 milliseconds
73529.41 requests per second
For example, all of the above are implemented 20000 Time get operation , stay 0.27 Seconds to complete , Amount of data per request
yes 3 Bytes ,99.11% The command execution time of is less than 1 millisecond ,Redis It can be processed per second
73529.41 Time get request .


3.-q

-q The options just show redis-benchmark Of requests per second Information , for example :
$redis-benchmark -c 100 -n 20000 -q
PING_INLINE: 74349.45 requests per second
PING_BULK: 68728.52 requests per second
SET: 71174.38 requests per second …
LRANGE_500 (first 450 elements): 11299.44 requests per second
LRANGE_600 (first 600 elements): 9319.67 requests per second
MSET (10 keys): 70671.38 requests per second
4.-r
In an empty Redis On the implementation of redis-benchmark Will find only 3 Key :
127.0.0.1:6379> dbsize
(integer) 3
127.0.0.1:6379> keys *
1) "counter:__rand_int__"
2) "mylist"
3) "key:__rand_int__"
If you want to Redis Insert more keys , Can be executed using -r(random) Options , Can be directed to
Redis Insert more random keys .
$redis-benchmark -c 100 -n 20000 -r 10000
-r The options will be in key、counter Add one to the key 12 Suffix of position ,-r10000 Represents only the last four
Bits are processed randomly (-r It's not a random number ). For example, after the above operation ,key Quantity and settlement of
The fruit structure is as follows :
127.0.0.1:6379> dbsize
(integer) 18641
127.0.0.1:6379> scan 0
1) "14336"
2) 1) "key:000000004580"
2) "key:000000004519"

10) "key:000000002113"

5.-P
-P Options represent each request pipeline The amount of data ( The default is 1).
6.-k<boolean>
-k Option represents whether the client uses keepalive,1 For the use of ,0 For not using , The default value is
1.
7.-t
-t Option to benchmark the specified command .
redis-benchmark -t get,set -q
SET: 98619.32 requests per second
GET: 97560.98 requests per second
8.--csv
--csv Option will follow the result as csv Format output , It is convenient for subsequent processing , Export to Excel
etc. .
redis-benchmark -t get,set --csv
"SET","81300.81"
"GET","79051.38"

原网站

版权声明
本文为[Gu Ge academic]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207042229263661.html