redis出現瓶頸的問題,現在把排查的一些經驗記錄下來備查,本篇只是思路的整理,不涉及具體的使用。
大體的思路如下:
1.通過slow log查看
參考 http://www.cnblogs.com/onmyway20xx/p/5486604.html
查看下是否有較為明顯的慢查詢?一般認為出現慢查詢的話,redis性能瓶頸已經比較明顯了
2. 通過info 查看;
info里面的信息比較多,通常關注以下幾塊
# Memory
used_memory_human:795.13K #redis現在占用的內存,有可能包括SWAP虛擬內存。
used_memory_rss:18259968 #系統給redis分配的內存
used_memory_peak_human:9.51M #Redis所用內存的峰值
mem_fragmentation_ratio:22.43 #used_memory_rss/used_memory ,當mem_fragmentation_ratio <1 時,說明used_memory > used_memory_rss,
這時Redis已經在使用SWAP,運行性能會受很大影響。
3. 通過benchmark測試下當前服務器的性能;
4. 通過MONITOR測算一次請求對redis操作的次數;
1. INFO
info指令返回服務器相關信息,包括:
-
server: General information about the Redis server
-
clients: Client connections section
-
memory: Memory consumption related information
-
persistence: RDB and AOF related information
-
stats: General statistics
-
replication: Master/slave replication information
-
cpu: CPU consumption statistics
-
commandstats: Redis command statistics
-
cluster: Redis Cluster section
-
keyspace: Database related statistics
其本身支持定制返回列表:
[root@~]# redis-cli info [root@~]# redis-cli info default [root@~]# redis-cli info all
詳情請見: http://www.redis.cn/commands/info.html
2. MONITOR
MONITOR是一個調試命令,返回服務器處理的每一個命令,它能幫助我們了解在數據庫上發生了什么操作。共有3種操作方法:
[root@~]# redis-cli monitor OK 1417532512.619715 [0 127.0.0.1:55043] "REPLCONF" "ACK" "6623624" [root@~]# telnet 127.0.0.1 6379 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. monitor +OK +1417532567.733458 [0 127.0.0.1:55043] "REPLCONF" "ACK" "6623708" +1417532568.735936 [0 127.0.0.1:55043] "REPLCONF" "ACK" "6623708" quit +OK Connection closed by foreign host. [root@~]# redis-cli 127.0.0.1:6379> monitor OK 1417532590.785487 [0 127.0.0.1:55043] "REPLCONF" "ACK" "6623736"
由於MONITOR命令返回服務器處理的所有的命令, 所以在性能上會有一些消耗。使用官方的壓測工具測試結果如下
在不運行MONITOR命令的情況下,benchmark的測試結果:
[root@~/software/redis-2.8.17]# src/redis-benchmark -c 10 -n 100000 -q PING_INLINE: 51020.41 requests per second PING_BULK: 50607.29 requests per second SET: 37257.82 requests per second GET: 49800.80 requests per second INCR: 38699.69 requests per second LPUSH: 38910.51 requests per second LPOP: 39277.30 requests per second SADD: 54614.96 requests per second SPOP: 51948.05 requests per second LPUSH (needed to benchmark LRANGE): 38819.88 requests per second LRANGE_100 (first 100 elements): 20112.63 requests per second LRANGE_300 (first 300 elements): 9025.27 requests per second LRANGE_500 (first 450 elements): 6836.67 requests per second LRANGE_600 (first 600 elements): 5406.28 requests per second MSET (10 keys): 19394.88 requests per second
在運行MONITOR命令的情況下,benchmark的測試結果: (redis-cli monitor > /dev/null):
[root@~/software/redis-2.8.17]# src/redis-benchmark -c 10 -n 100000 -q PING_INLINE: 42211.91 requests per second PING_BULK: 42936.88 requests per second SET: 26143.79 requests per second GET: 33990.48 requests per second INCR: 26553.37 requests per second LPUSH: 27337.34 requests per second LPOP: 27225.70 requests per second SADD: 30459.95 requests per second SPOP: 39494.47 requests per second LPUSH (needed to benchmark LRANGE): 26315.79 requests per second LRANGE_100 (first 100 elements): 22055.58 requests per second LRANGE_300 (first 300 elements): 8104.38 requests per second LRANGE_500 (first 450 elements): 6371.05 requests per second LRANGE_600 (first 600 elements): 5031.95 requests per second MSET (10 keys): 14861.05 requests per second
可以看到各項指標基本都有所下降。
詳情請見: http://www.redis.cn/commands/monitor.html
3. SLOWLOG
通過SLOWLOG可以讀取慢查詢日志。
使用SLOWLOG LEN就可以獲取當前慢日志長度。
[root@~/software/redis-2.8.17]# redis-cli 127.0.0.1:6379> slowlog len (integer) 28
使用SLOWLOG GET就可以獲取所有慢日志。
127.0.0.1:6379> slowlog get 1) 1) (integer) 27 2) (integer) 1417531320 3) (integer) 24623 4) 1) "info"
其中,各項指標表示:
-
A unique progressive identifier for every slow log entry.
-
The unix timestamp at which the logged command was processed.
-
The amount of time needed for its execution, in microseconds(注意,microseconds翻譯成微秒,而不是毫秒).
-
The array composing the arguments of the command.
使用SLOWLOG GET N就可以獲取最近N條慢日志。
127.0.0.1:6379> slowlog get 2 1) 1) (integer) 27 2) (integer) 1417531320 3) (integer) 24623 4) 1) "info" 2) 1) (integer) 26 2) (integer) 1417528379 3) (integer) 21363 4) 1) "get" 2) "user:score"
使用SLOWLOG RESET命令重置慢日志。一旦執行,將丟失以前的所有慢日志。
127.0.0.1:6379> slowlog reset
3. redis延遲時間排查
最近數據量越來越多,並發寫操作很多的情況下,Redis出現響應慢的情況;
可以使用 Redis命令來測試一下redis的響應速度:
redis-cli --latency -h
xxx
-p
xxxx
這條命令會向Redis插入示例數據來檢查平均延時。 Ctrl+C可以隨時結束測試;
下面我們列一下會出現延時的可能:
-
硬件,系統:硬件問題是所有問題最底層的問題了,如果硬件慢,例如CPU主頻低,內存小,磁盤IO慢,這些會讓所有運行在上面的系統響應慢;另外,使用虛擬機會讓系統運行的性能太為下降;當然,有錢的話,這問題很容易解決;系統方面,Linux本身的系統資源調度也會產生一定的延時。這些一般不會很大,可以忽略不計;
-
網絡:如果客戶端和redis在同一台服務器上,使用socket建立連接會比監聽 TCP/IP 端口快很多;
-
Redis命令:一些時間復雜度比較高的命令,如 lrem,sort,sunion等命令會花比較長時間;另外,大量的重復連接也會造成延時,重用連接是一種很好的品質;如果有大量寫操作,可以使用 pipeline 管道的方式(類似mysql事務),一次性提交,這樣數據量也少了,連接次數也少了,不用每次都返回數據,速度自然會快很多;
-
持久化:Redis持久化需要fork出一個進程來進行持久化操作,這本身就會引發延時,如果數據變化大,RDB配置時間短,那這個代價還是挺大的;再加上,硬盤這東西真有點不靠譜,如果還是虛擬機上的虛擬硬盤,如果還是NFS共享目錄,那這延時會讓你崩潰。所以,如果系統不需要持久化,關了吧。
- 相關參考地址:https://blog.csdn.net/li123128/article/details/85531376
- https://blog.csdn.net/xuyunti/article/details/84766936