HiSEN

Redis Cluster slot 分布不均匀问题排查

零、背景

在压测过程中发现有部分 redis cluster 节点内存占用比其它节点高(来自监控)

内存倾斜的隐患

  1. 更早达到容量瓶颈,触发淘汰策略
  2. 承担更多的负载( 读 / 写 ),导致节点压力增大,可能触发宕机。

一、问题

redis cluster slot 分配不均匀
redis cluster 集群内存分配算法的缺陷
问题算法:单节点内存 = 集群总内存 / 节点数

合理算法:单节点内存 = (集群总内容 / slot 数量) * 当前节点 slot 数量

二、排查

2.1 key 分布问题?

key 根据 crc16 计算之后比较均匀,排除 key 分布不均
在 key 均匀的情况下,考虑 slot 分配问题

2.2 slot 分布问题?

查找 slot 分布
redis 控制台执行:cluster slots
取最后一个节点的 rely 日志如下(日志含义见参考链接)

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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
(0)(0)10137
(1)10463
(2)(0)192.25.238.97
(1)2048
(2)1ab1735417e34e1fb38aa19958e2375498158be8
(3)(0)192.25.239.118
(1)2049
(2)2b444bb8ece6edcf4881cfacb60def2eb3bfba85
(1)(0)6540
(1)6866
(2)(0)192.168.184.118
(1)2048
(2)464c059fd4354b72cd277a7aaf6159a2682e47ff
(3)(0)192.168.198.232
(1)2048
(2)c4ae0536b5a9157b43516e26c4c77ca9e431005a
(2)(0)1308
(1)1634
(2)(0)192.168.197.41
(1)2048
(2)bb0b682b4eed16f773ed7b0d2cf430ac4e776624
(3)(0)192.25.222.250
(1)2048
(2)098251c97fb147b7aee8143ced13c742ec3115a9
(3)(0)8502
(1)8828
(2)(0)192.168.184.111
(1)2048
(2)3680ab0d0961e4dda6f9a35eb4c72cbcc04bba3b
(3)(0)192.168.176.75
(1)2048
(2)99d64ca4bf375460537a296ce0bdab2374c8487c
(4)(0)14388
(1)14714
(2)(0)192.25.239.116
(1)2048
(2)37d438fc43097bab896e04589b10659c2898c56b
(3)(0)192.168.184.113
(1)2048
(2)2697f554dac4c6b6452239b5f3f192052504431f
(5)(0)1962
(1)2288
(2)(0)192.168.198.241
(1)2049
(2)7c323af2d42df59cb172c1f2989fdb47972c4b6b
(3)(0)192.168.184.112
(1)2048
(2)94f67ff376efbecf3d84905dbfe6d9e8a5c9cf83
(6)(0)0
(1)326
(2)(0)192.168.197.9
(1)2048
(2)510edbf404ed2b24aeeff53f9d8f43c6404d62c0
(3)(0)192.168.198.249
(1)5010
(2)a150d1f3671d8a5e42c238e900a23031693ddd32
(7)(0)12426
(1)12752
(2)(0)192.168.198.240
(1)5010
(2)cb2c653239757df27da883dc024e87a8f11890f0
(3)(0)192.168.197.41
(1)2049
(2)ed4a2925e913bc2773cd3a4c13d48deac7f423fa
(8)(0)4251
(1)4577
(2)(0)192.25.239.105
(1)2048
(2)f2eb3bb1afc0d9345173d1eb2aaa675161f5d675
(3)(0)192.168.198.242
(1)2048
(2)67a2926615eb6e4e4f850f59f78f670010ef573a
(9)(0)9156
(1)9482
(2)(0)192.25.239.107
(1)5010
(2)4806e385ba1b193b48c0c6a8dd35e5d8a5738257
(3)(0)192.25.239.105
(1)2048
(2)c1490e0c9963628972be63339f032c63191f1897
(10)(0)6867
(1)7193
(2)(0)192.168.184.115
(1)2048
(2)2339d951c551c45daf84c147acaf6e6854f66f8c
(3)(0)192.25.222.250
(1)2049
(2)cd6e7d09828b8d795afe671c39a0312b3d6859fe
(11)(0)2616
(1)2942
(2)(0)192.168.198.234
(1)2048
(2)e5bfd81c7a0df2ec29b3ac4ce188795881b2bc1e
(3)(0)192.25.238.96
(1)2049
(2)513edbe433f180924500db83ef155426e7f2f040
(12)(0)5559
(1)5885
(2)(0)192.168.198.252
(1)2048
(2)515990729e830f0f75e3dc8179438d43c6c7c9b7
(3)(0)192.25.238.92
(1)2048
(2)5328403d6ecd5272c51250977cc1af7de3f16407
(13)(0)15369
(1)15695
(2)(0)192.168.176.78
(1)2049
(2)a140264d0aefdbb9b99c961c0c223266fa90129d
(3)(0)192.168.197.32
(1)2049
(2)5fbc13a478470e14006500e38fa032472ec0da50
(14)(0)12753
(1)13079
(2)(0)192.168.198.249
(1)2049
(2)120c04813a4f2db04bfc890874a5cf4e7ce47e15
(3)(0)192.168.197.9
(1)2048
(2)e8ca0053b9a99684f683202d68994cf8b28f26e0
(15)(0)4905
(1)5231
(2)(0)192.25.239.120
(1)2048
(2)e1a5f230ad1a7716c4e04bc785fcb001e7841b7f
(3)(0)192.168.176.76
(1)2049
(2)c370b87def3a5533ca436ac17ed22d2d5ef64cf7
(16)(0)15696
(1)16022
(2)(0)192.168.197.24
(1)2048
(2)ebad5e02b596b81431e7e35d80687b94eb4f495b
(3)(0)192.168.176.77
(1)5013
(2)96771bd26c4f0f3af5a1cb3de4de6467e07f1a6d
(17)(0)11118
(1)11444
(2)(0)192.168.198.147
(1)2049
(2)5e16b0661d43a03254fe0c9a97a21beada5665eb
(3)(0)192.168.184.109
(1)5010
(2)c132bdd8ab5c38d4cce4bee4ffc1a298e75d5538
(18)(0)16023
(1)16383
(2)(0)192.168.176.76
(1)2048
(2)e9b9b9354d6c77a980c0da35bef89b26042b716d
(3)(0)192.168.198.146
(1)2048
(2)1ca744d6e5c24d3f2c64eca1dec65eea8545ce8b
(19)(0)4578
(1)4904
(2)(0)192.25.239.104
(1)2048
(2)0aea965323c78cdefb3e8b4e03955b9651c719b6
(3)(0)192.168.198.240
(1)2049
(2)365350cfa7621749e2c696d84d32d2228122fdc6
(20)(0)10464
(1)10790
(2)(0)192.168.198.232
(1)2048
(2)2fe68c5065aefc24ddcc747071dd65c6cadd427b
(3)(0)192.168.184.118
(1)2049
(2)bdc2191de10b7d825f359922e3e31ce328c0a972
(21)(0)14061
(1)14387
(2)(0)192.168.198.231
(1)2049
(2)7a47f437951b03a95460924ffd349b3ecfb05bc7
(3)(0)192.168.184.114
(1)2048
(2)bfebcd3837ccf9df6989be91951087cb7351404c
(22)(0)9810
(1)10136
(2)(0)192.168.184.116
(1)2048
(2)3caff203db4214bea1f2bcbc2935bb3e2273ac72
(3)(0)192.25.223.2
(1)2048
(2)d2fe724704165a35eef6541f75cb2ba5245472fa
(23)(0)327
(1)653
(2)(0)192.25.238.90
(1)2049
(2)6254a81f003cce2cb66222dc312c0ad4ee0a900a
(3)(0)192.25.223.2
(1)2049
(2)7781d87ca77b51fcd07c8d0956ab350e3db3ba1c
(24)(0)9483
(1)9809
(2)(0)192.168.198.254
(1)2048
(2)a0acf25a9487c3fa6fc74af8ba57cc50d1b1a95c
(3)(0)192.25.239.119
(1)2048
(2)e05f23f53bad3f8ccc7ae4f9080426e289350557
(25)(0)12099
(1)12425
(2)(0)192.168.184.110
(1)2048
(2)39d6559849aa2d00b560cb554cfa79a30ee3ede1
(3)(0)192.168.198.247
(1)5010
(2)fe6696fa4333b4776ed5ee38b93380a21755eaa0
(26)(0)3924
(1)4250
(2)(0)192.168.198.180
(1)2048
(2)ba944b0643a2ad68033d129d78c1dee805d2191a
(3)(0)192.25.239.121
(1)2049
(2)b5b020b3105be1e4a49c57d54647da6797a0a3cc
(27)(0)14715
(1)15041
(2)(0)192.168.198.147
(1)2048
(2)937e58683236b2708fdc6ef77dd08e66ef7e7ad5
(3)(0)192.25.239.120
(1)2048
(2)274760f1e7e84bd14593b43f95965546f4cb34f5
(28)(0)1635
(1)1961
(2)(0)192.168.198.239
(1)2048
(2)ae4dc5d4d544dde49f177554c89e2001727d99e4
(3)(0)192.168.198.250
(1)2048
(2)bea5260033bac6fb7ab67a444f9a1d0e8c9f93b9
(29)(0)5886
(1)6212
(2)(0)192.25.239.116
(1)2049
(2)7c9f26573b5a79b77d9089ed7e5783f47f6d030d
(3)(0)192.25.239.104
(1)2048
(2)5c999bc681e6751ec576049db73c3bee2717f8b6
(30)(0)3270
(1)3596
(2)(0)192.168.198.179
(1)2048
(2)addeda3c397b1d6ca21996be33435f4a65ca7686
(3)(0)192.168.198.231
(1)5010
(2)1d78a81025acdc123f04680a30a80cff6258e909
(31)(0)2289
(1)2615
(2)(0)192.25.239.117
(1)2049
(2)b8ff018e1810697710963e0c4a17faca2b3b6421
(3)(0)192.168.184.111
(1)2049
(2)f7cc831c676ec19560f6ea779f86971bd227ba7f
(32)(0)6213
(1)6539
(2)(0)192.168.184.113
(1)2048
(2)ee943d8aec010e34fe1b573826d65cc9e1f14fce
(3)(0)192.168.198.242
(1)2049
(2)bfa424b2f9d4e476c3985025f5bf8bdb6625b74a
(33)(0)654
(1)980
(2)(0)192.25.239.106
(1)5010
(2)fa13eedd26af4e0364f56b1604cef9865afde98d
(3)(0)192.168.198.251
(1)2049
(2)ea87dd7cbf8b8cbeee37a3d482493b5fc41a755b
(34)(0)2943
(1)3269
(2)(0)192.25.239.118
(1)5010
(2)150932a7e4f0020b99c50312c8cec9d07d7aabcf
(3)(0)192.25.239.121
(1)5010
(2)816d511dcab3f7d9e2068401ba5958e01f2212dd
(35)(0)8175
(1)8501
(2)(0)192.168.198.148
(1)2048
(2)34d51673796ab652da7e312cff119296dae1d3bb
(3)(0)192.168.197.28
(1)2048
(2)e1f48544722bbb62d1fb8910a6a4b00d9ccbd09b
(36)(0)981
(1)1307
(2)(0)192.168.197.32
(1)5010
(2)41ea02b87652650c3ba2f085849fb6ac2169d897
(3)(0)192.168.197.28
(1)2049
(2)9717393eb89ab32126263741fd250198e378023c
(37)(0)5232
(1)5558
(2)(0)192.168.184.109
(1)2049
(2)96a728eece26f6f200b26ff9ddecae3fb5b8df11
(3)(0)192.25.239.117
(1)2048
(2)69f84dd28d8c254732a4f0d5e5fd56c60c58b7fa
(38)(0)3597
(1)3923
(2)(0)192.168.198.146
(1)2048
(2)526a0bc43f480738c704ea745f5377258c0e3cdb
(3)(0)192.168.176.78
(1)5010
(2)f54b7898236acf827c53277f22fc3dcadcd37602
(39)(0)15042
(1)15368
(2)(0)192.168.197.37
(1)2048
(2)57ceee2905d66bd5863acae5422bf9e39afa971d
(3)(0)192.168.198.179
(1)2048
(2)55192b8693c15df7f11cffc788f2043275886002
(40)(0)10791
(1)11117
(2)(0)192.25.222.249
(1)2048
(2)3c9b5f682360324951435b987489fbc2e4e5bfc3
(3)(0)192.168.198.251
(1)2048
(2)2dcf8603ecbdf43d18112dcaf06d3f9321bc2e0c
(41)(0)7194
(1)7520
(2)(0)192.168.184.112
(1)2048
(2)8a77d41c2bb0a0672654baecfdee52592bb04050
(3)(0)192.25.238.96
(1)2048
(2)8632629188cb269a900677ec0dc395cb292c1e2e
(42)(0)8829
(1)9155
(2)(0)192.168.198.241
(1)2048
(2)37684e5201b0fb50eaf184235a87f2e6a359dd01
(3)(0)192.168.184.115
(1)2048
(2)4684a38fa0b4df0b21f0d777b91300afcace551a
(43)(0)7848
(1)8174
(2)(0)192.168.184.114
(1)2048
(2)a6f9c01f4b3b2df4b4993f5745b78d1307d6ea81
(3)(0)192.168.197.37
(1)2049
(2)51e3568c115be10067d9e808d094f71f96c92647
(44)(0)13734
(1)14060
(2)(0)192.168.198.182
(1)2048
(2)6acca7f88ea98f8ba073f423707d7692592053f3
(3)(0)192.25.238.90
(1)2048
(2)84965ce2c21634a7ce9405374e36e80fa0e4521b
(45)(0)13407
(1)13733
(2)(0)192.168.184.117
(1)2048
(2)2fad26dc888fc776dec4fd33e167c860bd76d64e
(3)(0)192.25.239.181
(1)5090
(2)c91e8c9f8aea8424683258e18c5aa93ec45bb216
(46)(0)11772
(1)12098
(2)(0)192.168.199.1
(1)2048
(2)77c4678d6eddc2f8eb328fa02705d99a6c27a1ca
(3)(0)192.25.239.176
(1)5074
(2)21106c8a361da9c3cf494857e168c5b640eca234
(47)(0)7521
(1)7847
(2)(0)192.25.223.1
(1)2048
(2)ef990477dd7eb4b3c9915bf31b300e87d8fa9b29
(3)(0)192.25.239.106
(1)2049
(2)b0a2975c5cc56a69b3bc63b348a88252fa09ec1a
(48)(0)13080
(1)13406
(2)(0)192.168.198.182
(1)2049
(2)01baf618cc0945c5a379a37ff92e4b1e1eeefac8
(3)(0)192.168.184.116
(1)2048
(2)041464f26f3e7bdb277d0624ba7003bb447f227b
(49)(0)11445
(1)11771
(2)(0)192.25.239.107
(1)2049
(2)0495711f467db7ef747eeb9a24f16b7a5ce0c4ff
(3)(0)192.168.198.234
(1)2048
(2)34d285e446013db478a030f3172fce256b6d4ba8
reply from: 192.168.197.41:2048

