自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

阿里巴巴官方最新Redis開發(fā)規(guī)范!

運(yùn)維 數(shù)據(jù)庫運(yùn)維 Redis
本文主要介紹在使用阿里云Redis的開發(fā)規(guī)范,從下面幾個(gè)方面進(jìn)行說明。一起來看一下吧。

 [[358640]]

本文主要介紹在使用阿里云Redis的開發(fā)規(guī)范,從下面幾個(gè)方面進(jìn)行說明。

  •  鍵值設(shè)計(jì)
  •  命令使用
  •  客戶端使用
  •  相關(guān)工具

通過本文的介紹可以減少使用Redis過程帶來的問題。

一、鍵值設(shè)計(jì)

1、key名設(shè)計(jì)

可讀性和可管理性

以業(yè)務(wù)名(或數(shù)據(jù)庫名)為前綴(防止key沖突),用冒號(hào)分隔,比如業(yè)務(wù)名:表名:id

  1. ugc:video:1 

簡(jiǎn)潔性

保證語義的前提下,控制key的長(zhǎng)度,當(dāng)key較多時(shí),內(nèi)存占用也不容忽視,例如: 

  1. user:{uid}:friends:messages:{mid}簡(jiǎn)化為u:{uid}:fr:m:{mid}。 

不要包含特殊字符

反例:包含空格、換行、單雙引號(hào)以及其他轉(zhuǎn)義字符

2、value設(shè)計(jì)

拒絕bigkey

防止網(wǎng)卡流量、慢查詢,string類型控制在10KB以內(nèi),hash、list、set、zset元素個(gè)數(shù)不要超過5000。

反例:一個(gè)包含200萬個(gè)元素的list。

非字符串的bigkey,不要使用del刪除,使用hscan、sscan、zscan方式漸進(jìn)式刪除,同時(shí)要注意防止bigkey過期時(shí)間自動(dòng)刪除問題(例如一個(gè)200萬的zset設(shè)置1小時(shí)過期,會(huì)觸發(fā)del操作,造成阻塞,而且該操作不會(huì)不出現(xiàn)在慢查詢中(latency可查)),查找方法和刪除方法 選擇適合的數(shù)據(jù)類型

例如:實(shí)體類型(要合理控制和使用數(shù)據(jù)結(jié)構(gòu)內(nèi)存編碼優(yōu)化配置,例如ziplist,但也要注意節(jié)省內(nèi)存和性能之間的平衡) 反例: 

  1. set user:1:name tom  
  2. set user:1:age =19  
  3. set user:1:favor football 

正例: 

  1. hmset user:1 name tom age =19 favor football 

控制key的生命周期

redis不是垃圾桶,建議使用expire設(shè)置過期時(shí)間(條件允許可以打散過期時(shí)間,防止集中過期),不過期的數(shù)據(jù)重點(diǎn)關(guān)注idletime。

二、命令使用

1、O(N)命令關(guān)注N的數(shù)量

例如hgetall、lrange、smembers、zrange、sinter等并非不能使用,但是需要明確N的值。有遍歷的需求可以使用hscan、sscan、zscan代替。

2、禁用命令

禁止線上使用keys、flushall、flushdb等,通過redis的rename機(jī)制禁掉命令,或者使用scan的方式漸進(jìn)式處理。

3、合理使用select

redis的多數(shù)據(jù)庫較弱,使用數(shù)字進(jìn)行區(qū)分,很多客戶端支持較差,同時(shí)多業(yè)務(wù)用多數(shù)據(jù)庫實(shí)際還是單線程處理,會(huì)有干擾。

4、使用批量操作提高效率

原生命令:例如mget、mset。

非原生命令:可以使用pipeline提高效率。

但要注意控制一次批量操作的元素個(gè)數(shù)(例如500以內(nèi),實(shí)際也和元素字節(jié)數(shù)有關(guān))。

注意兩者不同:

  •  原生是原子操作,pipeline是非原子操作。
  •  pipeline可以打包不同的命令,原生做不到
  •  pipeline需要客戶端和服務(wù)端同時(shí)支持。

5、不建議過多使用Redis事務(wù)功能

Redis的事務(wù)功能較弱(不支持回滾),而且集群版本(自研和官方)要求一次事務(wù)操作的key必須在一個(gè)slot上(可以使用hashtag功能解決)

