Redis主从之从Redis服务器Key不失效
来源:互联网 发布:mac在哪里新建文件夹 编辑:程序博客网 时间:2024/06/04 18:13
大家在使用redis的时候,经常会用expire来设置key的过期时间,以为某个key到期就会马上清除。如果只是在单个redis(即一个主redis)下是没有问题的。但是如果为了实现读写分离,搭建了主从服务器,那么就会遇上从库Key不失效的问题了。(使用Redis3.2以后版本的同学请忽略,3.2之后的版本已不存在以下问题)
场景重现:
主: setex name 20 Johny从:get name >> Johnyttl name >> 18// 过了一分钟...// 发现name这个key已经失效了 ttl name >> -1get name >> Johny // 仍然能获取到key的值 主:get name >> nil // 主Redis服务器上获取不到从:get name >> nil // 此时,从Redis服务器上也获取不到了
Redis key的三种过期策略
惰性删除:当读/写一个已经过期的key时,会触发惰性删除策略,直接删除掉这个过期key,很明显,这是被动的!
定期删除:由于惰性删除策略无法保证冷数据被及时删掉,所以 redis 会定期主动淘汰一批已过期的key。(在第二节中会具体说明)
主动删除:当前已用内存超过maxMemory限定时,触发主动清理策略。主动设置的前提是设置了maxMemory的值
Redis删除过期Key的源码
int expireIfNeeded(redisDb *db, robj *key) { time_t when = getExpire(db,key); if (when < 0) return 0; /* No expire for this key */ /* Don't expire anything while loading. It will be done later. */ if (server.loading) return 0; /* If we are running in the context of a slave, return ASAP: * the slave key expiration is controlled by the master that will * send us synthesized DEL operations for expired keys. * * Still we try to return the right information to the caller, * that is, 0 if we think the key should be still valid, 1 if * we think the key is expired at this time. */ if (server.masterhost != NULL) { return time(NULL) > when; } /* Return when this key has not expired */ if (time(NULL) <= when) return 0; /* Delete the key */ server.stat_expiredkeys++; propagateExpire(db,key); return dbDelete(db,key);}通过以上源码发现,4行:没有设置超时时间,则不删;7行:在"loading"时不删;16行:非主库不删;21行未到期不删。25行同步从库和文件。
所以说,在从库执行主动删除操作,或者通过惰性删除的方式触发删除key的操作,最终都不会执行成功。原因就在上面的第16行代码。
参考:https://github.com/antirez/redis/issues/1768
http://www.cnblogs.com/bridger/archive/2012/11/07/2758734.html
http://www.tuicool.com/articles/INRZFr
0 0
- Redis主从之从Redis服务器Key不失效
- Redis---服务器管理之主从服务器
- redis主从服务器配置
- Redis主从服务器配置
- redis主从服务器配置
- redis主从服务器搭建
- redis设置主从服务器
- redis设置主从服务器
- Redis主从服务器配置
- redis主从服务器
- 【Redis】Redis之主从复制
- Redis的主从架构(主从&主从从)
- redis主从数据库,从库连接不上
- redis主从数据库转换之后从库连接不上
- redis源码分析:主从模式中从服务器同步策略
- redis之主从复制
- Redis 之 主从复制
- Redis之主从同步
- react-redux-react-router直通车
- Node.js开发的WeMall 6.0正式发布
- 【排序】堆排序实现
- css排版之float浅析
- 无废话WCF入门教程五[WCF的通信模式]
- Redis主从之从Redis服务器Key不失效
- 滑动插件scroll.js
- json数据如何存入到cookie中,如何获取
- Codeforces Round #371 (Div. 1) C. Sonya and Problem Wihtout a Legend(贪心+DP)
- 无废话WCF入门教程六[一个简单的Demo]
- java读取文件后修改,并写入到另一个文件
- Flask config方法
- 问题
- MPU6050姿态融合(转载)