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 19:00

相关推荐

  • 怎么删除邮件?

    linux邮件位置一般在/var/spool/mail/root下,如果想查看邮件,在屏幕上输入mail就可以查看了,输入之后会出现&符号,你想看哪一封邮件,直接在后面跟对应的数字就好了。 如果你感觉自己的邮件太多了,比较占内存,那就删了吧。 d 删除当前邮件,指针并下移。   d 3 删除编号为3 的邮件。   d1-100 删…

    2017-07-22
  • shell脚本编程基础之二(if、case、for、while、until、continue、break语句使用)

    在shell脚本编程中,我们可以根据命令的状态结果,判断要不要执行下一步,但是有时候要判断的问题不止一个,甚至对问题本身都要做判断;同时问题的结果有时也不止一个,这时要借助简单的逻辑与和逻辑或,就显得很无力;要完成复杂的任务,需要借助一定的流程控制:顺序执行、选择执行、循环执行、同时在脚本执行过程中,有用户交互输入的需; if语句 case语句 for语句 …

    Linux干货 2016-08-21
  • 永远不要让自己停下

    曾几何时,我还是刚出象牙塔的小男孩。想想现在,经过4年的洗礼,我不再是小男孩了。可是,我却发现自己变得越来越懒,守着那丁点的知识。 我早已坐吃山空而不自知,早已落后于时代的趋势而不自知。时代需要一个勤奋而专注的我,需要一个不懈怠的我。 如果,我不继续前进,等待我的只有堕落。逆水行舟不进则退,不逼自己一把,永远不知道自己的潜力有多大。 我希望说,再见,懈怠而懒…

    Linux干货 2016-10-07
  • 马哥教育网络班22期+第九周课程练习

    1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别统计这两类用户的个数;通过字符串比较来实现; #!/bin/bash declare -i sum1=0 declare -i sum2=0 shells=$(cat /etc/p…

    Linux干货 2016-11-28
  • 马哥教育网络班N22期+第5周课程练习

    马哥教育网络班N22期+第5周课程练习 1. 显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行;  grep -E "^[#][[:space:]]+[^[:space:]].*" /etc/rc.d/rc.sysinit 2. 显示…

    Linux干货 2016-09-19
  • 进程管理常用命令

    Linux系统上的进程查看及管理工具:pstree,ps,pidof,pgrep,top,htop,glances,pmap,vmstat,dstat,kill,pkill,job,bg,fg,nohup,nice,renice,killall,…CentOS 5 :SysV initCentOS 6 :upstartCentOS 7 :syst…

    Linux干货 2017-05-17