問題描述
當 Azure Redis 伺服器負載過高的情況下,使用時就會遇見連線超時,命令超時,IO Socket超時等異常。為了能定位是那些因素引起的,可以參考微軟官方文件( 管理 Azure Cache for Redis 的伺服器負載 : https://docs.azure.cn/zh-cn/azure-cache-for-redis/cache-best-practices-server-load#avoid-long-running-commands) 開始以下因素的分析:
- 值大小 : 可以通過Redis-cli.exe 的bigkeys來進行分析 [redis-benchmark -h <your redis name>.redis.cache.chinacloudapi.cn -a [xxxxxxxxxxxxxxxxxx your access key ] -t GET -n 1000000 -d 1024 -P 50 -c 50]
- 避免出現客戶端連線高峰 : 可以檢視Redis Metrics中的連線數
- 記憶體壓力 : 通過檢視Redis Metrics中的記憶體使用量
- 避免長時間執行的命令 : 通過Azure Redis門戶上的console或者是Redis-cli.exe 來執行 slowlog get 100指令
本文就重點演示在Azure Redis門戶上的console頁面執行 slowlog get 100 指令。
問題回答
因為Redis 伺服器是單執行緒系統。 長時間執行的命令(官名:慢指令, Slowlog)可能導致客戶端出現延遲或超時,因為伺服器在忙於處理長時間執行的命令時無法響應任何其他請求。那麼,如何來檢視慢指令呢?
第一步: 登入Azure門戶,選擇需要檢視的Redis服務,進入Console頁面
第二步:在Console命令頁輸入:slowlog get 100
- slowlog get 100: 獲取當前Redis服務中所記錄的慢指令資訊,每一個指令包含六部分內容,id,unix時間,指令執行時間(ms 微秒), 指令內容,客戶端IP:埠,客戶端機器名。
- slowlog len : 返回當前Redis伺服器中所記錄的慢指令日誌中所記錄指令數
- slowlog reset :清空當前慢指令記錄
指令輸出示例:
Welcome to secure redis console! This console connects to your live redis server and all commands are run on the server. WARNING: Use expensive commands with caution as they can impact your server load! >slowlog get 100 1) 1) (integer) 16 2) (integer) 1639993957 3) (integer) 10012 4) 1) "GET" 2) "key:__rand_int__" 5) "167.220.233.137:14835" 6) "" 2) 1) (integer) 15 2) (integer) 1639993727 3) (integer) 10195 4) 1) "GET" 2) "key:__rand_int__" 5) "167.220.233.137:19468" 6) "" 3) 1) (integer) 14 2) (integer) 1639992068 3) (integer) 13279 4) 1) "GET" 2) "key:__rand_int__" 5) "167.220.233.137:46504" 6) "" 4) 1) (integer) 13 2) (integer) 1639990412 3) (integer) 14152 4) 1) "GET" 2) "key:__rand_int__" 5) "167.220.233.137:39900" 6) "" 5) 1) (integer) 12 2) (integer) 1639990251 3) (integer) 10710 4) 1) "GET" 2) "key:__rand_int__" 5) "167.220.233.137:52381" 6) "" 6) 1) (integer) 11 2) (integer) 1639989209 3) (integer) 11665 4) 1) "GET" 2) "key:__rand_int__" 5) "167.220.233.137:38779" 6) "" 7) 1) (integer) 10 2) (integer) 1639989142 3) (integer) 13642 4) 1) "GET" 2) "key:__rand_int__" 5) "167.220.233.137:12348" 6) "" 8) 1) (integer) 9 2) (integer) 1639988822 3) (integer) 22073 4) 1) "GET" 2) "key:__rand_int__" 5) "167.220.233.137:51454" 6) "" 9) 1) (integer) 8 2) (integer) 1639988414 3) (integer) 13668 4) 1) "GET" 2) "key:__rand_int__" 5) "167.220.233.137:1550" 6) "" 10) 1) (integer) 7 2) (integer) 1639987239 3) (integer) 18846 4) 1) "GET" 2) "key:__rand_int__" 5) "167.220.233.137:57350" 6) "" 11) 1) (integer) 6 2) (integer) 1639986196 3) (integer) 12921 4) 1) "GET" 2) "key:__rand_int__" 5) "167.220.233.137:1739" 6) "" 12) 1) (integer) 5 2) (integer) 1639986196 3) (integer) 15833 4) 1) "GET" 2) "key:__rand_int__" 5) "167.220.233.137:1739" 6) "" 13) 1) (integer) 4 2) (integer) 1639985175 3) (integer) 14434 4) 1) "GET" 2) "key:__rand_int__" 5) "167.220.233.137:1649" 6) "" 14) 1) (integer) 3 2) (integer) 1639984227 3) (integer) 61332 4) 1) "GET" 2) "key:__rand_int__" 5) "167.220.233.137:24878" 6) "" 15) 1) (integer) 2 2) (integer) 1639984227 3) (integer) 27560 4) 1) "GET" 2) "key:__rand_int__" 5) "167.220.233.137:7637" 6) "" 16) 1) (integer) 1 2) (integer) 1639983695 3) (integer) 10361 4) 1) "SET" 2) "Message_dd2_0" 3) "eyJ0eXAiOiJKV1QiLCJub25jZSI6Inp1d2hMNzhSWDJsdGZITlN1TDYyOEhpRDNrTzhWSnJXZl9CNEtVcjBFTWsiLCJhbGciOiJSUzI1NiIsIng1dCI6IkpSNGxDdzkw... (4140520 more bytes)" 5) "167.220.233.137:1166" 6) "MININT-S4MGVOU" 17) 1) (integer) 0 2) (integer) 1639983310 3) (integer) 16577 4) 1) "SET" 2) "Message_dd_2222" 3) "eyJ0eXAiOiJKV1QiLCJub25jZSI6Inp1d2hMNzhSWDJsdGZITlN1TDYyOEhpRDNrTzhWSnJXZl9CNEtVcjBFTWsiLCJhbGciOiJSUzI1NiIsIng1dCI6IkpSNGxDdzkw... (7774 more bytes)" 5) "167.220.233.137:58313" 6) "MININT-S4MGVOU" >slowlog len (integer) 17 >slowlog reset OK >slowlog len (integer) 0
Each entry from the slow log is comprised of the following six values:
- 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.
- The array composing the arguments of the command.
- Client IP address and port.
- Client name if set via the CLIENT SETNAME command.
參考資料
SLOWLOG GET [count]:https://redis.io/commands/slowlog-get
管理 Azure Cache for Redis 的伺服器負載: https://docs.azure.cn/zh-cn/azure-cache-for-redis/cache-best-practices-server-load#value-sizes