-
Notifications
You must be signed in to change notification settings - Fork 1
/
15246678052226.html
638 lines (411 loc) · 20.3 KB
/
15246678052226.html
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
<!doctype html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>
阿里云Redis开发规范 - Junkman
</title>
<link href="atom.xml" rel="alternate" title="Junkman" type="application/atom+xml">
<link rel="stylesheet" href="asset/css/foundation.min.css" />
<link rel="stylesheet" href="asset/css/docs.css" />
<script src="asset/js/vendor/modernizr.js"></script>
<script src="asset/js/vendor/jquery.js"></script>
<script src="asset/highlightjs/highlight.pack.js"></script>
<link href="asset/highlightjs/styles/github.css" media="screen, projection" rel="stylesheet" type="text/css">
<script>hljs.initHighlightingOnLoad();</script>
<script type="text/javascript">
function before_search(){
var searchVal = 'site:panlw.github.io ' + document.getElementById('search_input').value;
document.getElementById('search_q').value = searchVal;
return true;
}
</script>
</head>
<body class="antialiased hide-extras">
<div class="marketing off-canvas-wrap" data-offcanvas>
<div class="inner-wrap">
<nav class="top-bar docs-bar hide-for-small" data-topbar>
<section class="top-bar-section">
<div class="row">
<div style="position: relative;width:100%;"><div style="position: absolute; width:100%;">
<ul id="main-menu" class="left">
<li id=""><a target="self" href="index.html">Home</a></li>
<li id=""><a target="_self" href="archives.html">Archives</a></li>
</ul>
<ul class="right" id="search-wrap">
<li>
<form target="_blank" onsubmit="return before_search();" action="http://google.com/search" method="get">
<input type="hidden" id="search_q" name="q" value="" />
<input tabindex="1" type="search" id="search_input" placeholder="Search"/>
</form>
</li>
</ul>
</div></div>
</div>
</section>
</nav>
<nav class="tab-bar show-for-small">
<a href="javascript:void(0)" class="left-off-canvas-toggle menu-icon">
<span> Junkman</span>
</a>
</nav>
<aside class="left-off-canvas-menu">
<ul class="off-canvas-list">
<li><a href="index.html">HOME</a></li>
<li><a href="archives.html">Archives</a></li>
<li><a href="about.html">ABOUT</a></li>
<li><label>Categories</label></li>
<li><a href="Infra.html">Infra</a></li>
<li><a href="Coding.html">Coding</a></li>
<li><a href="Modeling.html">Modeling</a></li>
<li><a href="Archtecting.html">Archtecting</a></li>
</ul>
</aside>
<a class="exit-off-canvas" href="#"></a>
<section id="main-content" role="main" class="scroll-container">
<script type="text/javascript">
$(function(){
$('#menu_item_index').addClass('is_active');
});
</script>
<div class="row">
<div class="large-8 medium-8 columns">
<div class="markdown-body article-wrap">
<div class="article">
<h1>阿里云Redis开发规范</h1>
<div class="read-more clearfix">
<span class="date">2018/4/25</span>
<span>posted in </span>
<span class="posted-in"><a href='Middleware.html'>Middleware</a></span>
<span class="comments">
</span>
</div>
</div><!-- article -->
<div class="article-content">
<blockquote>
<p><a href="https://yq.aliyun.com/articles/531067">https://yq.aliyun.com/articles/531067</a></p>
</blockquote>
<h2 id="toc_0">一、键值设计</h2>
<h3 id="toc_1">1. key名设计</h3>
<ul>
<li> (1)【建议】: 可读性和可管理性</li>
</ul>
<p>以业务名(或数据库名)为前缀(防止key冲突),用冒号分隔,比如业务名:表名:id</p>
<pre><code>ugc:video:1
</code></pre>
<ul>
<li> (2)【建议】:简洁性</li>
</ul>
<p>保证语义的前提下,控制key的长度,当key较多时,内存占用也不容忽视,例如:</p>
<pre><code>user:{uid}:friends:messages:{mid}简化为u:{uid}:fr:m:{mid}。
</code></pre>
<ul>
<li> (3)【强制】:不要包含特殊字符</li>
</ul>
<p>反例:包含空格、换行、单双引号以及其他转义字符</p>
<h3 id="toc_2">2. value设计</h3>
<ul>
<li> (1)【强制】:拒绝bigkey(防止网卡流量、慢查询)</li>
</ul>
<p>string类型控制在10KB以内,hash、list、set、zset元素个数不要超过5000。</p>
<p>反例:一个包含200万个元素的list。</p>
<p>非字符串的bigkey,不要使用del删除,使用hscan、sscan、zscan方式渐进式删除,同时要注意防止bigkey过期时间自动删除问题(例如一个200万的zset设置1小时过期,会触发del操作,造成阻塞,而且该操作不会不出现在慢查询中(latency可查)),<a href="#cc1">查找方法</a>和<a href="#cc2">删除方法</a></p>
<ul>
<li> (2)【推荐】:选择适合的数据类型。</li>
</ul>
<p>例如:实体类型(要合理控制和使用数据结构内存编码优化配置,例如ziplist,但也要注意节省内存和性能之间的平衡)</p>
<p>反例:</p>
<pre><code>set user:1:name tom
set user:1:age 19
set user:1:favor football
</code></pre>
<p>正例:</p>
<pre><code>hmset user:1 name tom age 19 favor football
</code></pre>
<h3 id="toc_3">3.【推荐】:控制key的生命周期,redis不是垃圾桶。</h3>
<p>建议使用expire设置过期时间(条件允许可以打散过期时间,防止集中过期),不过期的数据重点关注idletime。</p>
<h3 id="toc_4">二、命令使用</h3>
<h3 id="toc_5">1.【推荐】 O(N)命令关注N的数量</h3>
<p>例如hgetall、lrange、smembers、zrange、sinter等并非不能使用,但是需要明确N的值。有遍历的需求可以使用hscan、sscan、zscan代替。</p>
<h3 id="toc_6">2.【推荐】:禁用命令</h3>
<p>禁止线上使用keys、flushall、flushdb等,通过redis的rename机制禁掉命令,或者使用scan的方式渐进式处理。</p>
<h3 id="toc_7">3.【推荐】合理使用select</h3>
<p>redis的多数据库较弱,使用数字进行区分,很多客户端支持较差,同时多业务用多数据库实际还是单线程处理,会有干扰。</p>
<h3 id="toc_8">4.【推荐】使用批量操作提高效率</h3>
<pre><code>原生命令:例如mget、mset。
非原生命令:可以使用pipeline提高效率。
</code></pre>
<p>但要注意控制一次批量操作的<strong>元素个数</strong>(例如500以内,实际也和元素字节数有关)。</p>
<p>注意两者不同:</p>
<pre><code>1\. 原生是原子操作,pipeline是非原子操作。
2\. pipeline可以打包不同的命令,原生做不到
3\. pipeline需要客户端和服务端同时支持。
</code></pre>
<h3 id="toc_9">5.【建议】Redis事务功能较弱,不建议过多使用</h3>
<p>Redis的事务功能较弱(不支持回滚),而且集群版本(自研和官方)要求一次事务操作的key必须在一个slot上(可以使用hashtag功能解决)</p>
<h3 id="toc_10">6.【建议】Redis集群版本在使用Lua上有特殊要求:</h3>
<ul>
<li> 1.所有key都应该由 KEYS 数组来传递,redis.call/pcall 里面调用的redis命令,key的位置,必须是KEYS array, 否则直接返回error,"-ERR bad lua script for redis cluster, all the keys that the script uses should be passed using the KEYS array"</li>
<li> 2.所有key,必须在1个slot上,否则直接返回error, "-ERR eval/evalsha command keys must in same slot"</li>
</ul>
<h3 id="toc_11">7.【建议】必要情况下使用monitor命令时,要注意不要长时间使用。</h3>
<h3 id="toc_12">三、客户端使用</h3>
<h3 id="toc_13">1.【推荐】</h3>
<p>避免多个应用使用一个Redis实例</p>
<p>正例:不相干的业务拆分,公共数据做服务化。</p>
<h3 id="toc_14">2.【推荐】</h3>
<p>使用带有连接池的数据库,可以有效控制连接,同时提高效率,标准使用方式:</p>
<pre><code>执行命令如下:
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
//具体的命令
jedis.executeCommand()
} catch (Exception e) {
logger.error("op key {} error: " + e.getMessage(), key, e);
} finally {
//注意这里不是关闭连接,在JedisPool模式下,Jedis会被归还给资源池。
if (jedis != null)
jedis.close();
}
</code></pre>
<p>下面是JedisPool优化方法的文章:</p>
<ul>
<li> <a href="https://yq.aliyun.com/articles/236384">Jedis常见异常汇总</a></li>
<li> <a href="https://yq.aliyun.com/articles/236383">JedisPool资源池优化</a></li>
</ul>
<h3 id="toc_15">3.【建议】</h3>
<p>高并发下建议客户端添加熔断功能(例如netflix hystrix)</p>
<h3 id="toc_16">4.【推荐】</h3>
<p>设置合理的密码,如有必要可以使用SSL加密访问(阿里云Redis支持)</p>
<h3 id="toc_17">5.【建议】</h3>
<p>根据自身业务类型,选好maxmemory-policy(最大内存淘汰策略),设置好过期时间。</p>
<p>默认策略是volatile-lru,即超过最大内存后,在过期键中使用lru算法进行key的剔除,保证不过期数据不被删除,但是可能会出现OOM问题。</p>
<h4 id="toc_18">其他策略如下:</h4>
<ul>
<li> allkeys-lru:根据LRU算法删除键,不管数据有没有设置超时属性,直到腾出足够空间为止。</li>
<li> allkeys-random:随机删除所有键,直到腾出足够空间为止。</li>
<li> volatile-random:随机删除过期键,直到腾出足够空间为止。</li>
<li> volatile-ttl:根据键值对象的ttl属性,删除最近将要过期数据。如果没有,回退到noeviction策略。</li>
<li> noeviction:不会剔除任何数据,拒绝所有写入操作并返回客户端错误信息"(error) OOM command not allowed when used memory",此时Redis只响应读操作。</li>
</ul>
<h3 id="toc_19">四、相关工具</h3>
<h4 id="toc_20">1.【推荐】:数据同步</h4>
<p>redis间数据同步可以使用:redis-port</p>
<p><a></a></p>
<h4 id="toc_21">2.【推荐】:big key搜索</h4>
<p><a href="https://yq.aliyun.com/articles/117042">redis大key搜索工具</a></p>
<h4 id="toc_22">3.【推荐】:热点key寻找(内部实现使用monitor,所以建议短时间使用)</h4>
<p><a href="https://github.com/facebookarchive/redis-faina">facebook的redis-faina</a></p>
<pre><code>阿里云Redis已经在内核层面解决热点key问题,欢迎使用。
</code></pre>
<p><a></a></p>
<h2 id="toc_23">五 附录:删除bigkey</h2>
<pre><code>1\. 下面操作可以使用pipeline加速。
2\. redis 4.0已经支持key的异步删除,欢迎使用。
</code></pre>
<h4 id="toc_24">1. Hash删除: hscan + hdel</h4>
<pre><code>public void delBigHash(String host, int port, String password, String bigHashKey) {
Jedis jedis = new Jedis(host, port);
if (password != null && !"".equals(password)) {
jedis.auth(password);
}
ScanParams scanParams = new ScanParams().count(100);
String cursor = "0";
do {
ScanResult<Entry<String, String>> scanResult = jedis.hscan(bigHashKey, cursor, scanParams);
List<Entry<String, String>> entryList = scanResult.getResult();
if (entryList != null && !entryList.isEmpty()) {
for (Entry<String, String> entry : entryList) {
jedis.hdel(bigHashKey, entry.getKey());
}
}
cursor = scanResult.getStringCursor();
} while (!"0".equals(cursor));
//删除bigkey
jedis.del(bigHashKey);
}
</code></pre>
<h4 id="toc_25">2. List删除: ltrim</h4>
<pre><code>public void delBigList(String host, int port, String password, String bigListKey) {
Jedis jedis = new Jedis(host, port);
if (password != null && !"".equals(password)) {
jedis.auth(password);
}
long llen = jedis.llen(bigListKey);
int counter = 0;
int left = 100;
while (counter < llen) {
//每次从左侧截掉100个
jedis.ltrim(bigListKey, left, llen);
counter += left;
}
//最终删除key
jedis.del(bigListKey);
}
</code></pre>
<h4 id="toc_26">3. Set删除: sscan + srem</h4>
<pre><code>public void delBigSet(String host, int port, String password, String bigSetKey) {
Jedis jedis = new Jedis(host, port);
if (password != null && !"".equals(password)) {
jedis.auth(password);
}
ScanParams scanParams = new ScanParams().count(100);
String cursor = "0";
do {
ScanResult<String> scanResult = jedis.sscan(bigSetKey, cursor, scanParams);
List<String> memberList = scanResult.getResult();
if (memberList != null && !memberList.isEmpty()) {
for (String member : memberList) {
jedis.srem(bigSetKey, member);
}
}
cursor = scanResult.getStringCursor();
} while (!"0".equals(cursor));
//删除bigkey
jedis.del(bigSetKey);
}
</code></pre>
<h4 id="toc_27">4. SortedSet删除: zscan + zrem</h4>
<pre><code>public void delBigZset(String host, int port, String password, String bigZsetKey) {
Jedis jedis = new Jedis(host, port);
if (password != null && !"".equals(password)) {
jedis.auth(password);
}
ScanParams scanParams = new ScanParams().count(100);
String cursor = "0";
do {
ScanResult<Tuple> scanResult = jedis.zscan(bigZsetKey, cursor, scanParams);
List<Tuple> tupleList = scanResult.getResult();
if (tupleList != null && !tupleList.isEmpty()) {
for (Tuple tuple : tupleList) {
jedis.zrem(bigZsetKey, tuple.getElement());
}
}
cursor = scanResult.getStringCursor();
} while (!"0".equals(cursor));
//删除bigkey
jedis.del(bigZsetKey);
}
</code></pre>
<h3 id="toc_28">招聘:<a href="https://job.alibaba.com/zhaopin/position_detail.htm?positionId=40132">阿里云-技术专家-KVstore</a></h3>
<p>岗位描述:</p>
<ul>
<li> 负责阿里云Redis源码开发维护</li>
<li> 负责阿里云Redis cluster开发与设计</li>
</ul>
<p>岗位要求:</p>
<ul>
<li> 精通C/C++,熟悉TCP, Linux Kernel等优先</li>
<li> 数据结构,算法等基础知识扎实</li>
<li> 5年后台系统的设计与开发,或3年分布式系统的设计与开发,运维过大型分布式系统</li>
<li> 精通至少一项开源NoSQL产品。Redis,mongodb,memcached等优先。</li>
<li> 有云服务产品或基于SSD的系统开发经验优先</li>
<li> 善于创新,乐于挑战,有责任心,良好团队精神</li>
<li> 良好的表达能力,能够清晰和准确地描述问题,发现并解决问题能力</li>
</ul>
</div>
<div class="row">
<div class="large-6 columns">
<p class="text-left" style="padding:15px 0px;">
<a href="15248824810738.html"
title="Previous Post: 理解JWT的使用场景和优劣">« 理解JWT的使用场景和优劣</a>
</p>
</div>
<div class="large-6 columns">
<p class="text-right" style="padding:15px 0px;">
<a href="15244133646661.html"
title="Next Post: Native Extension for Node.js">Native Extension for Node.js »</a>
</p>
</div>
</div>
<div class="comments-wrap">
<div class="share-comments">
<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-5ae58078c0d7b2ab"></script>
</div>
</div>
</div><!-- article-wrap -->
</div><!-- large 8 -->
<div class="large-4 medium-4 columns">
<div class="hide-for-small">
<div id="sidebar" class="sidebar">
<div id="site-info" class="site-info">
<div class="site-a-logo"><img src="./asset/img/logo.jpg" /></div>
<h1>Junkman</h1>
<div class="site-des">“拾荒者”一词来自凯文・凯利的《失控》中关于机器学习的故事(“收集癖好机”如何完成他的收集工作)。</div>
<div class="social">
<a target="_blank" class="github" target="_blank" href="https://github.com/panlw/" title="GitHub">GitHub</a>
<a target="_blank" class="rss" href="atom.xml" title="RSS">RSS</a>
</div>
</div>
<div id="site-categories" class="side-item ">
<div class="side-header">
<h2>Categories</h2>
</div>
<div class="side-content">
<p class="cat-list">
<a href="Infra.html"><strong>Infra</strong></a>
<a href="Coding.html"><strong>Coding</strong></a>
<a href="Modeling.html"><strong>Modeling</strong></a>
<a href="Archtecting.html"><strong>Archtecting</strong></a>
</p>
</div>
</div>
<div id="site-categories" class="side-item">
<div class="side-header">
<h2>Recent Posts</h2>
</div>
<div class="side-content">
<ul class="posts-list">
<li class="post">
<a href="15517999043443.html">The Art of Crafting Architectural Diagrams</a>
</li>
<li class="post">
<a href="15517997955971.html">为什么说我们需要软件架构图?</a>
</li>
<li class="post">
<a href="15516128677869.html">DNS Servers That Offer Privacy and Filtering</a>
</li>
<li class="post">
<a href="15516123108194.html">Airbnb's Migration from Monolith to Services</a>
</li>
<li class="post">
<a href="15516097487470.html">Events As First-Class Citizens</a>
</li>
</ul>
</div>
</div>
</div><!-- sidebar -->
</div><!-- hide for small -->
</div><!-- large 4 -->
</div><!-- row -->
<div class="page-bottom clearfix">
<div class="row">
<p class="copyright">Copyright © 2015
Powered by <a target="_blank" href="http://www.mweb.im">MWeb</a>,
Theme used <a target="_blank" href="http://github.com">GitHub CSS</a>.</p>
</div>
</div>
</section>
</div>
</div>
<script src="asset/js/foundation.min.js"></script>
<script>
$(document).foundation();
function fixSidebarHeight(){
var w1 = $('.markdown-body').height();
var w2 = $('#sidebar').height();
if (w1 > w2) { $('#sidebar').height(w1); };
}
$(function(){
fixSidebarHeight();
})
$(window).load(function(){
fixSidebarHeight();
});
</script>
<script src="asset/chart/all-min.js"></script><script type="text/javascript">$(function(){ var mwebii=0; var mwebChartEleId = 'mweb-chart-ele-'; $('pre>code').each(function(){ mwebii++; var eleiid = mwebChartEleId+mwebii; if($(this).hasClass('language-sequence')){ var ele = $(this).addClass('nohighlight').parent(); $('<div id="'+eleiid+'"></div>').insertAfter(ele); ele.hide(); var diagram = Diagram.parse($(this).text()); diagram.drawSVG(eleiid,{theme: 'simple'}); }else if($(this).hasClass('language-flow')){ var ele = $(this).addClass('nohighlight').parent(); $('<div id="'+eleiid+'"></div>').insertAfter(ele); ele.hide(); var diagram = flowchart.parse($(this).text()); diagram.drawSVG(eleiid); } });});</script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><script type="text/x-mathjax-config">MathJax.Hub.Config({TeX: { equationNumbers: { autoNumber: "AMS" } }});</script>
</body>
</html>