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

相关推荐

  • 78 款 Markdown 工具

    Markdown 是一种易写易读的文本标记语言。本文介绍了编写、查看 Markdown 的78种工具,涵盖桌面端、移动端和 web 端。     Markable 强大的在线 Markdown 编辑器,支持语法高亮、自动缩进、高亮所在行、显示行数、即时预览。它支持将 HTML 转换成 Markdown。 可以配合 Evernote、Dro…

    2015-03-26
  • N21_第8周_网络

    N21_第8周_网络 作业题目: 1、请描述网桥、集线器、二层交换机、三层交换机、路由器的功能、使用场景与区别。     网桥:(Bridge)也称桥接器,是连接两个局域网的存储转发设备,用它可以完成具有相同或相似体系结构网络系统的连接。       &nbs…

    Linux干货 2016-10-09
  • 文件查找

    文件查找     一.文件查找locate和find 1.     locate命令 非实时查找( 数据库查找) 查询系统上预建的文件索引数据库 依赖于事先构建 的索引 索引的构建是在系统较为空闲时自动进行( 周期性任务) 管理员手动更新数据库(updatedb) 索引构建过程需要遍历整个根文件系统…

    Linux干货 2017-08-12
  • N25-第10周博客作业

    请详细描述CentOS的启动流程     启动第一步–加载BIOS         当打开计算机电源,计算机会首先加载BIOS信息,BIOS信息是如此的重要,以至于计算机必须在最开始就找到它,这是因为BIOS中包含了CPU的相关信息、设备启动顺序信息、硬盘信息、内存信息…

    Linux干货 2017-03-10
  • mariadb之再次演练

    架构图如下: 1.按照架构图所示,准备机器,做好时间同步,主机名解析 192.168.42.150 node1 [proxySQL keepalived]192.168.42.151 node2 [proxySQL keepalived]192.168.42.152 node3 [mysql-master wha]192.168.42.153 node4 […

    Linux干货 2016-03-06
  • ntp时间服务器

    前言   ntp(Network Time protocol)是网络时间协议,是用来使网络中的各个计算机时间同步的一种协议。 ntp配置  1)ntp是由ntp软件提供,如果没有可以使用yum进行安装  2)ntp配置文件介绍:/etc/ntp.conf   利用restrict来管理权限控制;语法格式:restric…

    Linux干货 2015-06-24