6、Redis集群版本在使用Lua上有特殊要求

1、所有key都應(yīng)該由 KEYS 數(shù)組來傳遞,redis.call/pcall 里面調(diào)用的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 arrayrn" 2、所有key,必須在1個(gè)slot上,否則直接返回error, "-ERR eval/evalsha command keys must in same slotrn"

7、monitor命令

必要情況下使用monitor命令時(shí),要注意不要長(zhǎng)時(shí)間使用。

三、客戶端使用

1、避免多個(gè)應(yīng)用使用一個(gè)Redis實(shí)例

不相干的業(yè)務(wù)拆分,公共數(shù)據(jù)做服務(wù)化。

2、使用連接池

可以有效控制連接,同時(shí)提高效率,標(biāo)準(zhǔn)使用方式:

執(zhí)行命令如下: 

  1. Jedis jedis = null 
  2. try {  
  3.     jedis = jedisPool.getResource();  
  4.  //具體的命令  
  5.     jedis.executeCommand()  
  6. } catch (Exception e) {  
  7.     logger.error("op key {} error: " + e.getMessage(), key, e);  
  8. } finally {  
  9.  //注意這里不是關(guān)閉連接,在JedisPool模式下,Jedis會(huì)被歸還給資源池。  
  10.  if (jedis != null)  
  11.         jedis.close();  

3、熔斷功能

高并發(fā)下建議客戶端添加熔斷功能(例如netflix hystrix)

4、合理的加密

設(shè)置合理的密碼,如有必要可以使用SSL加密訪問(阿里云Redis支持)

5、淘汰策略

根據(jù)自身業(yè)務(wù)類型,選好maxmemory-policy(最大內(nèi)存淘汰策略),設(shè)置好過期時(shí)間。

默認(rèn)策略是volatile-lru,即超過最大內(nèi)存后,在過期鍵中使用lru算法進(jìn)行key的剔除,保證不過期數(shù)據(jù)不被刪除,但是可能會(huì)出現(xiàn)OOM問題。

其他策略如下:

  •  allkeys-lru:根據(jù)LRU算法刪除鍵,不管數(shù)據(jù)有沒有設(shè)置超時(shí)屬性,直到騰出足夠空間為止。
  •  allkeys-random:隨機(jī)刪除所有鍵,直到騰出足夠空間為止。
  •  volatile-random:隨機(jī)刪除過期鍵,直到騰出足夠空間為止。
  •  volatile-ttl:根據(jù)鍵值對(duì)象的ttl屬性,刪除最近將要過期數(shù)據(jù)。如果沒有,回退到noeviction策略。
  •   noeviction:不會(huì)剔除任何數(shù)據(jù),拒絕所有寫入操作并返回客戶端錯(cuò)誤信息"(error) OOM command not allowed when used memory",此時(shí)Redis只響應(yīng)讀操作。

四、相關(guān)工具

1、數(shù)據(jù)同步

redis間數(shù)據(jù)同步可以使用:redis-port

2、big key搜索

redis大key搜索工具

3、熱點(diǎn)key尋找

內(nèi)部實(shí)現(xiàn)使用monitor,所以建議短時(shí)間使用facebook的redis-faina 阿里云Redis已經(jīng)在內(nèi)核層面解決熱點(diǎn)key問題

五、刪除bigkey

下面操作可以使用pipeline加速。redis 4.0已經(jīng)支持key的異步刪除,歡迎使用。

1、Hash刪除: hscan + hdel 

  1. public void delBigHash(String host, int port, String password, String bigHashKey) {  
  2.  Jedis jedis = new Jedis(host, port);  
  3.  if (password != null && !"".equals(password)) {  
  4.         jedis.auth(password);  
  5.     }  
  6.  ScanParams scanParams = new ScanParams().count(100);  
  7.  String cursor = "0" 
  8.  do {  
  9.  ScanResult<Entry<String, String>> scanResult = jedis.hscan(bigHashKey, cursor, scanParams);  
  10.  List<Entry<String, String>> entryList = scanResult.getResult();  
  11.  if (entryList != null && !entryList.isEmpty()) {  
  12.  for (Entry<String, String> entry : entryList) {  
  13.                 jedis.hdel(bigHashKey, entry.getKey()); 
  14.              }  
  15.         }  
  16.         cursor = scanResult.getStringCursor();  
  17.     } while (!"0".equals(cursor));   
  18.  //刪除bigkey  
  19.     jedis.del(bigHashKey);  

2、List刪除: ltrim 

  1. public void delBigList(String host, int port, String password, String bigListKey) {  
  2.  Jedis jedis = new Jedis(host, port); 
  3.   if (password != null && !"".equals(password)) {  
  4.         jedis.auth(password);  
  5.     }  
  6.  long llen = jedis.llen(bigListKey);  
  7.  int counter = 0
  8.   int left = 100 
  9.  while (counter < llen) {  
  10.  //每次從左側(cè)截掉100個(gè)  
  11.         jedis.ltrim(bigListKey, left, llen);  
  12.         counter += left;  
  13.     }  
  14.  //最終刪除key  
  15.     jedis.del(bigListKey);  

3、Set刪除: sscan + srem 

  1. public void delBigSet(String host, int port, String password, String bigSetKey) {  
  2.  Jedis jedis = new Jedis(host, port);  
  3.  if (password != null && !"".equals(password)) {  
  4.         jedis.auth(password);  
  5.     }  
  6.  ScanParams scanParams = new ScanParams().count(100);  
  7.  String cursor = "0" 
  8.  do {  
  9.  ScanResult<String> scanResult = jedis.sscan(bigSetKey, cursor, scanParams);  
  10.  List<String> memberList = scanResult.getResult();  
  11.  if (memberList != null && !memberList.isEmpty()) {  
  12.  for (String member : memberList) {  
  13.                 jedis.srem(bigSetKey, member); 
  14.              }  
  15.         }  
  16.         cursor = scanResult.getStringCursor();  
  17.     } while (!"0".equals(cursor));  
  18.  //刪除bigkey  
  19.     jedis.del(bigSetKey);  

4、SortedSet刪除: zscan + zrem 

  1. public void delBigZset(String host, int port, String password, String bigZsetKey) {   
  2.  Jedis jedis = new Jedis(host, port);  
  3.  if (password != null && !"".equals(password)) {  
  4.         jedis.auth(password);  
  5.     }  
  6.  ScanParams scanParams = new ScanParams().count(100);  
  7.  String cursor = "0" 
  8.  do {  
  9.  ScanResult<Tuple> scanResult = jedis.zscan(bigZsetKey, cursor, scanParams);  
  10.  List<Tuple> tupleList = scanResult.getResult();  
  11.  if (tupleList != null && !tupleList.isEmpty()) {  
  12.  for (Tuple tuple : tupleList) {  
  13.                 jedis.zrem(bigZsetKey, tuple.getElement());  
  14.             }  
  15.         }  
  16.         cursor = scanResult.getStringCursor();  
  17.     } while (!"0".equals(cursor));  
  18.  //刪除bigkey  
  19.     jedis.del(bigZsetKey);  
  20.  

 

責(zé)任編輯:龐桂玉 來源: 民工哥技術(shù)之路
相關(guān)推薦

2010-06-28 10:43:47

2017-05-02 21:14:20

阿里巴巴Java開發(fā)

2024-07-10 18:43:42

2013-08-22 09:41:52

阿里巴巴去IOE王堅(jiān)

2023-03-29 09:42:32

2013-08-22 09:36:45

阿里巴巴王堅(jiān)阿里云

2019-08-15 10:25:02

代碼開發(fā)工具

2009-02-27 10:46:32

DBA筆試題阿里巴巴

2014-12-31 10:48:40

阿里巴巴馬云

2024-04-25 09:14:57

數(shù)據(jù)庫Mysql阿里巴巴

2010-08-25 14:33:59

抄底

2014-06-11 10:04:05

UC優(yōu)視阿里巴巴

2015-04-23 15:30:08

2018-10-16 15:34:17

阿里巴巴Apache Flin大數(shù)據(jù)

2015-05-12 15:09:01

阿里巴巴公有云IaaS

2015-06-12 10:12:30

2013-06-02 21:53:51

阿里巴巴Windows Azu淘寶

2022-08-22 08:07:45

DruidMySQL密碼

2009-06-30 13:28:54

阿里巴巴旺旺
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)