Redis 从入门到精通(十三):性能优化与运维实战 —— 慢查询、内存优化、监控与安全

Redis 从入门到精通(十三):性能优化与运维实战 ------ 慢查询、内存优化、监控与安全


一、慢查询分析与优化

1.1 慢查询日志

Redis 的慢查询日志记录的是执行时间超过阈值的命令(不包括排队和网络传输时间)。

bash 复制代码
# ============ 配置 ============
CONFIG SET slowlog-log-slower-than 10000  # 超过 10000 微秒(10ms)记录
CONFIG SET slowlog-max-len 128            # 最多保留 128 条记录

# ============ 查看 ============
SLOWLOG GET 10     # 查看最近 10 条慢查询

# 输出示例:
# 1) 1) (integer) 42           # 日志 ID
#    2) (integer) 1686123456    # 时间戳
#    3) (integer) 15234         # 执行耗时(微秒)
#    4) 1) "KEYS"              # 命令
#       2) "user:*"
#    5) "127.0.0.1:54321"     # 客户端

SLOWLOG LEN        # 当前慢查询条数
SLOWLOG RESET      # 清空慢查询日志

1.2 常见慢查询原因与解决方案

原因 特征 解决方案
O(n) 命令 KEYS *SMEMBERSHGETALLLRANGE 0 -1 在大 key 上 SCAN 替代 KEYSHSCAN 替代 HGETALL、分页取
大 Key 删除 DEL bigkey 阻塞 UNLINK 异步删除(4.0+)
复杂度高的命令 SORTSUNIONZINTERSTORE 评估是否在业务层做
fork 耗时 BGSAVE/BGREWRITEAOF 期间延迟升高 减少 fork 频率,预留内存
内存淘汰 maxmemory 触发淘汰 扩容或调整淘汰策略
bash 复制代码
# 排查 O(n) 命令的 key 大小
redis-cli --bigkeys

# 查看某个 key 的内存占用
MEMORY USAGE big:hash:key

# 查看命令的复杂度
# Redis 文档中每个命令都标了 Time complexity
# 特别注意 O(N)、O(N+M) 的命令

1.3 Latency Monitor

Redis 2.8.13 引入延迟监控,可以记录各种延迟事件:

bash 复制代码
# 开启延迟监控
CONFIG SET latency-monitor-threshold 100  # 超过 100ms 的事件记录

# 查看延迟事件
LATENCY LATEST       # 最近的事件
LATENCY HISTORY command  # 命令延迟历史
LATENCY DOCTOR       # 诊断报告
LATENCY RESET        # 重置

二、内存优化

2.1 内存分析命令

bash 复制代码
# 总体内存使用
INFO memory

# 关键指标解读
used_memory:8779512             # Redis 实际使用的内存(字节)
used_memory_rss:14020608        # 操作系统分配的内存(含碎片)
mem_fragmentation_ratio:1.60    # 碎片率 = used_memory_rss / used_memory
                                 # > 1.5 需要关注,> 2 需要处理

# 单个 key 内存占用(Redis 4.0+)
MEMORY USAGE user:1001

# 抽样分析 key 大小
MEMORY STATS                     # 内存使用统计概览

2.2 内存碎片处理

碎片产生原因

  • jemalloc 分配器的固定大小分配策略
  • 频繁的 key 增删导致内存碎片
  • 大量小 key 更新引起内存页重新分配

处理方案

bash 复制代码
# 方案一:自动碎片整理(Redis 4.0+)
CONFIG SET activedefrag yes

# 碎片整理触发条件
active-defrag-ignore-bytes 100mb    # 碎片超过 100MB 开始整理
active-defrag-threshold-lower 10    # 碎片率 > 10% 开始
active-defrag-threshold-upper 100   # 碎片率达到 100% 时全力整理

# 方案二:重启(最终手段)
# 通过主从切换实现零停机重启

2.3 内存优化最佳实践

bash 复制代码
# 1. 用小数据结构
# Hash/Set/ZSet 在元素少时用 listpack/intset 编码,极大省内存
# 确保不超过阈值
hash-max-listpack-entries 512
hash-max-listpack-value 64

# 2. 用整数 ID 而不是长字符串
# "user:1001" 优于 "user:550e8400-e29b-41d4-a716-446655440000"

