Redis做為單機緩存使用建議 前言 由於原來項目使用的緩存中間件無法在國產麒麟操作系統裡面使用,準備在項目中引入redis做為單機緩存。 配置優化建議 配置redis服務以守護進程啟動 Redis預設不是以守護進程的方式運行,可以通過將配置項daemonize修改為yes,這樣啟動redis-se ...
Redis做為單機緩存使用建議
前言
由於原來項目使用的緩存中間件無法在國產麒麟操作系統裡面使用,準備在項目中引入redis做為單機緩存。
配置優化建議
配置redis服務以守護進程啟動
Redis預設不是以守護進程的方式運行,可以通過將配置項daemonize修改為yes,這樣啟動redis-server後會自動在後臺運行。
安全配置
將bind配置為127.0.0.1可以避免redis受外部攻擊。另外使用requirepass配置項,可以設置訪問redis伺服器數據時先要輸入密碼。
一個小遺憾是redis只支持在配置文件中使用明文保存訪問密碼,這裡提供一個動態生成配置文件的思路增加安全性:
首先將redis.conf備份成redis.conf.tml,在裡面的requirepass配置好密碼密文。
在啟動redis服務前使用其它程式讀取redis.conf.tml的requirepass配置項,把密文解密,替換requirepass值生成redis配置文件redis.conf.,啟動redis服務後把redis.conf刪除,這樣就達到保密效果。
設置最大記憶體及記憶體淘汰策略
為避免redis占用記憶體無限膨脹,導致把系統記憶體耗盡,建議將maxmemory設置為1024mb。(實際用ps命令查看,會發現redis-server最多會使用比maxmemory多一些的記憶體)
同時配置記憶體淘汰策略maxmemory-policy為allkeys-lru,讓redis在記憶體滿時在所有的key中使用LRU演算法對數據進行淘汰。
日誌文件配置
Redis提供了logfile配置項,可以指定日誌輸出位置。但預設情況下redis會把所有日誌輸出到同一個文件,天常日久,這個日誌文件會越積越大。
建議修改redis的源碼,把裡面的redisLog函數改為按天輸出日誌。
持久化配置
由於我們只需要使用redis做資料庫緩存,所以不需要持久化。也不需要擔心redis重啟出現“緩存雪崩”的現象,因為我們業務伺服器有很多台,不會同時重啟。
單台業務伺服器tps有限,緩存清零對資料庫也不會產生太大壓力。
關閉持久化方法把原來的save配置屏蔽,增加save ""
慢查詢配置
設置slowlog-log-slower-than 5000,把所有響應時間大於5ms的請求記錄起來,方便出故障時定位問題。
高風險命令配置
有一些redis命令會消耗redis伺服器比較多資源,導致查詢緩存效率下降。為了防止新手誤操作,我們可以把這些命令改名,配置如下:
rename-command MONITOR "DANGEROUS_CMD_MONITOR"
rename-command FLUSHALL "DANGEROUS_CMD_FLUSHALL"
rename-command FLUSHDB "DANGEROUS_CMD_FLUSHDB"
rename-command CONFIG "DANGEROUS_CMD_CONFIG"
rename-command KEYS "DANGEROUS_CMD_KEYS"
優化前後性能測試比較
優化前:
[huangcihui:/home/huangcihui] redis-benchmark -h 127.0.0.1 -p 6379 -c 100 -n 100000 -k 1 -e -q -r 10000 -d 512 PING_INLINE: 56148.23 requests per second PING_BULK: 55617.35 requests per second SET: 57570.52 requests per second GET: 56085.25 requests per second INCR: 55309.73 requests per second LPUSH: 54764.51 requests per second RPUSH: 57570.52 requests per second LPOP: 54644.81 requests per second RPOP: 54884.74 requests per second SADD: 50327.12 requests per second HSET: 58445.36 requests per second SPOP: 53191.49 requests per second LPUSH (needed to benchmark LRANGE): 54945.05 requests per second LRANGE_100 (first 100 elements): 11693.17 requests per second LRANGE_300 (first 300 elements): 3824.09 requests per second LRANGE_500 (first 450 elements): 2342.19 requests per second LRANGE_600 (first 600 elements): 1671.12 requests per second MSET (10 keys): 40192.93 requests per second [huangcihui:/home/huangcihui] redis-benchmark -h 127.0.0.1 -p 6379 -c 100 -n 100000 -k 1 -e -q -r 10000 -d 512 PING_INLINE: 55340.34 requests per second PING_BULK: 54854.64 requests per second SET: 53937.43 requests per second GET: 54347.82 requests per second INCR: 52910.05 requests per second LPUSH: 54674.69 requests per second RPUSH: 51894.13 requests per second LPOP: 53676.86 requests per second RPOP: 53022.27 requests per second SADD: 53676.86 requests per second HSET: 55401.66 requests per second SPOP: 56085.25 requests per second LPUSH (needed to benchmark LRANGE): 54347.82 requests per second LRANGE_100 (first 100 elements): 11160.71 requests per second LRANGE_300 (first 300 elements): 3383.98 requests per second LRANGE_500 (first 450 elements): 2246.33 requests per second LRANGE_600 (first 600 elements): 1592.66 requests per second MSET (10 keys): 37622.27 requests per second [huangcihui:/home/huangcihui] redis-benchmark -h 127.0.0.1 -p 6379 -c 100 -n 100000 -k 1 -e -q -r 10000 -d 512 PING_INLINE: 54734.54 requests per second PING_BULK: 54024.85 requests per second SET: 54854.64 requests per second GET: 52798.31 requests per second INCR: 55463.12 requests per second LPUSH: 55432.37 requests per second RPUSH: 55834.73 requests per second LPOP: 54495.91 requests per second RPOP: 53705.69 requests per second SADD: 52521.01 requests per second HSET: 54229.93 requests per second SPOP: 54585.15 requests per second LPUSH (needed to benchmark LRANGE): 55648.30 requests per second LRANGE_100 (first 100 elements): 11225.86 requests per second LRANGE_300 (first 300 elements): 3598.29 requests per second LRANGE_500 (first 450 elements): 2222.77 requests per second LRANGE_600 (first 600 elements): 1620.25 requests per second MSET (10 keys): 38684.72 requests per second
優化後:
[huangcihui:/home/huangcihui] redis-benchmark -h 127.0.0.1 -p 6379 -c 100 -n 100000 -k 1 -e -q -r 10000 -d 512 -a pass123 PING_INLINE: 51361.07 requests per second PING_BULK: 46838.41 requests per second SET: 49043.65 requests per second GET: 50150.45 requests per second INCR: 51786.64 requests per second LPUSH: 55493.89 requests per second RPUSH: 50150.45 requests per second LPOP: 56915.20 requests per second RPOP: 55928.41 requests per second SADD: 56369.79 requests per second HSET: 58651.02 requests per second SPOP: 57703.40 requests per second LPUSH (needed to benchmark LRANGE): 56593.10 requests per second LRANGE_100 (first 100 elements): 11723.33 requests per second LRANGE_300 (first 300 elements): 3954.76 requests per second LRANGE_500 (first 450 elements): 2504.95 requests per second LRANGE_600 (first 600 elements): 1733.61 requests per second MSET (10 keys): 44444.45 requests per second [huangcihui:/home/huangcihui] redis-benchmark -h 127.0.0.1 -p 6379 -c 100 -n 100000 -k 1 -e -q -r 10000 -d 512 -a pass123 PING_INLINE: 57339.45 requests per second PING_BULK: 56561.09 requests per second SET: 56116.72 requests per second GET: 56625.14 requests per second INCR: 57142.86 requests per second LPUSH: 59880.24 requests per second RPUSH: 51387.46 requests per second LPOP: 51599.59 requests per second RPOP: 51334.70 requests per second SADD: 55865.92 requests per second HSET: 57937.43 requests per second SPOP: 58719.91 requests per second LPUSH (needed to benchmark LRANGE): 56625.14 requests per second LRANGE_100 (first 100 elements): 11845.53 requests per second LRANGE_300 (first 300 elements): 3999.20 requests per second LRANGE_500 (first 450 elements): 2414.70 requests per second LRANGE_600 (first 600 elements): 1702.16 requests per second MSET (10 keys): 39494.47 requests per second [huangcihui:/home/huangcihui] redis-benchmark -h 127.0.0.1 -p 6379 -c 100 -n 100000 -k 1 -e -q -r 10000 -d 512 -a pass123 PING_INLINE: 51786.64 requests per second PING_BULK: 38417.21 requests per second SET: 55524.71 requests per second GET: 39047.25 requests per second INCR: 44822.95 requests per second LPUSH: 53276.50 requests per second RPUSH: 58582.31 requests per second LPOP: 57208.24 requests per second RPOP: 55066.08 requests per second SADD: 52910.05 requests per second HSET: 55187.64 requests per second SPOP: 57405.28 requests per second LPUSH (needed to benchmark LRANGE): 57570.52 requests per second LRANGE_100 (first 100 elements): 10960.11 requests per second LRANGE_300 (first 300 elements): 3794.20 requests per second LRANGE_500 (first 450 elements): 2355.44 requests per second LRANGE_600 (first 600 elements): 1705.41 requests per second MSET (10 keys): 44130.62 requests per second
經優化後運行效率會有小提升
使用約定
集群
Redis提供三種集群模式,分別是主從,哨兵,分片三種。但因為我們只打算做為單機緩存,所以不需要配置。
合理使用資料庫和鍵名首碼區分業務
Redis提供了多資料庫配置,最多支持256個資料庫。我們可以規定不同業務模塊使用不同的資料庫,這樣可以避免資料庫主鍵名稱衝突。
但即使同一業務模塊,也經常容易出現主鍵名稱相同的情況,所以鍵名需要制定一些規範:統一使用”首碼:” + 具體值。
例如set uname:13560453764 huangcihui
首碼可以使用excel管理起來,這樣基本可以解決鍵名衝突問題。
失效時間
最好對所有鍵設置失效時間,失效時間最好是某個範圍內的隨機數,這樣可以避免緩存同時失效的情況。
壓測
部署redis前最好先執行壓測命令,看一下性能是否有異常。如果有異常需要考慮是否調整操作系統參數。
redis-benchmark -h 127.0.0.1 -p 6379 -c 100 -n 100000 -k 1 -e -a pass123 -q
註意需要使用-k參數,設置長連接,不然測試結果性能會差很多。(我的機器上測試相差5倍)