Redis集群高可用方案 Redis提供多种高可用架构方案。 Sentinel哨兵 基本架构 配置Sentinel 启动Sentinel Sentinel优势 自动故障转移 配置中心 服务发现 Redis Cluster 基本架构 集群配置 创建集群 集群操作 Codis架构 基本架构 Codis组件 Codis Server:修改的Redis Codis Proxy:代理层 Codis Dashboard:管理界面 Zookeeper:配置存储 架构对比 方案 | 优点 | 缺点 Sentinel | 简单 | 单主写入 Cluster | 官方方案 | 迁移成本 Codis | 兼容性好 | 额外组件 高可用实践 主从复制 读写分离 故障转移 Redis集群需要根据业务选择。
Redis提供多种高可用架构方案。
应用 → Sentinel → Master ↓ Slave
# sentinel.conf port 26379 sentinel monitor mymaster 127.0.0.1 6379 2 sentinel down-after-milliseconds mymaster 5000 sentinel failover-timeout mymaster 10000 sentinel parallel-syncs mymaster 1
redis-sentinel sentinel.conf
应用 → Redis Cluster [槽0-5460] [槽5461-10922] [槽10923-16383] Master Master Master Slave Slave Slave
# redis.conf cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 cluster-require-full-coverage yes
redis-cli --cluster create \ 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 \ 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006 \ --cluster-replicas 1
# 查看集群状态 redis-cli -c cluster info # 查看节点 redis-cli -c cluster nodes # 添加节点 redis-cli --cluster add-node new_host:7000 existing_host:7000 # 故障转移 redis-cli --cluster failover host:7000
应用 → Codis Proxy → Redis Slots ↓ [多个Redis实例]
| 方案 | 优点 | 缺点 |
|---|---|---|
| Sentinel | 简单 | 单主写入 |
| Cluster | 官方方案 | 迁移成本 |
| Codis | 兼容性好 | 额外组件 |
# 从库配置 slaveof master_host 6379 # 查看复制状态 redis-cli info replication
# 主库写入 redis-cli -h master_host SET key value # 从库读取 redis-cli -h slave_host GET key
# Sentinel自动转移 # 或手动转移 redis-cli FAILOVER
Redis集群需要根据业务选择。Sentinel适合简单场景,Cluster适合大规模部署。