# 3. 过期时间合理设置
# 不要让不需要的数据永远存在

# 4. 共享整数池
# 0~9999 的整数对象被 Redis 预先创建并共享

# 5. 关闭不用的功能
# 如果不需要持久化,关闭 RDB 和 AOF 可减少内存开销

三、监控指标体系

3.1 核心监控指标

#mermaid-svg-IrN9faZfwNW7XMuH{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-IrN9faZfwNW7XMuH .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-IrN9faZfwNW7XMuH .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-IrN9faZfwNW7XMuH .error-icon{fill:#552222;}#mermaid-svg-IrN9faZfwNW7XMuH .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-IrN9faZfwNW7XMuH .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-IrN9faZfwNW7XMuH .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-IrN9faZfwNW7XMuH .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-IrN9faZfwNW7XMuH .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-IrN9faZfwNW7XMuH .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-IrN9faZfwNW7XMuH .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-IrN9faZfwNW7XMuH .marker{fill:#333333;stroke:#333333;}#mermaid-svg-IrN9faZfwNW7XMuH .marker.cross{stroke:#333333;}#mermaid-svg-IrN9faZfwNW7XMuH svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-IrN9faZfwNW7XMuH p{margin:0;}#mermaid-svg-IrN9faZfwNW7XMuH .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-IrN9faZfwNW7XMuH .cluster-label text{fill:#333;}#mermaid-svg-IrN9faZfwNW7XMuH .cluster-label span{color:#333;}#mermaid-svg-IrN9faZfwNW7XMuH .cluster-label span p{background-color:transparent;}#mermaid-svg-IrN9faZfwNW7XMuH .label text,#mermaid-svg-IrN9faZfwNW7XMuH span{fill:#333;color:#333;}#mermaid-svg-IrN9faZfwNW7XMuH .node rect,#mermaid-svg-IrN9faZfwNW7XMuH .node circle,#mermaid-svg-IrN9faZfwNW7XMuH .node ellipse,#mermaid-svg-IrN9faZfwNW7XMuH .node polygon,#mermaid-svg-IrN9faZfwNW7XMuH .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-IrN9faZfwNW7XMuH .rough-node .label text,#mermaid-svg-IrN9faZfwNW7XMuH .node .label text,#mermaid-svg-IrN9faZfwNW7XMuH .image-shape .label,#mermaid-svg-IrN9faZfwNW7XMuH .icon-shape .label{text-anchor:middle;}#mermaid-svg-IrN9faZfwNW7XMuH .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-IrN9faZfwNW7XMuH .rough-node .label,#mermaid-svg-IrN9faZfwNW7XMuH .node .label,#mermaid-svg-IrN9faZfwNW7XMuH .image-shape .label,#mermaid-svg-IrN9faZfwNW7XMuH .icon-shape .label{text-align:center;}#mermaid-svg-IrN9faZfwNW7XMuH .node.clickable{cursor:pointer;}#mermaid-svg-IrN9faZfwNW7XMuH .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-IrN9faZfwNW7XMuH .arrowheadPath{fill:#333333;}#mermaid-svg-IrN9faZfwNW7XMuH .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-IrN9faZfwNW7XMuH .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-IrN9faZfwNW7XMuH .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-IrN9faZfwNW7XMuH .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-IrN9faZfwNW7XMuH .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-IrN9faZfwNW7XMuH .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-IrN9faZfwNW7XMuH .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-IrN9faZfwNW7XMuH .cluster text{fill:#333;}#mermaid-svg-IrN9faZfwNW7XMuH .cluster span{color:#333;}#mermaid-svg-IrN9faZfwNW7XMuH div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-IrN9faZfwNW7XMuH .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-IrN9faZfwNW7XMuH rect.text{fill:none;stroke-width:0;}#mermaid-svg-IrN9faZfwNW7XMuH .icon-shape,#mermaid-svg-IrN9faZfwNW7XMuH .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-IrN9faZfwNW7XMuH .icon-shape p,#mermaid-svg-IrN9faZfwNW7XMuH .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-IrN9faZfwNW7XMuH .icon-shape .label rect,#mermaid-svg-IrN9faZfwNW7XMuH .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-IrN9faZfwNW7XMuH .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-IrN9faZfwNW7XMuH .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-IrN9faZfwNW7XMuH :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Redis 监控体系
性能指标

