设为首页 收藏本站
查看: 1148|回复: 0

[经验分享] redis的maxmemory设置以及淘汰策略介绍

[复制链接]

尚未签到

发表于 2017-12-21 16:37:28 | 显示全部楼层 |阅读模式
  转载地址:http://www.iyunv.com/database/201507/420889.html
  redis的maxmemory参数用于控制redis可使用的最大内存容量。如果超过maxmemory的值,就会动用淘汰策略来处理expaire字典中的键。
  关于redis的淘汰策略:
  Redis提供了下面几种淘汰策略供用户选择,其中默认的策略为noeviction策略:
  ·         noeviction:当内存使用达到阈值的时候,所有引起申请内存的命令会报错。
  ·         allkeys-lru:在主键空间中,优先移除最近未使用的key。
  ·         volatile-lru:在设置了过期时间的键空间中,优先移除最近未使用的key。
  ·         allkeys-random:在主键空间中,随机移除某个key。
  ·         volatile-random:在设置了过期时间的键空间中,随机移除某个key。
  ·         volatile-ttl:在设置了过期时间的键空间中,具有更早过期时间的key优先移除。
  PS:
DSC0000.png

  关于maxmemory的设置,如果redis的应用场景是作为db使用,那不要设置这个选项,因为db是不能容忍丢失数据的。
  如果作为cache使用,则可以启用这个选项(其实既然有淘汰策略,那就是cache了。。。)
  但是在集群环境下(尤其是有多个slavers的情形),maxmeomory的值并不是实际redis使用的内存,这个选项值并没有包括slaver的output buffer。
  redis早期版本出过一个bug,在多个slaver的情形下,设置了maxmemory值,同时设定了淘汰策略,会造成master上的数据被渐渐擦除。
  antirez先生给出了这个问题的原因:
The issue happens for the following reason: Redis reached the configured limit, so it tries to expire keys.Evicting keys turns into explicit DELs sent to slaves, since masters control the eviction of slaves for well known reasons.But this way if there are enough slaves, emitting the protocol in the output buffers will actually take more memory than the amount freed removing keys...So the key eviction process starts to enter into an infinite loop.Up to a given point the fact that there is a static buffer part in the output queue of every client (including slaves) mitigate this in certain conditions, but once Redis can't use the output buffer but must use the queue of objects the infinite loop is triggered.  简单说来,删除过期键,需要产生del命令发送给slaver,如果slaver足够多,output buffer将会占用足够多的内存,导致更多的键过期,如此往复,陷入了无线循环。
  解决方案有多种,比如output buffer可以不计入maxmemory。
  因此,在3.0版本的配置说明中有了以下表述:
# WARNING: If you have slaves attached to an instance with maxmemory on,# the size of the output buffers needed to feed the slaves are subtracted# from the used memory count, so that network problems / resyncs will# not trigger a loop where keys are evicted, and in turn the output# buffer of slaves is full with DELs of keys evicted triggering the deletion# of more keys, and so forth until the database is completely emptied.## In short... if you have slaves attached it is suggested that you set a lower# limit for maxmemory so that there is some free RAM on the system for slave# output buffers (but this is not needed if the policy is 'noeviction').## maxmemory <bytes></bytes>  由此可见,如果有slaver的情况下,建议适当调低maxmemory,给output buffer留出一定的可用空间是合理的。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-426540-1-1.html 上篇帖子: redis配置密码 下篇帖子: redis 通配符 批量删除key
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表