Redis優化要點--讀後總結

壹頁書發表於2016-05-24
http://www.4wei.cn/archives/1002605
http://redis.io/commands/INFO

info命令
# Memory
used_memory:11570933696
used_memory_human:10.78G
used_memory_rss:11895382016
used_memory_peak:12194989280
used_memory_peak_human:11.36G
used_memory_lua:33792
mem_fragmentation_ratio:1.03
mem_allocator:jemalloc-3.2.0

used_memory:
實際快取佔用的記憶體和Redis自身執行所佔用的記憶體(如後設資料、lua)
它是由Redis使用記憶體分配器分配的記憶體,所以這個資料並沒有把記憶體碎片浪費掉的記憶體給統計進去

total number of bytes allocated by Redis using its allocator (either standard libc, jemalloc, or an alternative allocator such as tcmalloc

如果used_memory大於實體記憶體,表明系統使用了swap交換

used_memory_rss:
作業系統上顯示已經分配的記憶體總量, 包括碎片
Number of bytes that Redis allocated as seen by the operating system (a.k.a resident set size). This is the number reported by tools such as top(1) and ps(1)

mem_fragmentation_ratio=used_memory_rss/used_memory
記憶體碎片率稍大於1是合理的,這個值表示記憶體碎片率比較低,也說明redis沒有發生記憶體交換。
但如果記憶體碎片率超過1.5,那就說明Redis消耗了實際需要實體記憶體的150%,其中50%是記憶體碎片率
若是記憶體碎片率低於1的話,說明Redis記憶體分配超出了實體記憶體,作業系統正在進行記憶體交換。記憶體交換會引起非常明顯的響應延遲


重置redis計數器.

CONFIG RESETSTAT

http://redisdoc.com/server/config_resetstat.html

重置 INFO 命令中的某些統計資料,包括:
Keyspace hits (鍵空間命中次數)
Keyspace misses (鍵空間不命中次數)
Number of commands processed (執行命令的次數)
Number of connections received (連線伺服器的次數)
Number of expired keys (過期key的數量)
Number of rejected connections (被拒絕的連線數量)
Latest fork(2) time(最後執行 fork(2) 的時間)
The aof_delayed_fsync counter(aof_delayed_fsync 計數器的值)

Bigkeys
持續取樣,實時輸出當時得到的 value 佔用空間最大的 key
./redis-cli  --bigkeys
# Scanning the entire keyspace to find biggest keys as well as
# average sizes per key type.  You can use -i 0.1 to sleep 0.1 sec
# per 100 SCAN commands (not usually needed).

[00.00%] Biggest string found so far 'A' with 110 bytes
[00.00%] Biggest string found so far 'B' with 111 bytes
[00.00%] Biggest string found so far 'C' with 137 bytes
......
-------- summary -------

Sampled 860830 keys in the keyspace!
Total key length in bytes is 43384761 (avg len 50.40)

Biggest string found 'X' has 194 bytes

860830 strings with 96957492 bytes (100.00% of keys, avg size 112.63)
0 lists with 0 items (00.00% of keys, avg size 0.00)
0 sets with 0 members (00.00% of keys, avg size 0.00)
0 hashs with 0 fields (00.00% of keys, avg size 0.00)
0 zsets with 0 members (00.00% of keys, avg size 0.00) 

命令統計(配合重置計數器)
./redis-cli  info commandstats
# Commandstats
cmdstat_set:calls=78911,usec=558105,usec_per_call=7.07
cmdstat_setex:calls=292806,usec=2172915,usec_per_call=7.42
cmdstat_strlen:calls=860830,usec=1195826,usec_per_call=1.39
cmdstat_del:calls=35035,usec=132577,usec_per_call=3.78
cmdstat_incr:calls=12417,usec=39987,usec_per_call=3.22
cmdstat_lpush:calls=68806,usec=301346,usec_per_call=4.38
cmdstat_ltrim:calls=12024,usec=56763,usec_per_call=4.72
cmdstat_lrem:calls=55246,usec=568687,usec_per_call=10.29
cmdstat_zadd:calls=450,usec=6991,usec_per_call=15.54
cmdstat_zremrangebyscore:calls=93,usec=1153,usec_per_call=12.40
cmdstat_zremrangebyrank:calls=272,usec=2586,usec_per_call=9.51
cmdstat_hset:calls=753,usec=5235,usec_per_call=6.95
cmdstat_hdel:calls=379,usec=2173,usec_per_call=5.73
cmdstat_incrby:calls=5034,usec=18849,usec_per_call=3.74
cmdstat_select:calls=195364,usec=174016,usec_per_call=0.89
cmdstat_expire:calls=13775,usec=41087,usec_per_call=2.98
cmdstat_scan:calls=82681,usec=2272315,usec_per_call=27.48
cmdstat_dbsize:calls=1,usec=2,usec_per_call=2.00
cmdstat_auth:calls=7,usec=14,usec_per_call=2.00
cmdstat_ping:calls=6305,usec=14882,usec_per_call=2.36
cmdstat_type:calls=860830,usec=1246794,usec_per_call=1.45
cmdstat_multi:calls=2524,usec=669,usec_per_call=0.27
cmdstat_exec:calls=2524,usec=5888,usec_per_call=2.33
cmdstat_info:calls=607,usec=44198,usec_per_call=72.81
cmdstat_config:calls=1,usec=20836,usec_per_call=20836.00
cmdstat_subscribe:calls=3,usec=9,usec_per_call=3.00
cmdstat_publish:calls=5748,usec=53970,usec_per_call=9.39
cmdstat_client:calls=3,usec=5,usec_per_call=1.67


除錯.檢視Redis執行的所有命令.
./redis-cli  monitor

記憶體佔用分析
yum install python-pip
pip install rdbtools
rdb -c memory dump.rdb > memory.csv



優化與禁忌
解決儲存快照失敗後redis無法寫入的問題
>config set stop-writes-on-bgsave-error no

定期日誌重寫,減小aof過載時的時間開銷
>auto-aof-rewrite-percentage 100
>auto-aof-rewrite-min-size 64mb

使用scan ... MATCH  .. 代替  keys *

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29254281/viewspace-2105885/,如需轉載,請註明出處,否則將追究法律責任。

相關文章