QPS / 延迟 / 慢查询
内存指标

used_memory / 碎片率 / 淘汰数
连接指标

连接数 / 拒绝连接数
持久化指标

RDB 状态 / AOF 大小
复制指标

主从延迟 / backlog 使用
错误指标

命令错误 / 拒绝连接

指标 命令/来源 告警阈值建议
QPS INFO statsinstantaneous_ops_per_sec 超过历史峰值 1.5 倍
命令延迟 客户端埋点 P99 > 10ms
慢查询 SLOWLOG LEN 每分钟新增 > 10
内存使用率 used_memory / maxmemory > 80%
碎片率 mem_fragmentation_ratio > 1.5
连接数 connected_clients > 80% maxclients
主从延迟 INFO replicationlag > 5 秒
RDB 状态 rdb_last_bgsave_status ≠ ok

3.2 监控脚本示例

bash 复制代码
#!/bin/bash
# redis_monitor.sh ------ 通过 cron 每分钟执行

REDIS_CLI="redis-cli -h localhost -p 6379 -a your_password"

# 内存使用率
USED=$($REDIS_CLI INFO memory | grep used_memory: | cut -d: -f2 | tr -d '\r')
MAX=$($REDIS_CLI CONFIG GET maxmemory | tail -1)

if [ "$MAX" != "0" ]; then
    RATIO=$(( USED * 100 / MAX ))
    if [ $RATIO -gt 80 ]; then
        echo "WARNING: Redis memory usage ${RATIO}%" | \
            curl -d @- http://your-alert-webhook
    fi
fi

# 慢查询
SLOW=$($REDIS_CLI SLOWLOG LEN)
if [ "$SLOW" -gt 50 ]; then
    echo "WARNING: Slowlog count: $SLOW"
fi

# 主从延迟(如果是从库)
LAG=$($REDIS_CLI INFO replication | grep lag= | cut -d= -f2 | tr -d '\r')
if [ -n "$LAG" ] && [ "$LAG" -gt 5 ]; then
    echo "WARNING: Replication lag: ${LAG}s"
fi

3.3 Info 命令速查

bash 复制代码
INFO server          # 服务器基本信息(版本、进程 ID、运行时间)
INFO clients         # 客户端连接信息
INFO memory          # 内存使用详情
INFO persistence     # RDB 和 AOF 状态
INFO stats           # 命令统计、QPS、命中率
INFO replication     # 主从复制状态
INFO cpu             # CPU 消耗
INFO commandstats    # 每个命令的执行次数和耗时
INFO keyspace        # 每个数据库的 key 数量

# 计算命中率
# keyspace_hits / (keyspace_hits + keyspace_misses)

四、线上故障排查思路

4.1 排查框架

