redis主从复制(1)— 慢查询导致复制中断

redis的异常行为是一件令人头疼的问题。redis提供的错误日志只提供了一些server的运行状态信息,而没有server在故障现场的操作日志,比如执行了什么命令,命令操作对象的数据结构信息。如果redis也有mysql的slow-log,那么很多性能和故障问题也很容易处理。
1、redis rdb
在redis2.8版本以前,redis主从数据复制在生产上存在一个严重的问题。当主从连接断开时,redis slave会请请求master重传数据,进行全量的rdb重传。对于大数据集和高并发的redis实例,会引起redis阻塞相当长的时间,甚至导致redis不能处理任何客户端的请求。(redis latency)。

1)redis开启bgsave,调用fork创建一个子进程,然后对数据集进行copy-on-write。fork()产生的子进程需要复制父进程的地址空间(页表)。比如在页大小为4KB的linux系统上,24GB的redis数据集,需要一个24GB/4KB*8=48MB的页表。子进程复制页表(read-only状态)的时候,redis主进程(父进程)不能处理写入请求。bgsave和aofrewrite的实现过程可以查看rdb.c和aof.c文件.

2)如果数据集较大,系统内存不是很充足,rdb会导致swap,redis性能非常低下。

2、2.8版本出现复制中断现象
而2.8版本实现了增量复制,利用缓冲区实现了断点续传,避免出现rdb重传。虽然psync不成功的情况,也会进行rdb重传。
下文要讲述的问题与rdb/aof并无多大关系,而下面的日志是在redis2.8.8上发生的复制中断的问题。
具体日志信息如下:
error log of slave

[181852] 12 Mar 20:31:34.695 # MASTER timeout: no data nor PING received...
[181852] 12 Mar 20:31:34.696 # Connection with master lost.
[181852] 12 Mar 20:31:34.696 * Caching the disconnected master state.
[181852] 12 Mar 20:31:34.697 * Connecting to MASTER 192.168.0.197:6384
[181852] 12 Mar 20:31:34.697 * MASTER <-> SLAVE sync started
[181852] 12 Mar 20:31:37.697 * Non blocking connect for SYNC fired the event.
[181852] 12 Mar 20:31:37.698 * Master replied to PING, replication can continue...
[181852] 12 Mar 20:31:37.698 * Trying a partial resynchronization (request 
737d5014a4916a0fcabfcbeef337fdc0d552a44a:64363916219).
[181852] 12 Mar 20:31:37.699 * Successful partial resynchronization with master.
[181852] 12 Mar 20:31:37.700 * MASTER <-> SLAVE sync: Master accepted a Partial Resynchronization.

errorerror log of master

[189366] 12 Mar 20:31:35.788 # Connection with slave 192.168.0.174:6384 lost.
[189366] 12 Mar 20:31:37.698 * Slave asks for synchronization
[189366] 12 Mar 20:31:37.699 * Partial resynchronization request accepted. Sending 135901 bytes of backlog starting from 
offset 64363916219.

3、客户端异常:

在应用客户端发生了什么呢?开发告诉我,应用程序端(PHP)出现大量的“Redis server went away”。

4、master-slave的基本行为
1)首先,slave(第34s)日志显示没有收到master的数据或者ping信息,slave重新连接master。
2)接着,master(第35s)日志提示从库断开了连接。
3)在第37秒,主从的日志均显示复制连接重现建立成功,并开始psync增量复制。

5、寻找问题的蛛丝马迹
1)机器上的其它的主从复制都很正常,没有复制断开的状况,排除了网络故障原因。
2)master机器上的流量突然上升,而从库的网卡流量比较正常。这说明master突然有数据大量读取。
3)master自身堵住了,没有rdb和aofrewr。那么只有redis的慢查询了,通过slowlog get 10。果不其然,存在大量的keys*操作,导致了复制中断。

“Redis server went away”的原因也也很明显,大量的慢查询导致redis server堵塞,不能立即响应redis客户端的请求而出现连接超时。

附加话题:redis复制超时
https://github.com/antirez/redis/issues/918

Since the master -> slave channel is one-way in Redis, currently replication timeout is only up to the slave. Basically this is what happens:
 
The master normally sends the replication stream to the slave.
The master also PINGs the slave from time to time, to make sure that even when no new data is available to be transmitted to the slave, the slave still get some news from the master.
Because of this, there is always data going from master to slave, so the slave will be able to detect when the connection is down and reconnect if possible.
 
On the master side, if we don't get an error from the socket, we keep sending data. The output buffer can get too large, but the limits in Redis 2.6 will detect that and close the connection with the slave.

转自:http://mdba.cn/?p=787

原创文章,作者:s19930811,如若转载,请注明出处:http://www.178linux.com/2474

(1)
s19930811s19930811
上一篇 2016-04-12 18:58
下一篇 2016-04-12

相关推荐

  • rpm数据库损坏 不能使用正常使用yum的处理方法

           不知道有没有跟我一样的小伙伴,在使用yum安装软件时,发现无法正常使用yum安装软件包,下面出现几句英语提示。在使用rpm时也无法正常的使用。其实 是rpm数据库崩溃导致的 下面就简单的介绍一下怎么回复,让其正常的工作,很简单几个命了即可。 1. 出现错误提示,这时我们需要认真的阅读下面的提示,不要乱找原…

    Linux干货 2017-06-13
  • shell中的位置参数变量

    要了解位置变量,首先要知道什么是变量?  变量就是让某一个特定的字符串来代表不固定的内容,简单理解就是用一个简单的“字眼”来替代另一个比较复杂或者容易变动的数据。 而什么又是位置变量呢?  位置变量就是bash将传递给脚本的参数,按照位置不同,保存在不同的位置变量中,以便于脚本调用这些变量。 位置参数变量: $1, $2, ……

    Linux干货 2016-08-12
  • Linux磁盘管理进阶

    Linux磁盘管理进阶 dd命令 SWAP与移动介质 磁盘配额 RAID dd命令 dd – convert and copy a file Copy a file, converting and formatting according&…

    Linux干货 2016-09-05
  • centos7 搭建SVN服务器

    运维常见的工作就是日常软件的安装和维护,SVN虽然被Git侵占了市场份额,但是仍然是高效简捷的源码管理工具。从日常软件的安装部署开始,熟悉一些Linux的常用命令。

    Linux干货 2018-03-26
  • iptables:iptables工具详解

    之前的博客聊了关于iptables防火墙工作原理的相关介绍, 本片将详细介绍iptables的使用。 一、 iptables 查看链表,创建链表,类命令     1. iptables [-t table] -N chain : 创建一条自定义规则的链      #&n…

    Linux干货 2015-08-06
  • shell脚本编写-5

      1、 数组 变量:存储单个元素的内存空间 数组:存储多个元素的连续的内存空间,相当于多个变量的集合 数组名和索引 索引:编号从0 开始,属于数值索引 注意:索引可支持使用自定义的格式,而不仅是数值格式,即为关联索引,bash4.0版本之后开始支持。而且bash 的数组支持稀疏格式(索引不连续) 1)、定义数组 声明数组: declare -a …

    Linux干货 2016-09-01