redis之哨兵模式

📅 2026/7/1 16:00:16
redis之哨兵模式
哨兵(sentinel)哨兵执行守卫任务的士兵或看守人在redis的哨兵模式中就是扮演监督/监控角色的节点。架构组成┌─────────────────────────────────────────────────────────────┐ │ 哨兵集群Sentinel Cluster │ │ ┌─────────┐ ┌─────────┐ ┌─────────┐ │ │ │ Sentinel│◄──►│ Sentinel│◄──►│ Sentinel│ │ │ │ S1 │ │ S2 │ │ S3 │ │ │ │ :26379 │ │ :26380 │ │ :26381 │ │ │ └────┬────┘ └────┬────┘ └────┬────┘ │ │ │ │ │ │ │ └──────────────┼──────────────┘ │ │ │ │ │ 监控 故障转移决策 │ │ │ │ │ ┌───────────────────┼───────────────────┐ │ │ │ ▼ │ │ │ │ ┌─────────┐ ┌─────────┐ │ │ │ │ │ Master │◄─────────│ Slave │ │ 主从复制数据层 │ │ │ │ :6379 │ 同步 │ :6380 │ │ │ │ │ │(写读)│ │(读)│ │ │ │ │ └─────────┘ └─────────┘ │ │ │ └─────────────────────────────────────┘ │ └─────────────────────────────────────────────────────────────┘推荐配置至少 3 个 Sentinel 节点奇数个便于投票决策核心工作机制监控机制Sentinel 每秒向所有节点发送 PING 命令 ┌─────────────────────────────────────────────┐ │ Sentinel ──PING──► Master │ │ Sentinel ──PING──► Slave │ │ Sentinel ──PING──► 其他 Sentinel │ └─────────────────────────────────────────────┘ 超时判定 ┌─────────────────────────────────────────────┐ │ down-after-milliseconds默认 30s │ │ │ │ 若 PING 在指定时间内无有效回复 │ │ • 主观下线SDOWN, Subjective Down │ │ → 单个 Sentinel 认为节点不可用 │ │ │ │ 若多数 Sentinelquorum都认为主观下线 │ │ • 客观下线ODOWN, Objective Down │ │ → 触发故障转移流程 │ └─────────────────────────────────────────────┘故障转移流程步骤 1判定客观下线ODOWN ┌─────────────────────────────────────────────┐ │ Sentinel S1 发现 Master 主观下线 │ S1 向其他 Sentinel 询问 Master 状态 │ 若 ≥ quorum(法定人数) 个 Sentinel 同意 → ODOWN └─────────────────────────────────────────────┘ ↓ 步骤 2选举领头 SentinelLeader Election ┌─────────────────────────────────────────────┐ │ 每个 Sentinel 向其他 Sentinel 发送请求 │ 我要当 Leader请投票给我 │ │ 规则 │ • 每个 Sentinel 只能投一票 │ • 先到先得 │ • 获得多数票的 Sentinel 成为 Leader │ │ 目的避免多个 Sentinel 同时执行故障转移 └─────────────────────────────────────────────┘ ↓ 步骤 3选择新 Master ┌─────────────────────────────────────────────┐ │ Leader Sentinel 从 Slave 中选择新 Master │ │ 筛选条件依次判断 │ 1. 排除已下线的 Slave │ 2. 排除最近 5 秒内没有回复 PING 的 Slave │ 3. 排除与旧 Master 断开复制时间过长的 Slave │ 防止数据太旧 │ 4. 优先选择优先级高的replica-priority │ 5. 优先选择复制偏移量大的数据最新 │ 6. 优先选择 Run ID 小的 └─────────────────────────────────────────────┘ ↓ 步骤 4晋升与通知 ┌─────────────────────────────────────────────┐ │ 1. 向选中的 Slave 发送 SLAVEOF NO ONE │ │ → 该 Slave 变为 Master │ │ │ │ 2. 向其他 Slave 发送 SLAVEOF new_master_ip │ │ → 重新建立主从复制 │ │ │ │ 3. 更新自身配置记录新 Master 信息 │ │ │ │ 4. 向客户端发布 switch-master 频道消息 │ │ → 通知客户端切换连接 │ └─────────────────────────────────────────────┘核心配置参数参数默认值说明sentinel monitor mymaster 127.0.0.1 6379 2—监控的 Master 名、IP、端口、quorum判定 ODOWN 的最小 Sentinel 数sentinel down-after-milliseconds mymaster 3000030000主观下线判定时间毫秒sentinel parallel-syncs mymaster 11故障转移时同时同步的 Slave 数量sentinel failover-timeout mymaster 180000180000故障转移超时时间毫秒sentinel auth-pass mymaster password—Master 密码认证客户端连接机制传统方式直连 客户端 ──► Master ──► 读写 Master 宕机 → 客户端报错 → 手动改配置 哨兵方式 ┌─────────────────────────────────────────────┐ │ 1. 客户端连接 Sentinel 集群 │ 获取当前 Master 地址 │ │ 2. 客户端缓存 Master 地址 │ 正常读写 │ │ 3. Sentinel 检测到故障转移 │ 发布 switch-master 消息 │ │ 4. 客户端收到通知 │ 断开旧连接连接新 Master │ │ 5. 若客户端未收到通知网络问题 │ 读写旧 Master 失败 → 重新向 Sentinel 查询 └─────────────────────────────────────────────┘ 主流客户端支持 • JedisSentinelPoolJava • redis-py-sentinelPython • go-redis FailoverClientGo • StackExchange.Redis.NET数据一致性┌─────────────────────────────────────────────────────────────┐ │ 异步复制带来的数据风险 │ │ Master 写入数据 A ──► 未同步到 Slave ──► Master 宕机 │ │ 结果 │ • 新 Master原 Slave没有数据 A │ • 数据 A 丢失 │ │ 缓解方案 │ • min-slaves-to-write 1 # 至少 1 个 Slave 同步才返回成功 │ • min-slaves-max-lag 10 # Slave 延迟不超过 10 秒 │ │ 代价写入性能下降 └─────────────────────────────────────────────────────────────┘优缺点优点缺点自动故障转移高可用异步复制故障转移时可能丢数据客户端自动切换透明只有一个 Master写性能无法扩展配置简单轻量级脑裂风险网络分区时可能双 Master支持多组主从监控不自动处理数据分片需 Cluster 模式与cluster对比维度哨兵模式Cluster 模式数据分片不支持支持16384 个 Slot写扩展单 Master多 Master读扩展多 Slave多 Slave故障转移自动自动复杂度低高适用场景数据量小、高可用数据量大、高并发总结哨兵模式通过多 Sentinel 节点投票监控、领头选举、自动故障转移在主从复制基础上实现了 Redis 的高可用。它解决了Master 宕机后手动切换的问题但受限于单 Master 写性能和异步复制的数据一致性风险。