2013年8月23日星期五

redis save bgsave

性能报告邮件看到 php_error 排行老大,查了一下日志,频率大概不到10分钟爆发一次,

PHP Fatal error:  Uncaught exception 'RedisException' with message 'Redis server went away' in /diska/htdocs/client/include/class.MlRedis.php:71



看起来是提示redis 挂了。检查各个节点的情况,set get 操作都是ok 的。(PS:如果是挂了,应该一直报)

redis 是从1g 内存调整到4g 后才有此问题的,应该量大,并发压力过大,或者什么挂起的操作引起,首先检查是否是计划任务引起的,取消计划任务,报错依旧。



这种情况肯定撑不住,缓存架构需要调整,但是得先解决问题,回归到redis 上:

服务器上redis 保存策略(save 后面是时间和变更的key数):



save 900 1

save 300 10

save 60 10000



临时修改(保留一个,每10小时保存一次):

save 36000 1 

#save 300 10 

#save 60 10000 



修改后可以避免频繁dump 操作。

刚开始有想过用bgsave 执行计划任务来取代 save 操作,bgsave 官方说法貌似是不会阻塞外部连接,刚开始我也是这么尝试,但发现还是会有报错:

从以下情况来看,应该是最后时刻交换文件引起。



开一个客户端,执行bgsave 命令,再开一个终端,看bgsave 产生临时文件:



[root@APP redis]# ls -lh

total 1.7G

-rw-r--r-- 1 root root 1.4G Jul  1 23:50 dump.rdb

-rw-r--r-- 1 root root 185M Jul  2 00:11 temp-3128.rdb



[root@APP redis]# ls -lh     

total 3.4G

-rw-r--r-- 1 root root 1.4G Jul  1 23:50 dump.rdb

-rw-r--r-- 1 root root 1.4G Jul  2 00:11 temp-3128.rdb



整个临时文件生成后,temp-3128.rdb 文件应该更新到 dump.rdb ,此时会hold住文件,应该是内存中需要更新到内存的操作比较多



[root@APP redis]# ls -lh     

total 1.4G

-rw-r--r-- 1 root root 1.4G Jul  2 00:11 dump.rdb



对比一下save,开一个客户端,执行save 命令 ,回发现一开始就出错:



[root@APP redis]# ls -lh        #此时tailf 就已经可以看到错误不断的在刷了,直到save ok

total 3.3G

-rw-r--r-- 1 root root 1.4G Jul  1 19:12 dump.rdb

-rw-r--r-- 1 root root 1.4G Jul  1 20:45 dump2.rdb

-rw-r--r-- 1 root root 360M Jul  2 00:22 temp-17242.rdb



配置文件写的是save,调用的应该还是bgsave 来执行,因为top 经常可以看到多一个redis 进程出来,一会消失,这个回头再看看。



附官方对两个命令解释



save 的原文说明如下:

The SAVE commands performs a synchronous save of the dataset producing a point in time snapshot of all the data inside the Redis instance, in the form of an RDB file.

You almost never want to call SAVE in production environments where it will block all the other clients. Instead usually BGSAVE is used. However in case of issues preventing Redis to create the background saving child (for instance errors in the fork(2) system call), the SAVE command can be a good last resort to perform the dump of the latest dataset.



bgsave 的原文如下:

Save the DB in background. The OK code is immediately returned. Redis forks, the parent continues to serve the clients, the child saves the DB on disk then exits. A client my be able to check if the operation succeeded using the LASTSAVE command.

没有评论:

发表评论