Redis CPU高负载案例分析
redis info 信息
# Server
redis_version:5.0.14
redis_git_sha1:d0da6741
redis_git_dirty:1
redis_build_id:170c004c23eab494
redis_mode:standalone
os:Linux
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:0.0.0
process_id:30692
run_id:743bc236c6b8d47cfbe01c8b86db2fe367810a70
tcp_port:6379
uptime_in_seconds:1027891
uptime_in_days:11
hz:10
configured_hz:10
lru_clock:15353649
executable:
config_file:
support_ptod:1
io_threads_active:0# Clients
connected_clients:1937
client_recent_max_input_buffer:82467
client_recent_max_output_buffer:1163648
blocked_clients:1
tracking_clients:0
pubsub_clients:987
watching_clients:0
clients_in_timeout_table:1
total_watched_keys:0
total_blocking_keys:1# Memory
used_memory:8513245840
used_memory_human:7.93G
used_memory_rss:8724193280
used_memory_rss_human:8.13G
used_memory_peak:10476231856
used_memory_peak_human:9.76G
used_memory_peak_perc:81.26%
used_memory_overhead:503302644
used_memory_startup:9284352
used_memory_dataset:8009943196
used_memory_dataset_perc:94.19%
allocator_allocated:8513868696
allocator_active:8610136064
allocator_resident:8818167808
used_memory_lua:44032
used_memory_lua_human:43.00K
used_memory_scripts:2208
used_memory_scripts_human:2.16K
number_of_cached_scripts:8
maxmemory:34359738368
maxmemory_human:32.00G
maxmemory_policy:volatile-lru
allocator_frag_ratio:1.01
allocator_frag_bytes:96267368
allocator_rss_ratio:1.02
allocator_rss_bytes:208031744
rss_overhead_ratio:0.99
rss_overhead_bytes:-93974528
mem_fragmentation_ratio:1.02
mem_fragmentation_bytes:211075240
mem_not_counted_for_evict:386151482
mem_replication_backlog:33554432
mem_clients_slaves:20512
mem_clients_normal:40085260
mem_aof_buffer:386153352
mem_allocator:jemalloc-5.1.0
mem_overhead_db_hashtable_rehashing:0
active_defrag_running:0
lazyfree_pending_objects:0
mem_overhead_hashtable_main:30647776
mem_overhead_hashtable_expires:3554752
oom_err_count:0# Stats
total_connections_received:5950271
total_commands_processed:71869259985
instantaneous_ops_per_sec:71028
instantaneous_write_ops_per_sec:43342
instantaneous_read_ops_per_sec:15977
instantaneous_other_ops_per_sec:11709
total_net_input_bytes:18466498214489
total_net_output_bytes:132415679782255
total_net_repl_input_bytes:5606203743
total_net_repl_output_bytes:17383770166575
instantaneous_input_kbps:18356.90
instantaneous_output_kbps:124500.94
instantaneous_input_repl_kbps:0.00
instantaneous_output_repl_kbps:17270.56
rejected_connections:0
sync_full:3
sync_partial_ok:0
sync_partial_err:1
expired_keys:17505442
expired_stale_perc:1.13
expired_time_cap_reached_count:0
expire_cycle_cpu_milliseconds:678198
evicted_keys:0
keyspace_hits:13986441669
keyspace_misses:1694424826
pubsub_channels:389
pubsub_patterns:4
latest_fork_usec:916
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
tracking_total_keys:0
tracking_total_items:0
tracking_total_prefixes:0
unexpected_error_replies:0
total_error_replies:1211787
instantaneous_error_replies_ops_per_sec:0
total_reads_processed:20746321954
total_writes_processed:68548565368
io_threaded_reads_processed:0
io_threaded_writes_processed:0
client_query_buffer_limit_disconnections:0
client_output_buffer_limit_disconnections:0
slot_psync_ok:0
slot_psync_err:0
total_commands_received:71789530051
commands_received_per_sec:70947
evicted_keys_per_sec:0
hits_per_sec:14490
misses_per_sec:1487
hit_rate_percentage:90.69
cmd_slowlog_count:38
traffic_control_input:0
traffic_control_input_status:0
traffic_control_output:25815196780
traffic_control_output_status:1
stat_avg_rt:2
stat_max_rt:4093# Replication
role:master# CPU
used_cpu_sys:396997.838678
used_cpu_user:402853.038219
used_cpu_sys_children:25034.417695
used_cpu_user_children:126502.980377# Errorstats
errorstat_ERR:count=1208188
errorstat_LOADING:count=56
errorstat_NOAUTH:count=42
errorstat_NOMASTERLINK:count=75
errorstat_NOPERM:count=3426# Cluster
cluster_enabled:0
databases:256
nodecount:1# Keyspace
db0:keys=1289,expires=486,avg_ttl=43357629
db3:keys=109537,expires=6389,avg_ttl=1671029
db8:keys=4,expires=4,avg_ttl=15423106774
db9:keys=1,expires=0,avg_ttl=0
db10:keys=392013,expires=74700,avg_ttl=203625475
db11:keys=5629,expires=184,avg_ttl=77967
db12:keys=1978,expires=0,avg_ttl=0
db13:keys=14037,expires=14037,avg_ttl=121470782
redis分析
🧩 一、基础信息(# Server)
项目 | 结果 | 分析 |
---|---|---|
Redis 版本 | 5.0.14 | 较稳定版本,但无多线程 I/O,CPU 单核性能很关键 |
模式 | standalone | 单机模式,无主从/集群 |
运行时间 | 11天 | 稳定运行中,无重启迹象 |
I/O 多路复用 | epoll | 正常,Linux 默认最佳性能选择 |
IO 线程数 | 0 | 说明未启用多线程(Redis 6+ 才支持) |
✅ 总体结论:
Redis 运行稳定,版本正常。单线程模式意味着 CPU 单核会是性能瓶颈来源之一。
👥 二、客户端信息(# Clients)
指标 | 值 | 说明 |
---|---|---|
connected_clients | 1937 当前连接非常多 | (默认 maxclients = 10,000),负载较高 |
pubsub_clients | 987 约一半连接为 | Pub/Sub 订阅连接 |
blocked_clients | 1 偶尔出现阻塞请求 | (例如 BLPOP 等) |
client_recent_max_output_buffer | 1.1 MB | 输出缓冲较大,但未超限 |
⚠️ 观察点:
订阅连接数量偏多,需关注是否有大量空闲订阅(可能浪费资源);
建议对订阅连接做 空闲超时清理( cmd=subscribe 连接)。
🧠 三、内存使用(# Memory)
指标 | 值 | 分析 |
---|---|---|
used_memory | 7.93 GB | 实际使用内存 |
used_memory_peak | 9.76 GB | 峰值使用量 |
maxmemory | 32 GB | 实例配置内存上限 |
mem_fragmentation_ratio | 1.02 | 正常(理想值 1.0~1.5) |
mem_not_counted_for_evict | 386 MB | 持久化/AOF缓冲未计入淘汰范围 |
mem_allocator | jemalloc-5.1.0 | 正常 |
oom_err_count | 0 | 从未 OOM |
✅ 结论:
内存使用仅占上限的 25%,资源充裕;
内存碎片率健康;
未出现 OOM 或频繁内存重分配;
AOF 缓冲区占用较大(mem_aof_buffer ~386MB),说明存在一定写入压力。
⚙️ 四、运行指标(# Stats)
指标 | 值 | 说明 |
---|---|---|
instantaneous_ops_per_sec | 71,028 | 当前每秒处理约 7 万命令,负载较高 |
instantaneous_input_kbps | 18 MB/s | 网络输入速率较高 |
instantaneous_output_kbps | 124 MB/s | 输出数据量非常大(Pub/Sub 推送明显) |
keyspace_hits/misses | 13,986,441,669 / 1,694,424,826 | 命中率约 90.7%,优秀 |
expired_keys | 17,505,442 | 正常,有活跃的 key 过期机制 |
evicted_keys | 0 | 未达到内存上限,无淘汰压力 |
total_commands_processed | 71,869,259,985 | 高请求量场景 |
error_replies | 1,211,787 | 错误率 <0.002%,可接受 |
✅ 结论:
请求量巨大但系统稳定;
Pub/Sub 占比高(output kbps大),I/O 压力集中在推送;
命中率良好;
没有内存淘汰;
说明 性能瓶颈更可能在 CPU 或网络带宽,而不是内存。
🧩 五、复制状态(# Replication)
字段 | 值 | 说明 |
---|---|---|
role | master | 当前为主节点,无从节点 |
✅ 说明:
没有同步复制压力;
适合分析性能问题时聚焦本节点。
🔢 六、CPU 使用(# CPU)
指标 | 值 | 说明 |
---|---|---|
used_cpu_sys | 396,997.8 秒 | 内核态 CPU 累积时间 |
used_cpu_user | 402,853.0 秒 | 用户态 CPU 累积时间 |
总计 CPU 时间 | ≈ 799,851 秒 | 主进程累积使用 CPU 时间 |
运行时长 | 1,027,891 秒 | 程序运行约 11.9 天 |
👉 平均 CPU 使用核数:
799,851 ÷ 1,027,891 ≈ 0.78 核
💡 意味着:
Redis 运行时平均只占用不到 1 核;
峰值请求时可能瞬时打满单核;
Redis 5.0 单线程 => 提升性能需靠更高主频或分片。
❗ 七、错误统计(# Errorstats)
ERR: 1,208,188 次(一般是命令格式错误或 key 不存在操作)
NOAUTH: 42 次(可能脚本/应用忘记认证)
NOPERM: 3,426 次(权限错误)
LOADING: 56(启动阶段)
NOMASTERLINK: 75(短暂断链)
⚠️ 建议:
检查应用日志,看是否频繁访问未授权的命令;
1.2M 的 ERR 有点偏高(可能客户端脚本逻辑问题)。
🗃️ 八、键空间(# Keyspace)
DB | Keys | Expiring | AvgTTL |
---|---|---|---|
db0 | 1,289 | 486 | 43,357秒 (~12小时) |
db3 | 109,537 | 6,389 | ~27分钟 |
db10 | 392,013 | 74,700 | ~2.3天 |
db13 | 14,037 | 14,037 | ~33小时 |
✅ 说明:
db10 是主要业务数据库;
多数 key 有 TTL,说明缓存机制设计良好;
没有明显的 TTL 异常。
🧾 综合诊断结论
模块 | 状态 | 建议 |
---|---|---|
内存 | ✅ 健康 | 使用约 8GB/32GB,无碎片问题 |
CPU | ⚠️ 轻度瓶颈 | 平均占用 <1核,但 Redis 单线程,高 QPS 时可能打满 |
网络 | ⚠️ 高负载 | output 超 100MB/s,Pub/Sub 推送压力大 |
客户端 | ⚠️ 较多(1937个) | 建议监控订阅连接是否存在空闲长期连接 |
错误率 | ⚠️ 低但异常多(1.2M ERR) | 检查客户端命令格式、授权逻辑 |
keyspace 命中率 | ✅ 优秀 | 命中率 90.7% |
过期机制 | ✅ 正常 | key 按预期过期 |
✅ 建议优化方向
Pub/Sub 优化
检查是否存在长时间空闲订阅;
如果可以,改为 Stream 或 Kafka 异步消息。
CPU 性能优化
Redis 5.0 是单线程 → 考虑升级 Redis 6+ 开启 io-threads;
或在应用层分片多实例,分摊 CPU 压力。
客户端优化
限制空闲连接数;
统一连接池,防止“连接风暴”。
监控增强
定时抓取 INFO 对比 used_cpu_user 增长率;
设置 Redis 慢查询日志;
关注 instantaneous_ops_per_sec 峰值。