Redisreplication主从复制原理及配置-创新互联
本文主要介绍Redis replication 主从复制原理和配置及基本操作 等
成都创新互联服务项目包括元宝山网站建设、元宝山网站制作、元宝山网页制作以及元宝山网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,元宝山网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到元宝山省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!主要参考官方文档:
/tupian/20230521/
/tupian/20230521/replication.html
一.原理
This system works using three main mechanisms:
系统工作的三个主要机制:
1. When a master and a slave instances are well-connected, the master keeps the slave updated by sending a stream of commands to the slave, in order to replicate the effects on the dataset happening in the master side due to: client writes, keys expired or evicted, any other action changing the master dataset.
当master和slave实例连接良好时,master通过从slave发送命令流来更新slave
2. When the link between the master and the slave breaks, for network issues or because a timeout is sensed in the master or the slave, the slave reconnects and attempts to proceed with a partial resynchronization: it means that it will try to just obtain the part of the stream of commands it missed during the disconnection.
当master和slave连接中断时,因为网络故障或者感知到master或者slave超时。Slave重连并试图进行重新同步:意味着获取失去连接时丢失的命令流
3. When a partial resynchronization is not possible, the slave will ask for a full resynchronization. This will involve a more complex process in which the master needs to create a snapshot of all its data, send it to the slave, and then continue sending the stream of commands as the dataset changes.
当无法重新部分同步(partial resynchronization)时,slave将要求完全重新同步(full resynchronization)。它将包含一个复杂过程,这过程中master需要为所有数据创建一个快照,发送到slave,再继续对变化的数据发送命令流
数据安全性概念
Redis的持久化存储主要提供两种方式:RDB与AOF
/tupian/20230521/persistence.html
1. RDB(Snapshot)模式
是默认的,即保存某一时刻的数据到硬盘,在下一次触发snapshot前如果服务器crash,在上次snapshot之后修改的数据会丢失。
主要配置redis.conf参数:
save
stop-writes-on-bgsave-error yes
rdbcompression yes
dbfilename dump.rdb
dir ./
参数说明:
save
stop-writes-on-bgsave-error:在bgsave遇到error的时候是否停止持久化,默认是yes代表是,no代表不是
rdbcompression:是否压缩,默认是yes代表是,no代表不是,如果想节省CPU的话就设为no,但rdb文件会比较大
dbfilename:持久化的文件名字,默认是dump.rdb
dir:持久化的目录名字,默认是redis.conf所在的目录./
2.AOF(append-only file)模式
需要手动开启,开启后以追加的方式默认每秒钟将记录的修改操作保存到硬盘
主要配置redis.conf参数:
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
dir ./
### 以下rewrite参数,AOF模式以追加方式记录所有写操作命令到硬盘,文件会越来越大,为缓解这种问题,redis使用了BGREWRITEAOF用于删除重复多余的写命令,类似BGSAVE,rewrite的时候会删除旧的AOF文件
###
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
参数说明:
appendonly:是否启动aof,默认是no代表不启用
appendfilename:aof的文件名,默认是appendonly.aof
appendfsync:触发的间隔,默认是everysec代表每秒,还可使用always代表有改变都触发,性能差些但数据最安全,no代表让OS自己决定什么时候执行,性能最好但数据不安全
dir:持久化的目录名字,默认是redis.conf所在的目录./
no-appendfsync-on-rewrite:执行rewrite时appendfsync是否暂停,默认no代表不暂停
auto-aof-rewrite-percentage:aof rewrite触发时机,当前aof文件大小超过上一次rewrite后aof文件的百分比后触发rewrite。如200,即当前的aof文件超过上一次重写后aof文件的2倍时才会再次rewrite
auto-aof-rewrite-min-size aof文件重写最小文件大小,即最开始aof文件必须要达到这个文件时才触发,后面的每次重写就不会根据这个变量了(根据上一次重写完成之后的大小).此变量仅初始化启动redis有效.如果是redis恢复时,则lastSize等于初始aof文件大小.
在使用redis复制的设置中,强烈建议在主服务器和从服务器中打开持久性。如果不能开启的话,例如由于磁盘速度非常慢而引起的延迟问题,则应避免在重启OS后自动重新启动服务,以免数据丢失。官方对此有做举例说明:
To better understand why masters with persistence turned off configured to auto restart are dangerous, check the following failure mode where data is wiped from the master and all its slaves:
1. We have a setup with node A acting as master, with persistence turned down, and nodes B and C replicating from node A.
2. Node A crashes, however it has some auto-restart system, that restarts the process. However since persistence is turned off, the node restarts with an empty data set.
3. Nodes B and C will replicate from node A, which is empty, so they'll effectively destroy their copy of the data.
新闻标题:Redisreplication主从复制原理及配置-创新互联
当前地址:http://scjbc.cn/article/dcieoc.html