经验分享:防止Redis网络闪断进行主从全量同步

防止Redis网络闪断进行全量同步

当网络抖动时Redis 会发生主从全量同步,若是数据量大的话,加上传输时间,Reload时间会让业务长时间异常,能够从如下参数上进行调整,有效防止网络闪断带来的风险。redis

1.repl-timeout服务器

redis里面的repl-timeout参数值过小也将会致使复制不成功.网络

redis配置文件中对repl-timeout的参数解释以下:this

# The following option sets the replication timeout for:
#
# 1) Bulk transfer I/O during SYNC, from the point of view of slave.
# 2) Master timeout from the point of view of slaves (data, pings).
# 3) Slave timeout from the point of view of masters (REPLCONF ACK pings).

三种状况认为复制超时:spa

1)slave角度,若是在repl-timeout时间内没有收到master SYNC传输的rdb snapshot数据,
2)slave角度,在repl-timeout没有收到master发送的数据包或者ping。
3)master角度,在repl-timeout时间没有收到REPCONF ACK确认信息。

当redis检测到repl-timeout超时(默认值60s),将会关闭主从之间的链接,redis slave发起从新创建主从链接的请求。code

对于内存数据集比较大的系统,能够增大repl-timeout参数。server

2.slave ping period内存

redis slave会按期从master发送ping命令,时间间隔repl-ping-slave-period指定。ci

于是,设置参数时, repl-timeout > repl-ping-slave-period。同步

# Slaves send PINGs to server in a predefined interval. The default value is 10 seconds.
# repl-ping-slave-period 10

# It is important to make sure that this value is greater than the values pecified for repl-ping-slave-period otherwise a timeout will be detected every time there is low traffic between the master and the slave.

3.repl-backlog-size

当主服务器进行命令传播的时候,maser不只将全部的数据更新命令发送到全部slave的replication buffer,还会写入replication backlog。当断开的slave从新链接上master的时候,slave将会发送psync命令(包含复制的偏移量offset),请求partial resync。若是请求的offset不存在,那么执行全量的sync操做,至关于从新创建主从复制。

为了不网络不稳定形成的全量同步.

修改参数以下:

config set repl-timeout 240config set repl-backlog-size 524288000
相关文章
相关标签/搜索