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> 

原创文章 843 获赞 2097 访问量 427万+

猜你喜欢

转载自blog.csdn.net/yangshangwei/article/details/105759444