通过 awk 计算每个节点管理的槽 ( slot ) 数量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 把上面的日志粘贴到 slots 文件中
$ vi /tmp/slots
# 计算每个节点的槽数量并且排序,输出结果
$ cat /tmp/slots | awk '{if((NR % 8 == 1) || (NR % 8 == 2)) print $0}' | awk '{if(NR % 2 == 1) print $0,getline,$0}' | sed 's/)/ /g' | awk '{print $6,$3,$6-$3}' | sort -k 3 -r | head -n 3
16383 16023 360
9809 9483 326
980 654 326

# 根据槽号反向查找
$ cat /tmp/slots| grep 16023 -A8
(18)(0)16023
(1)16383
(2)(0)192.168.176.76
(1)2049
(2)e9b9b9354d6c77a980c0da35bef89b26042b716d
(3)(0)192.168.198.146
(1)2048
(2)1ca744d6e5c24d3f2c64eca1dec65eea8545ce8b
(19)(0)4578

由于本案例当中,大部分节点都是包含 326 个 slot,问题节点明显偏多 360

2.2.1 相关命令解释

awk NR 代表行号
awk getline 获取下一行,并且赋值到 $0 (本意是代表当前行数据)。
sort -k 3 -r,以第三列排序(-k 3),倒序输出(-r)
head -n 3,输出前三行

三、结论

根据上述假设,经过验证,发现确实是由于 slot 分配不均匀导致

四、改进

人工手动调整 slot 较多节点的内存,使之达到与其它节点内存占用水平。
提示:redis 可以通过修改参数( maxmemory,单位 byte )调整最大内存。

五、参考