阻塞

1.内在原因 慢查询/CPU饱和/持久化阻塞 2.外部原因 CPU竞争/内存交换/网络问题

1.慢查询

1.发现耗时的命令, 修改为低算法度的命令,如hgetall改为hmget等,禁用keys、sort等命令 2.调整大对象:缩减大对象数据或把大对象拆分为多个小对象,防止一次命令操作过多的数据。 大对象拆分过程需要视具体的业务决定

1.1查询日志
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
slowlog-log-slower-than 慢查询阈值(微秒) 默认是10毫秒
slowlog-max-len         慢查询队列长度 默认是128

slowlog get{n} 获取最新的n条慢查询命令
slowlog len    获取慢查询队列的长度
slowlog rest   清空慢查询队列


修改配置文件
动态修改

config set slowlog-log-slower-than 20000 
config set slowlog-max-len 1000
config rewrite


127.0.0.1:6379> slowlog get
1) 1) (integer) 457838     # 标识id
   2) (integer) 1548923538 # 发生时间戳
   3) (integer) 1370857    # 命令耗时
   4)  1) "SUBSCRIBE"      # 执行命令
       2) "celery-task"    # 执行参数
1.2发现大对象

执行 redis-cli –bigkeys

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
  # 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 list   found so far 'mylist' with 30000 items
	[00.00%] Biggest string found so far 'key:__rand_int__' with 3 bytes
	[00.00%] Biggest string found so far 'counter:__rand_int__' with 5 bytes

	-------- summary -------

	Sampled 4 keys in the keyspace!
	Total key length in bytes is 43 (avg len 10.75)

	Biggest string found 'counter:__rand_int__' has 5 bytes
	Biggest   list found 'mylist' has 30000 items

	3 strings with 9 bytes (75.00% of keys, avg size 3.00)
	1 lists with 30000 items (25.00% of keys, avg size 30000.00)
	0 sets with 0 members (00.00% of keys, avg size 0.00)
	0 hashs with 0 fields (00.00% of keys, avg size 0.00)
	0 zsets with 0 members (00.00% of keys, avg size 0.00)

根据结果获取到大对象的键,以及不同类型数据 结构的使用情况

2.cpu饱和

执行 redis-cli –stat 结果

1
2
3
4
5
6
7
------- data ------ --------------------- load -------------------- - child -
	keys mem clients blocked requests connections
	3789785 3.20G 507 0 8867955607 (+0) 555894
	3789813 3.20G 507 0 8867959511 (+63904) 555894
	3789822 3.20G 507 0 8867961602 (+62091) 555894
	3789831 3.20G 507 0 8867965049 (+63447) 555894
	3789842 3.20G 507 0 8867969520 (+62675) 555894

可以发现单redis实例QPS每秒处理请求达到6w+ 这种情况下考虑集群水平拓展分摊QPS压力