Redis做為單機緩存使用建議


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倍)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM