Redis進階-無所不知的info命令診斷redis

在這裏插入圖片描述


官方指導

https://redis.io/commands/info

在這裏插入圖片描述

info

在使用 Redis 時,時常會遇到很多問題需要診斷,在診斷之前需要了解 Redis 的運行狀態,通過強大的 Info 指令,你可以清晰地知道 Redis 內部一系列運行參數。

info 指令

Info 指令顯示的信息非常繁多,分爲 9 大塊,每個塊都有非常多的參數 。

  • 1、Server 服務器運行的環境參數
  • 2、Clients 客戶端相關信息
  • 3、Memory 服務器運行內存統計數據
  • 4、Persistence 持久化信息
  • 5、Stats 通用統計數據
  • 6、Replication 主從複製相關信息
  • 7、CPU CPU 使用情況
  • 8、Cluster 集羣信息
  • 9、KeySpace 鍵值對統計數量信息

Info 可以一次性獲取所有的信息,也可以按塊取信息。

# 獲取所有信息
> info
# 獲取內存相關信息
> info memory
# 獲取複製相關信息
> info replication

我們挑幾個常用的說一下

內存佔用多大

在 Memory 塊裏,可以通過 info memory 看到

[redis@artisan bin]$ ./redis-cli  -a artisan -c -h 192.168.18.131 -p 8001  info memory  |grep used |grep human
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
used_memory_human:2.27M  # 內存分配器 (jemalloc) 從操作系統分配的內存總量
used_memory_rss_human:9.87M  # 操作系統看到的內存佔用 ,top 命令看到的內存
used_memory_peak_human:6.18M  # Redis 內存消耗的峯值
used_memory_lua_human:37.00K  # lua 腳本引擎佔用的內存大小
used_memory_scripts_human:0B
[redis@artisan bin]$ 

如果單個 Redis 內存佔用過大,並且在業務上沒有太多壓縮的空間的話,可以考慮集羣化了。


連接了多少客戶端

這個信息在 Clients 塊裏,可以通過 info clients 看到.

[redis@artisan bin]$ ./redis-cli  -a artisan -c -h 192.168.18.131 -p 8001  info clients
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Clients
connected_clients:1   # 這個就是正在連接的客戶端數量
client_recent_max_input_buffer:2
client_recent_max_output_buffer:0
blocked_clients:0
[redis@artisan bin]$ 

這個信息也是比較有用的,通過觀察這個數量可以確定是否存在意料之外的連接。

如果發現這個數量不對勁,接着就可以使用 client list 指令列出所有的客戶端鏈接地址來確定源頭。 如下所示

[redis@artisan bin]$ ./redis-cli  -a artisan -c -h 192.168.18.131 -p 8001  
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.18.131:8001> client list
id=13 addr=192.168.18.133:59659 fd=14 name= age=12542 idle=0 flags=S db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=replconf
id=22 addr=192.168.18.131:55287 fd=17 name= age=4 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=26 qbuf-free=32742 obl=0 oll=0 omem=0 events=r cmd=client
192.168.18.131:8001> 

關於客戶端的數量還有個重要的參數需要觀察,那就是rejected_connections,它表示因爲超出最大連接數限制而被拒絕的客戶端連接次數,如果這個數字很大,意味着服務器的最大連接數設置的過低需要調整 maxclients 參數。

info stats |grep reject

[redis@artisan bin]$ ./redis-cli  -a artisan -c -h 192.168.18.131 -p 8001  info stats |grep reject
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
rejected_connections:0
[redis@artisan bin]$ 


每秒執行多少次指令

這個信息在 Stats 塊裏,可以通過 info stats 看到

info stats |grep ops

[redis@artisan bin]$ ./redis-cli  -a artisan -c -h 192.168.18.131 -p 8001  info stats |grep ops
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
instantaneous_ops_per_sec:1
[redis@artisan bin]$ 

以上,表示 ops 是 1,也就是所有客戶端每秒會發送 1條指令到服務器執行。

極限情況下,Redis 可以每秒執行 10w 次指令,CPU 幾乎完全榨乾。

如果 qps 過高,可以考慮通過 monitor 指令快速觀察一下究竟是哪些 key 訪問比較頻繁,從而在相應的業務上進行優化,以減少 IO 次數。

monitor 指令會瞬間吐出來巨量的指令文本,所以一般在執行monitor 後立即 ctrl+c 中斷輸出。

> redis-cli monitor

複製積壓緩衝區多大

這個信息在 Replication 塊裏,可以通過 info replication 看到。

[redis@artisan bin]$ ./redis-cli  -a artisan -c -h 192.168.18.131 -p 8001  info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.18.133,port=8006,state=online,offset=203138,lag=0
master_replid:ec02aae9722256cae7c508a32ccf1b5ad9e84193
master_replid2:673ed237e9a0c00405b1411f533c6202ff5097b1
master_repl_offset:203138
second_repl_offset:7505
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:203138
[redis@artisan bin]$ 


repl_backlog_size:1048576 # 這個就是積壓緩衝區大小

複製積壓緩衝區大小非常重要,它嚴重影響到主從複製的效率。當從庫因爲網絡原因臨時斷開了主庫的複製,然後網絡恢復了,又重新連上的時候,這段斷開的時間內發生在master 上的修改操作指令都會放在積壓緩衝區中,這樣從庫可以通過積壓緩衝區恢復中斷的主從同步過程。

積壓緩衝區是環形的,後來的指令會覆蓋掉前面的內容。如果從庫斷開的時間過長,或者緩衝區的大小設置的太小,都會導致從庫無法快速恢復中斷的主從同步過程,因爲中間的修改指令被覆蓋掉了。這時候從庫就會進行全量同步模式,非常耗費 CPU 和網絡資源。

如果有多個從庫複製,積壓緩衝區是共享的,它不會因爲從庫過多而線性增長。如果實例的修改指令請求很頻繁,那就把積壓緩衝區調大一些,幾十個 M 大小差不多了,如果很閒,那就設置爲幾個 M。

[redis@artisan bin]$ ./redis-cli  -a artisan -c -h 192.168.18.131 -p 8001  info stats  |grep sync
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
sync_full:0
sync_partial_ok:8
sync_partial_err:0  # 半同步失敗次數
[redis@artisan bin]$ 

通過查看 sync_partial_err 變量的次數來決定是否需要擴大積壓緩衝區,它表示主從半同步複製失敗的次數


info demo

192.168.18.131:8001> info
# Server
redis_version:5.0.3
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:bf47f1531d0f24a7
redis_mode:cluster
os:Linux 3.10.0-123.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:4.8.5
process_id:2193
run_id:f3937ab4fad750de107022bda6bcca601f37db3e
tcp_port:8001
uptime_in_seconds:145012
uptime_in_days:1
hz:10
configured_hz:10
lru_clock:9903968
executable:/home/redis/redis-5.0.3/bin/redis-server
config_file:/home/redis/redis-cluster/8001/redis.conf

# Clients
connected_clients:1
client_recent_max_input_buffer:4
client_recent_max_output_buffer:0
blocked_clients:0

# Memory
used_memory:2376832
used_memory_human:2.27M
used_memory_rss:10981376
used_memory_rss_human:10.47M
used_memory_peak:6475600
used_memory_peak_human:6.18M
used_memory_peak_perc:36.70%
used_memory_overhead:2299862
used_memory_startup:1184376
used_memory_dataset:76970
used_memory_dataset_perc:6.45%
allocator_allocated:2890272
allocator_active:3293184
allocator_resident:9801728
total_system_memory:1027518464
total_system_memory_human:979.92M
used_memory_lua:37888
used_memory_lua_human:37.00K
used_memory_scripts:0
used_memory_scripts_human:0B
number_of_cached_scripts:0
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
allocator_frag_ratio:1.14
allocator_frag_bytes:402912
allocator_rss_ratio:2.98
allocator_rss_bytes:6508544
rss_overhead_ratio:1.12
rss_overhead_bytes:1179648
mem_fragmentation_ratio:4.70
mem_fragmentation_bytes:8646800
mem_not_counted_for_evict:182
mem_replication_backlog:1048576
mem_clients_slaves:16922
mem_clients_normal:49694
mem_aof_buffer:182
mem_allocator:jemalloc-5.1.0
active_defrag_running:0
lazyfree_pending_objects:0

# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1586958875
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:0
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:2363392
aof_enabled:1
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:0
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
aof_last_cow_size:4308992
aof_current_size:1224
aof_base_size:92
aof_pending_rewrite:0
aof_buffer_length:0
aof_rewrite_buffer_length:0
aof_pending_bio_fsync:0
aof_delayed_fsync:0

# Stats
total_connections_received:13
total_commands_processed:137945
instantaneous_ops_per_sec:1
total_net_input_bytes:5299462
total_net_output_bytes:423203
instantaneous_input_kbps:0.05
instantaneous_output_kbps:6.87
rejected_connections:0
sync_full:0
sync_partial_ok:8
sync_partial_err:0
expired_keys:0
expired_stale_perc:0.00
expired_time_cap_reached_count:0
evicted_keys:0
keyspace_hits:8
keyspace_misses:1
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:1326
migrate_cached_sockets:0
slave_expires_tracked_keys:0
active_defrag_hits:0
active_defrag_misses:0
active_defrag_key_hits:0
active_defrag_key_misses:0

# Replication
role:master
connected_slaves:1
slave0:ip=192.168.18.133,port=8006,state=online,offset=200940,lag=1
master_replid:ec02aae9722256cae7c508a32ccf1b5ad9e84193
master_replid2:673ed237e9a0c00405b1411f533c6202ff5097b1
master_repl_offset:200940
second_repl_offset:7505
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:200940

# CPU
used_cpu_sys:661.392933
used_cpu_user:139.363941
used_cpu_sys_children:0.925828
used_cpu_user_children:0.007012

# Cluster
cluster_enabled:1

# Keyspace
db0:keys=2,expires=0,avg_ttl=0
192.168.18.131:8001> 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章