Redis优化系列:连接数优化
原创
©著作权归作者所有:来自51CTO博客作者passedbylove的原创作品,请联系作者获取转载授权,否则将追究法律责任
笔者最近在使用Protostuff优化redis缓存部分,为了前后对比使用内存变化,于是使用期了命令。
1、登录服务器
/usr/bin/redis-cli -p 6379
2、查看内存
其中used_memory是使用的内存,used_memory_human是便于阅读的数值(human参数类似于linux的h参数,方便人机阅读)。
当然用info命令也可以查看所有参数
127.0.0.1:9543> info
# Server
redis_version:3.2.12
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:7897e7d0e13773f
redis_mode:standalone
os:Linux 4.19.0-6.el7.ucloud.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.8.5
process_id:53065
run_id:9647503ba308d70c662154fd1f874fca2d7f861e
tcp_port:9543
uptime_in_seconds:4241299
uptime_in_days:49
hz:10
lru_clock:16335376
executable:/usr/bin/redis-server
config_file:/etc/redis.conf
# Clients
connected_clients:51
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0
# Memory
used_memory:2552592
used_memory_human:2.43M
used_memory_rss:9994240
used_memory_rss_human:9.53M
used_memory_peak:2957584
used_memory_peak_human:2.82M
total_system_memory:1919422464
total_system_memory_human:1.79G
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
mem_fragmentation_ratio:3.92
mem_allocator:jemalloc-3.6.0
# Persistence
loading:0
rdb_changes_since_last_save:6
rdb_bgsave_in_progress:0
rdb_last_save_time:1610170433
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:0
rdb_current_bgsave_time_sec:-1
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
# Stats
total_connections_received:1156
total_commands_processed:44870
instantaneous_ops_per_sec:0
total_net_input_bytes:28006092
total_net_output_bytes:134623051
instantaneous_input_kbps:0.00
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:3
evicted_keys:0
keyspace_hits:11414
keyspace_misses:1111
pubsub_channels:0
pubsub_patterns:1
latest_fork_usec:606
migrate_cached_sockets:0
# Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
# CPU
used_cpu_sys:2169.39
used_cpu_user:1198.49
used_cpu_sys_children:0.28
used_cpu_user_children:0.33
# Cluster
cluster_enabled:0
# Keyspace
db0:keys=52,expires=0,avg_ttl=0
就在笔者使用这个命令列举运行时状态信息后,发现connected_clients参数非常大,这台redis是外网的服务器。发现51个连接。
使用同样程序在内网跑,连接数只有15个,相差较多。按照网上进行优化
1、查看连接超时(0代表不超时)
2、设置超时参数
永久生效办法,修改配置文件
vim /etc/redis.conf
修改timeout为1000
再次查询只有3个连接
参考:Redis连接的客户端(connected_clients)数过高或者不减的问题解决方案