#mermaid-svg-ve1P9Hmwjt5oFo1U{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-ve1P9Hmwjt5oFo1U .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-ve1P9Hmwjt5oFo1U .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-ve1P9Hmwjt5oFo1U .error-icon{fill:#552222;}#mermaid-svg-ve1P9Hmwjt5oFo1U .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-ve1P9Hmwjt5oFo1U .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-ve1P9Hmwjt5oFo1U .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-ve1P9Hmwjt5oFo1U .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-ve1P9Hmwjt5oFo1U .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-ve1P9Hmwjt5oFo1U .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-ve1P9Hmwjt5oFo1U .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-ve1P9Hmwjt5oFo1U .marker{fill:#333333;stroke:#333333;}#mermaid-svg-ve1P9Hmwjt5oFo1U .marker.cross{stroke:#333333;}#mermaid-svg-ve1P9Hmwjt5oFo1U svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-ve1P9Hmwjt5oFo1U p{margin:0;}#mermaid-svg-ve1P9Hmwjt5oFo1U .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-ve1P9Hmwjt5oFo1U .cluster-label text{fill:#333;}#mermaid-svg-ve1P9Hmwjt5oFo1U .cluster-label span{color:#333;}#mermaid-svg-ve1P9Hmwjt5oFo1U .cluster-label span p{background-color:transparent;}#mermaid-svg-ve1P9Hmwjt5oFo1U .label text,#mermaid-svg-ve1P9Hmwjt5oFo1U span{fill:#333;color:#333;}#mermaid-svg-ve1P9Hmwjt5oFo1U .node rect,#mermaid-svg-ve1P9Hmwjt5oFo1U .node circle,#mermaid-svg-ve1P9Hmwjt5oFo1U .node ellipse,#mermaid-svg-ve1P9Hmwjt5oFo1U .node polygon,#mermaid-svg-ve1P9Hmwjt5oFo1U .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-ve1P9Hmwjt5oFo1U .rough-node .label text,#mermaid-svg-ve1P9Hmwjt5oFo1U .node .label text,#mermaid-svg-ve1P9Hmwjt5oFo1U .image-shape .label,#mermaid-svg-ve1P9Hmwjt5oFo1U .icon-shape .label{text-anchor:middle;}#mermaid-svg-ve1P9Hmwjt5oFo1U .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-ve1P9Hmwjt5oFo1U .rough-node .label,#mermaid-svg-ve1P9Hmwjt5oFo1U .node .label,#mermaid-svg-ve1P9Hmwjt5oFo1U .image-shape .label,#mermaid-svg-ve1P9Hmwjt5oFo1U .icon-shape .label{text-align:center;}#mermaid-svg-ve1P9Hmwjt5oFo1U .node.clickable{cursor:pointer;}#mermaid-svg-ve1P9Hmwjt5oFo1U .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-ve1P9Hmwjt5oFo1U .arrowheadPath{fill:#333333;}#mermaid-svg-ve1P9Hmwjt5oFo1U .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-ve1P9Hmwjt5oFo1U .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-ve1P9Hmwjt5oFo1U .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ve1P9Hmwjt5oFo1U .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-ve1P9Hmwjt5oFo1U .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ve1P9Hmwjt5oFo1U .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-ve1P9Hmwjt5oFo1U .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-ve1P9Hmwjt5oFo1U .cluster text{fill:#333;}#mermaid-svg-ve1P9Hmwjt5oFo1U .cluster span{color:#333;}#mermaid-svg-ve1P9Hmwjt5oFo1U div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-ve1P9Hmwjt5oFo1U .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-ve1P9Hmwjt5oFo1U rect.text{fill:none;stroke-width:0;}#mermaid-svg-ve1P9Hmwjt5oFo1U .icon-shape,#mermaid-svg-ve1P9Hmwjt5oFo1U .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ve1P9Hmwjt5oFo1U .icon-shape p,#mermaid-svg-ve1P9Hmwjt5oFo1U .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-ve1P9Hmwjt5oFo1U .icon-shape .label rect,#mermaid-svg-ve1P9Hmwjt5oFo1U .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ve1P9Hmwjt5oFo1U .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-ve1P9Hmwjt5oFo1U .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-ve1P9Hmwjt5oFo1U :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 响应变慢
连不上
内存告警
数据丢了
QPS 突降
故障发生
什么症状?

  1. 查慢查询 SLOWLOG

  2. 查延迟 LATENCY DOCTOR

  3. 查 CPU INFO cpu

  4. 查 BGSAVE 是否进行中

  5. 查连接数 INFO clients

  6. 查 maxclients 配置

  7. 查网络/防火墙

  8. 查内存使用 INFO memory

  9. 查 bigkeys

  10. 查碎片率

  11. 查是否有 BGSAVE 导致 COW

  12. 查持久化是否正常

  13. 查 AOF 文件是否损坏

  14. 查主从同步状态

  15. 查慢查询

  16. 查带宽

  17. 查是否有热 Key

4.2 常见故障快速修复

bash 复制代码
# 1. O(n) 命令阻塞
# 症状:Redis 突然无响应,CPU 100%
# 排查:
CLIENT LIST | grep "KEYS\|SMEMBERS\|FLUSHALL"
# 修复:
CLIENT KILL addr  # 杀掉正在执行慢命令的客户端
# 预防:rename-command KEYS ""  # 禁用危险命令

# 2. 内存突然暴涨
# 排查:
redis-cli --bigkeys
INFO memory | grep mem_fragmentation_ratio
# 修复:
# - 如果有过期 key 堆积,等待定期删除
# - 如果是 BGSAVE 导致的 COW,等待子进程完成
# - 紧急情况:手动触发内存淘汰 CONFIG SET maxmemory-policy allkeys-lru

