【架构实战】缓存架构 Redis 集群部署

一、Redis缓存的重要性

在高并发系统中,缓存是必不可少的。Redis作为最流行的内存数据库,以其高性能、低延迟的特点,成为缓存层的首选。

二、单机Redis的瓶颈

单机Redis虽然性能优异,但存在以下问题:

  • 容量限制:受单机内存限制
  • 可用性风险:单点故障导致整个缓存层瘫痪
  • 性能瓶颈:无法水平扩展

三、Redis集群架构

1. Redis主从复制

复制代码
Master (读写)
  ↓ 复制
Slave1 (只读)
Slave2 (只读)
yaml 复制代码
# Master配置
port 6379
bind 0.0.0.0

# Slave配置
port 6380
slaveof 127.0.0.1 6379

2. Redis Sentinel(哨兵模式)

复制代码
Sentinel1 ─┐
Sentinel2 ─┼─ 监控 Master/Slave
Sentinel3 ─┘
yaml 复制代码
# sentinel.conf
port 26379
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000

3. Redis Cluster(集群模式)

复制代码
┌─────────┐
│ Node 1  │ (Slot 0-5460)
└─────────┘
     ↓
┌─────────┐
│ Node 2  │ (Slot 5461-10922)
└─────────┘
     ↓
┌─────────┐
│ Node 3  │ (Slot 10923-16383)
└─────────┘

四、Redis Cluster部署

1. 启动集群节点

bash 复制代码
# 启动6个节点(3主3从)
redis-server --port 7000 --cluster-enabled yes --cluster-config-file nodes-7000.conf
redis-server --port 7001 --cluster-enabled yes --cluster-config-file nodes-7001.conf
redis-server --port 7002 --cluster-enabled yes --cluster-config-file nodes-7002.conf
redis-server --port 7003 --cluster-enabled yes --cluster-config-file nodes-7003.conf
redis-server --port 7004 --cluster-enabled yes --cluster-config-file nodes-7004.conf
redis-server --port 7005 --cluster-enabled yes --cluster-config-file nodes-7005.conf

2. 创建集群

bash 复制代码
redis-cli --cluster create 127.0.0.1:7000 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 --cluster-replicas 1

3. 验证集群

bash 复制代码
redis-cli -c -p 7000
> cluster info
> cluster nodes

五、Java客户端连接

java 复制代码
@Configuration
public class RedisConfig {
    
    @Bean
    public LettuceConnectionFactory connectionFactory() {
        return new LettuceConnectionFactory();
    }
    
    @Bean
    public RedisTemplate redisTemplate(
            LettuceConnectionFactory connectionFactory) {
        RedisTemplate template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);
        
        // 设置序列化
        StringRedisSerializer stringSerializer = new StringRedisSerializer();
        template.setKeySerializer(stringSerializer);
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        
        return template;
    }
}

@Service
public class CacheService {
    
    @Autowired
    private RedisTemplate redisTemplate;
    
    public void set(String key, Object value, long timeout, TimeUnit unit) {
        redisTemplate.opsForValue().set(key, value, timeout, unit);
    }
    
    public Object get(String key) {
        return redisTemplate.opsForValue().get(key);
    }
}

六、集群常见问题

问题1:数据倾斜

原因 :某个节点的Slot数据量过大
解决:使用 redis-cli --cluster rebalance 重新平衡

问题2:集群分裂

原因 :网络分区导致集群分裂
解决:配置 cluster-node-timeout 和 cluster-replica-validity-factor

问题3:性能下降

原因 :网络延迟、GC暂停
解决:优化网络、调整JVM参数

七、总结

Redis集群提供了高可用和水平扩展的能力,但也增加了复杂性。选择合适的架构(主从/Sentinel/Cluster)需要根据业务规模和可用性要求来决定。

思考题:在你的项目中,为什么选择了当前的Redis架构?有没有遇到过集群相关的问题?


个人观点,仅供参考

相关推荐
自由生长20241 小时前
科普-clickhouse的一些基本知识
架构
木易 士心2 小时前
Node.js 后端开发全解析:从核心原理架构到实战应用
后端·架构·node.js
皮卡丘不断更2 小时前
我把传统项目问答升级成了 Agent-RAG:Spring Boot + FastAPI + ChromaDB 工程落地实践
人工智能·spring boot·后端·架构·python3.11
自由生长20243 小时前
技术科普-存算分离
架构
SuniaWang10 小时前
《Spring AI + 大模型全栈实战》学习手册系列 · 专题六:《Vue3 前端开发实战:打造企业级 RAG 问答界面》
java·前端·人工智能·spring boot·后端·spring·架构
Percep_gan11 小时前
Linux中安装Redis,很详细
linux·运维·redis
heimeiyingwang11 小时前
【架构实战】云原生架构设计原则
云原生·架构
老鱼说AI12 小时前
大规模并发处理器程序设计(PMPP)讲解(CUDA架构):第四期:计算架构与调度
c语言·深度学习·算法·架构·cuda