Redis Shell

12/18/2021 Redis

# 一:前言

Redis 提供了 redis-cliredis-serverredis-benchmark 等 Shell 工具。它们虽然比较简单,但是麻雀虽小五脏俱全,有时可以很巧妙地解决一些问题。

# 二:redis-cli












 

## 例如:redis-cli -h {host} -p {port}
redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> set hello world
OK
127.0.0.1:6379> get hello
"world"

## 例如:redis-cli -h {host} -p {port} {command}
redis-cli -h 127.0.0.1 -p 6379 get hello
"world"

## 可以执行 redis-cli -help 命令来进行查看全部参数
1
2
3
4
5
6
7
8
9
10
11
12
  • -h(host):地址,默认连接 127.0.0.1
  • -p(port):端口,默认 6379
  • -r(repeat):将命令执行多次;
  • -i(interval):代表每隔几秒执行一次命令 ;必须和 -r 选项一起使用
  • -x:从标准输入(stdin)读取数据作为 redis-cli 的最后一个参数;
  • -c(cluster):连接 Redis Cluster 节点时需要使用的,-c 选项可以防止 moved 和 ask 异常
  • -a(auth):密码选项;
  • --scan和--pattern:用于扫描指定模式的键,相当于使用 scan 命令;
  • --slave:把当前客户端模拟成当前 Redis 节点的从节点,可以用来获取当前 Redis 节点的更新操作。合理的利用这个选项可以记录当前连接 Redis 节点的一些更新操作,这些更新操作很可能是实际开发业务时需要的数据;
  • --rdb:请求 Redis 实例生成并发送 RDB 持久化文件,保存在本地。可使用它做持久化文件的定期备份;
  • --pipe:用于将命令封装成 Redis 通信协议定义的数据格式,批量发送给 Redis 执行;
  • --bigkeys:使用 scan 命令对 Redis 的键进行采样,从中找到内存占用比较大的键值,这些键可能是系统的瓶颈;
  • --eval:用于执行指定 Lua 脚本;
  • --latency:有三个选项,分别是 --latency--latency-history--latency-dist。它们都可以检测网络延迟;
  • --stat:可以实时获取 Redis 的重要统计信息,虽然 info 命令中的统计信息更全,但是能实时看到一些增量的数据(例如request);
  • --raw和--no-raw:要求命令的返回结果必须是原始的格式,--raw 恰恰相反,返回格式化后的结果。
## -r
### 执行三次ping命令
> redis-cli -r 3 ping
PONG
PONG
PONG

## -i -r
### 每隔1秒执行一次ping命令,共执行5次
### 其中 -i 单位是秒,不支持毫秒为单位,如果想间隔10毫秒,可以 -i 0.01
> redis-cli -r 5 -i 1 ping
PONG
PONG
PONG
PONG
PONG
### 每隔1秒输出内存的使用量,一共输出100次
> 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.95G

## -x
### 将字符串world作为set hello的值
> echo "world" | redis-cli -x set hello
OK

## --slave参数使用
### 第一个客户端
> redis-cli --slave
SYNC with master, discarding 72 bytes of bulk transfer...
SYNC done. Logging commands from master.
### 第二个客户端做一些更新操作
> redis-cli
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"
### 第一个客户端,PING命令是由于主从复制产生的
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"

## --pipe
> 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

## --latency
### 执行结果只有一条
### 客户端B和Redis在机房B,客户端A在机房A,机房A和机房B是跨地区的:
### 客户端B
> redis-cli -h {machineB} --latency
min: 0, max: 1, avg: 0.07 (4211 samples)
### 客户端A
> redis-cli -h {machineB} --latency 
min: 0, max: 2, avg: 1.04 (2096 samples)
### 可以看出客户端A由于距离Redis比较远,平均网络延迟会稍微高一些

## --latency-history
### 以分时段的形式了解延迟信息
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

## --latency-dist
### 使用统计图表的形式从控制台输出延迟统计信息

## --state
> 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)  13373562452182 3.43G 1161    0       7426150275 (+1303) 1337356

## --raw
> redis-cli set hello "你好" 
OK
### 如果正常执行get或者使用--no-raw选项,那么返回的结果是二进制格式:
> redis-cli get hello 
"\xe4\xbd\xa0\xe5\xa5\xbd" 
> redis-cli --no-raw get hello 
"\xe4\xbd\xa0\xe5\xa5\xbd"
### 如果使用--raw将返回中文
> redis-cli --raw get hello
你好
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102

# 三:redis-server

redis-server 除了启动 Redis 外,还有一个 --test-memory 选项用来检测当前操作系统能否稳定地分配指定容量的内存给 Redis,通过这种检测可以有效避免因为内存问题造成 Redis 崩溃,例如下面操作检测当前操作系统能否提供 1G 的内存给 Redis:

redis-server --test-memory 1024

整个内存检测的时间比较长。当输出 passed this test 时说明内存检测完毕,最后会提示 --test-memory 只是简单检测,如果有质疑可以使用更加专业的内存检测工具

Please keep the test running several minutes per GB of memory.
Also check http:// www.memtest86.com/ and http:// pyropus.ca/software/memtester/
................忽略检测细节................
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/

通常无需每次开启 Redis 实例时都执行 --test-memory 选项,该功能更偏向于调试和测试,例如,想快速占满机器内存做一些极端条件的测试,这个功能是一个不错的选择。

# 四:redis-benchmark

redis-benchmark 可以为 Redis 做基准性能测试,提供了很多选项帮助开发和运维人员测试 Redis 的相关性能

  • -c(clients):客户端的并发数量(默认是50);
  • -n <requests>(num):客户端请求总数(默认是100_000);
  • -q:仅仅显示 redis-benchmark 的 requests per second 信息;
  • -r(random):插入随机键,控制命令,非个数;
  • -P:每个请求 pipeline 的数据量(默认为1);
  • -k <boolean>:客户端是否使用 keepalive,1 为使用,0 为不使用,默认值为 1
  • -t:可以对指定命令进行基准测试;
  • --csv:会将结果按照 csv 格式输出。
## -c -n
### 100个客户端同时请求Redis,一共执行20000次。
> redis-benchmark -c 100 -n 20000
====== 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
### 上面一共执行了20000次get操作,在0.27秒完成,每个请求数据量是3个字节,99.11%的命令执行时间小于1毫秒,Redis每秒可以处理73529.41次get请求。

## -q
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

## -r
### 执行redis-benchmark,默认只新增三个随机键
> dbsize (integer) 
3 
> keys * 
1) "counter:__rand_int__" 
2) "mylist" 
3) "key:__rand_int__"
### 使用-r选项,会在key、counter键上加一个12位的后缀
### -r 10000代表只对后四位做随机处理(-r不是随机数的个数)
> redis-benchmark -c 100 -n 20000 -r 10000
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"

## -t
> redis-benchmark -t get,set -q 
SET: 98619.32 requests per second 
GET: 97560.98 requests per second

## --csv
redis-benchmark -t get,set --csv 
"SET","81300.81" 
"GET","79051.38"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

# 五:参考文献

最后更新: 12/26/2023, 4:16:14 PM