Redis 性能监控与诊断
性能监控是保障 Redis 稳定运行的关键。通过监控关键指标、分析慢查询、使用诊断工具,可以快速定位和解决性能问题。本文将深入 Redis 监控体系。
一、监控指标
1.1 关键指标
# 获取所有信息
redis-cli INFO
# 分类获取
INFO server # 服务器信息
INFO clients # 客户端信息
INFO memory # 内存信息
INFO persistence # 持久化信息
INFO stats # 统计信息
INFO replication # 复制信息
INFO cpu # CPU 信息
INFO commandstats # 命令统计
1.2 内存指标
INFO memory
# 关键指标
used_memory: 104857600 # 已用内存(字节)
used_memory_human: "100.00M" # 已用内存(可读)
used_memory_rss: 125829120 # 操作系统分配内存
used_memory_peak: 157286400 # 峰值内存
maxmemory: 2147483648 # 最大内存
maxmemory_human: "2.00G"
mem_fragmentation_ratio: 1.20 # 碎片率
告警阈值:
used_memory / maxmemory > 0.9 # 内存使用率过高
mem_fragmentation_ratio > 1.5 # 碎片率过高
mem_fragmentation_ratio < 1.0 # 碎片率过低(可能 swap)
1.3 性能指标
INFO stats
# 关键指标
total_connections_received: 100000 # 总连接数
total_commands_processed: 5000000 # 总命令数
instantaneous_ops_per_sec: 10000 # 每秒操作数
total_net_input_bytes: 1000000000 # 网络输入字节
total_net_output_bytes: 2000000000 # 网络输出字节
keyspace_hits: 4500000 # 缓存命中数
keyspace_misses: 500000 # 缓存未命中数
hit_rate: 0.9 # 命中率 = hits/(hits+misses)
告警阈值:
hit_rate < 0.8 # 命中率过低
instantaneous_ops_per_sec > 50000 # QPS 过高
1.4 客户端指标
INFO clients
# 关键指标
connected_clients: 100 # 当前连接数
blocked_clients: 5 # 阻塞客户端数
maxclients: 10000 # 最大客户端数
client_recent_max_input_buffer: 1024 # 最大输入缓冲
client_recent_max_output_buffer: 2048 # 最大输出缓冲
告警阈值:
connected_clients / maxclients > 0.8 # 连接数过多
blocked_clients > 10 # 阻塞客户端过多
1.5 持久化指标
INFO persistence
# RDB 指标
rdb_changes_since_last_save: 100 # 未保存的变更数
rdb_last_bgsave_status: ok # 上次 BGSAVE 状态
rdb_last_bgsave_time_sec: 10 # 上次 BGSAVE 耗时
# AOF 指标
aof_enabled: 1 # AOF 是否启用
aof_rewrite_in_progress: 0 # 是否在重写
aof_last_rewrite_time_sec: 5 # 上次重写耗时
aof_current_size: 104857600 # 当前 AOF 大小
告警阈值:
rdb_last_bgsave_time_sec > 60 # BGSAVE 耗时过长
aof_current_size > 1GB # AOF 文件过大
二、慢查询分析
2.1 慢查询配置
# redis.conf
slowlog-log-slower-than 10000 # 记录超过 10ms 的查询
slowlog-max-len 128 # 慢查询日志最大长度
2.2 查看慢查询
# 获取慢查询
SLOWLOG GET 10
# 输出示例
1) 1) (integer) 100
2) (integer) 1640000000
3) (integer) 15000
4) 1) "KEYS"
2) "user:*"
5) "192.168.1.100:54321"
6) "GET"
# 字段说明:
# 1: 查询 ID
# 2: 执行时间戳
# 3: 执行耗时(微秒)
# 4: 执行的命令
# 5: 客户端地址
# 6: 命令来源
2.3 慢查询分析
# 获取慢查询数量
SLOWLOG LEN
# 清空慢查询日志
SLOWLOG RESET
# 分析慢查询
redis-cli SLOWLOG GET 100 | grep -E "KEYS|SMEMBERS|HGETALL"
# 找出危险命令
2.4 常见慢查询
# 危险命令
KEYS * # 全量扫描(生产禁用)
SMEMBERS large_set # 大集合全量获取
HGETALL large_hash # 大 Hash 全量获取
LRANGE large_list 0 -1 # 大 List 全量获取
# 优化方案
SCAN 0 MATCH pattern COUNT 100 # 分批扫描
SSCAN key 0 COUNT 100 # 分批获取集合
HSCAN key 0 COUNT 100 # 分批获取 Hash
LRANGE key 0 99 # 限制范围
三、诊断工具
3.1 redis-cli 内置工具
# 延迟测试
redis-cli --latency
# 延迟历史
redis-cli --latency-history
# 延迟间隔测试
redis-cli --latency-interval 1000
# BigKeys 扫描
redis-cli --bigkeys
# 输出示例
# Scanning the entire keyspace to find biggest keys...
#
# Biggest string found so far '"key1"' with 1048576 bytes
# Biggest list found so far '"key2"' with 10000 items
# Biggest set found so far '"key3"' with 5000 items
# Biggest hash found so far '"key4"' with 3000 items
3.2 内存分析
# 内存使用情况
MEMORY STATS
# 输出示例
# 1) "peak.allocated"
# 2) (integer) 157286400
# 3) "total.allocated"
# 4) (integer) 104857600
# 5) "startup.allocated"
# 6) (integer) 1048576
# ...
# 单个 Key 内存占用
MEMORY USAGE key
# 示例
MEMORY USAGE user:1001
# (integer) 1024 # 1KB
3.3 集群诊断
# 检查集群
redis-cli --cluster check 192.168.1.1:7000
# 修复集群
redis-cli --cluster fix 192.168.1.1:7000
# 重新分片
redis-cli --cluster reshard 192.168.1.1:7000
3.4 实时监控
# 实时监控命令
redis-cli --stat
# 输出示例
# ------- data ------ ----------------- load -------------------- - child -
# keys mem clients blocked requests connections
# 1000 100M 10 0 1000/s 50
四、性能优化
4.1 内存优化
# 1. 设置内存淘汰策略
CONFIG SET maxmemory-policy allkeys-lru
# 2. 清理大 Key
redis-cli --bigkeys
DEL large_key
# 3. 使用合适的数据结构
# Hash vs String
HSET user:1001 name "John" age 25 # 更省内存
# 4. 定期清理过期数据
SCAN 0 MATCH temp:* COUNT 100
DEL temp:key
4.2 命令优化
# 1. 使用批量操作
MSET key1 value1 key2 value2
MGET key1 key2 key3
# 2. 避免危险命令
# ❌ KEYS *
# ✅ SCAN 0 MATCH pattern COUNT 100
# 3. 使用 Pipeline
redis-cli --pipe < commands.txt
4.3 配置优化
# 内存配置
maxmemory 4gb
maxmemory-policy allkeys-lru
# 网络配置
tcp-keepalive 60
timeout 300
# 持久化配置
save 900 1
save 300 10
appendonly yes
appendfsync everysec
# 慢查询配置
slowlog-log-slower-than 10000
slowlog-max-len 128
五、监控方案
5.1 Prometheus + Grafana
# prometheus.yml
scrape_configs:
- job_name: 'redis'
static_configs:
- targets: ['192.168.1.1:6379']
metrics_path: /metrics
# redis_exporter 配置
redis_exporter:
redis_addr: "192.168.1.1:6379"
redis_password: "your_password"
namespace: "redis"
5.2 关键告警
# Grafana 告警规则
- alert: RedisMemoryHigh
expr: redis_memory_used_bytes / redis_max_memory_bytes > 0.9
for: 5m
labels:
severity: warning
annotations:
summary: "Redis memory usage is high"
- alert: RedisHitRateLow
expr: redis_keyspace_hits / (redis_keyspace_hits + redis_keyspace_misses) < 0.8
for: 10m
labels:
severity: warning
annotations:
summary: "Redis cache hit rate is low"
- alert: RedisSlowlogHigh
expr: increase(redis_slowlog_length[5m]) > 10
for: 5m
labels:
severity: warning
annotations:
summary: "Redis slowlog is increasing"
5.3 日志分析
# Redis 日志位置
/var/log/redis/redis-server.log
# 查看慢查询日志
grep "slow command" /var/log/redis/redis-server.log
# 查看连接日志
grep "Accepted" /var/log/redis/redis-server.log
# 查看错误日志
grep "ERROR" /var/log/redis/redis-server.log
六、故障排查
6.1 内存问题
# 问题:内存使用率过高
# 排查步骤:
# 1. 检查内存使用
INFO memory
# 2. 查找大 Key
redis-cli --bigkeys
# 3. 检查内存碎片
MEMORY DOCTOR
# 4. 清理过期数据
SCAN 0 MATCH temp:* COUNT 100
DEL temp:key
# 5. 重启(最后手段)
redis-cli SHUTDOWN
redis-server
6.2 性能问题
# 问题:QPS 下降
# 排查步骤:
# 1. 检查慢查询
SLOWLOG GET 100
# 2. 检查客户端连接
CLIENT LIST
# 3. 检查网络
redis-cli --latency
# 4. 检查 CPU
INFO cpu
# 5. 检查阻塞
INFO clients | grep blocked
6.3 持久化问题
# 问题:AOF 文件过大
# 排查步骤:
# 1. 检查 AOF 状态
INFO persistence
# 2. 触发重写
BGREWRITEAOF
# 3. 检查重写进度
INFO persistence | grep rewrite
七、最佳实践
7.1 监控配置
监控频率:
- 关键指标:1 分钟
- 一般指标:5 分钟
- 慢查询:实时
告警阈值:
- 内存使用率 > 90%
- 命中率 < 80%
- 慢查询 > 10/5 分钟
7.2 定期检查
每日检查:
- 内存使用率
- 命中率
- 慢查询数量
每周检查:
- 大 Key 扫描
- 内存碎片率
- 持久化状态
每月检查:
- 性能基准测试
- 容量规划
- 配置优化
7.3 文档记录
维护文档:
- 集群架构图
- 配置参数说明
- 故障处理手册
- 性能基准数据
总结
Redis 监控核心要点:
| 指标类型 | 关键指标 | 告警阈值 |
|---|---|---|
| 内存 | used_memory | > 90% |
| 性能 | hit_rate | < 80% |
| 客户端 | connected_clients | > 80% |
| 持久化 | rdb_last_bgsave_time | > 60s |
| 慢查询 | slowlog | > 10/5min |
最佳实践:
- 配置合理的监控指标
- 设置告警阈值
- 定期分析慢查询
- 使用专业监控工具
- 建立故障排查手册
掌握监控诊断,保障 Redis 稳定运行!