副标题[/!--empirenews.page--]
Redis作为一个非结构化的内存数据库,在某些应用场景具备相应优势,在实际的场景设计中也得到广泛的关注和使用。但是,大部分企业的Redis数据库架构为单机运行,没有设计容灾复制,这样对于Redis的容错特性没有发挥出来,而且无持久化情况下,数据存在丢失风险。
特别是以一些微服务企业,Redis作为热点数据层,面对传统数据库的请求较少。因此对于缓存依赖性就很高,一旦出现缓存崩掉,所有的数据就会流入到传统数据库,对于高并发情况下,这样的性能反应就会很差。
因此,本文在依托Redis主从环境下,针对访问的数据一致性进行分析,解开Redis复制原理的神秘面纱。
一.Redis架构
开篇以Redis的架构出发,这也是分析Redis数据一致性的基础前提,对Redis的架构进而了解后,分析数据一致性的实现原理。本文着重与Redis集群与主从复制进行对比分析。
Redis的常规架构方式有以下几种:
- 单机单实例运行
- 系统HA主从复制
- Redis 集群
- 系统HA Redis 集群

图1,Redis架构类型
二、Redis 集群
在很多企业中没有Redis集群,但是至少做了主从复制。有了主从复制,当主节点挂掉的时候,可以让从节点过来进行接管,这样服务可以继续运行。如果没有此操作,那么要恢复业务,就需要等主节点进行数据恢复和重启,不仅耗时较长,同时影响业务的连续性。
Redis 集群提供了以下两个好处:
- 将数据自动切分(split)到多个节点的能力。
- 当集群中的一部分节点失效或者无法进行通讯时, 仍然可以继续处理命令请求的能力。
Codis 是redis的集群方案之一,欣慰的是它是国内自己的工程师开发的。

图2,国产codis集群架构
三、Redis 主从复制
主从复制的诞生,就是为了存在单节点故障情况下,可以进行快速转移,使得业务可以正常运作。 Redis 集群对节点使用了主从复制功能: 集群中的每个节点都有 1 个至 N 个复制节点(replica), 其中一个复制节点为主节点(master), 而其余的 N-1 个复制节点为从节点(slave)。
复制虽然解决了数据多副本的问题,但是同时也存在多副本一致性的难题。在此之前,构建一套主从复制模型,针对其运行进行剖析。
对于主从复制的搭建也是非常简单的,为了方便演示下面简述一下其搭建过程。
- 主服务器IP:127.0.0.1
- 主服务器端口:6379
- 从服务器IP:127.0.0.1
- 主服务器端口:6380
(1) 整理conf配置文件
复制一份conf配置文件给从库使用,方便后期从库的配置管理
- [redis@albert redis-5.0.4]$ cp redis.conf redis.conf6380
(2) 同步复制配置
备注:仅在从库上进行设置
- ################################# REPLICATION #################################
-
- # Master-Replica replication. Use replicaof to make a Redis instance a copy of
- # another Redis server. A few things to understand ASAP about Redis replication.
- #
- # +------------------+ +---------------+
- # | Master | ---> | Replica |
- # | (receive writes) | | (exact copy) |
- # +------------------+ +---------------+
- #
- # 1) Redis replication is asynchronous, but you can configure a master to
- # stop accepting writes if it appears to be not connected with at least
- # a given number of replicas.
- # 2) Redis replicas are able to perform a partial resynchronization with the
- # master if the replication link is lost for a relatively small amount of
- # time. You may want to configure the replication backlog size (see the next
- # sections of this file) with a sensible value depending on your needs.
- # 3) Replication is automatic and does not need user intervention. After a
- # network partition replicas automatically try to reconnect to masters
- # and resynchronize with them.
- #
- # replicaof <masterip> <masterport>
- # slaveof <masterip> <masterport>
- slaveof 127.0.0.1 6379
(编辑:好传媒网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|