# 3. AOF 文件损坏
# 症状:Redis 无法启动
# 修复:
redis-check-aof --fix appendonly.aof
# 然后正常重启

# 4. 集群脑裂
# 排查:
CLUSTER NODES  # 检查各节点状态
# 修复:手动修复分区,确保多数派可用
redis-cli --cluster fix 192.168.1.101:6379

4.3 热 Key 应急处理

bash 复制代码
# 1. 快速发现热 Key(Redis 4.0+)
redis-cli --hotkeys

# 2. 查看热 Key 的访问频率
OBJECT FREQ hotkey

# 3. 临时多副本(客户端层做)
# 4. 紧急扩容 Cluster 分片

五、安全加固

5.1 基础安全配置

bash 复制代码
# ============ redis.conf 安全配置 ============

# 1. 绑定内网 IP
bind 10.0.0.1

# 2. 设置密码
requirepass "strong_password_here"

# 3. 禁用高危命令
rename-command FLUSHDB ""
rename-command FLUSHALL ""
rename-command CONFIG "CONFIG_9a8b7c6d"   # 重命名而非禁用
rename-command KEYS ""
rename-command DEBUG ""

# 4. 限制连接数
maxclients 10000

# 5. 超时断连
timeout 300  # 空闲 300 秒断开

# 6. 保护模式(默认开启)
protected-mode yes

5.2 ACL 2.0(Redis 6.0+)

bash 复制代码
# 创建用户
ACL SETUSER readonly on >password123 ~key:* +@read -@write -@dangerous
#                       ↑        ↑          ↑       ↑      ↑
#                       启用     密码        允许key模式  可读   禁止写 禁止危险命令

# 创建管理员
ACL SETUSER admin on >admin_pass ~* +@all

# 查看用户
ACL LIST
ACL WHOAMI

# 禁用 default 用户
ACL SETUSER default off

# 保存 ACL(持久化)
ACL SAVE

# 使用 ACL 登录
redis-cli -h localhost -p 6379 --user readonly --pass password123

5.3 TLS 加密(Redis 6.0+)

bash 复制代码
# 生成证书
openssl req -x509 -newkey rsa:4096 -keyout redis.key -out redis.crt \
    -days 365 -nodes -subj "/CN=redis-server"

# redis.conf
tls-port 6380
tls-cert-file /path/to/redis.crt
tls-key-file /path/to/redis.key
tls-ca-cert-file /path/to/ca.crt

# 客户端连接
redis-cli --tls --cert client.crt --key client.key --cacert ca.crt

六、总结

运维检查清单

bash 复制代码
# 每日检查
□ 内存使用率 < 80%
□ 慢查询数量 < 10/min
□ BGSAVE 状态 = ok
□ AOF 文件大小正常增长
□ 主从延迟 < 1s

# 每周检查
□ 内存碎片率 < 1.5
□ 大 Key 扫描
□ 命令统计异常排查
□ 备份文件完好
□ 连接数趋势

# 每季度
□ 性能压测
□ 故障演练(主从切换、集群分区恢复)
□ 版本升级评估

如有疑问或指正,欢迎在评论区交流。

相关推荐
用户3074596982079 小时前
Redis 延时队列详解
redis
烤代码的吐司君12 小时前
Redis 数据结构 ZSet, BIT, HyperLogLog,Geo 空间数据
redis·后端
你听得到1119 小时前
用户说 App 卡,但说不清在哪?我把 Flutter 监控 SDK 升级成了链路观测工作台
前端·flutter·性能优化
leeyi3 天前
Checkpoint 机制:Agent 怎么在断电后接着跑
redis·aigc·agent
云技纵横4 天前
一个 @Async 让循环依赖暴雷:Spring 代理的暗坑
redis
乘云数字DATABUFF4 天前
5分钟部署开源APM Databuff:OpenTelemetry全链路追踪入门实战
运维·后端
亲亲小宝宝鸭4 天前
前端性能监控:web-vitals
前端·性能优化·监控
犯困蛋挞yy4 天前
用Claude快速解决Redis代码报错反复无解的问题
redis
荣--6 天前
一键部署不是为了省时间 —— 它是把"买来的 PaaS"变成"自己的平台"的拐点
运维·zabbix·工程化·一键部署·平台化·边界设计
江华森6 天前
动手实战学 Docker — 从零到集群编排完全指南
运维