浅谈redis的maxmemory设置以及淘汰策略

 更新时间:2017年03月27日 09:05:47   投稿:jingxian  
下面小编就为大家带来一篇浅谈redis的maxmemory设置以及淘汰策略。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

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:

关于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留出一定的可用空间是合理的。

以上这篇浅谈redis的maxmemory设置以及淘汰策略就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • redis yml配置的用法小结

    redis yml配置的用法小结

    RedisYML配置是Redis的一种配置文件格式,,对Redis的配置进行统一管理,本文就来介绍了redis yml配置的用法小结,具有一定的参考价值,感兴趣的可以了解一下
    2024-02-02
  • redis底层数据结构之ziplist实现详解

    redis底层数据结构之ziplist实现详解

    这篇文章主要为大家介绍了redis底层数据结构之ziplist实现详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-12-12
  • Redis 哨兵与集群脑裂问题及其解决

    Redis 哨兵与集群脑裂问题及其解决

    本文主要介绍了Redis 哨兵与集群脑裂问题及其解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2025-04-04
  • 一文带你了解Redis的三种集群模式

    一文带你了解Redis的三种集群模式

    Redis 的常用的集群方式主要有以下三种,分别是主从复制模式、哨兵模式、Redis-Cluster集群模式,那么下面我们就分别了解一下这三种集群模式的优点与缺点
    2023-06-06
  • Redis 如何清空所有数据

    Redis 如何清空所有数据

    这篇文章主要介绍了Redis 如何清空所有数据,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-08-08
  • 基于Redis的List实现特价商品列表功能

    基于Redis的List实现特价商品列表功能

    本文通过场景分析给大家介绍了基于Redis的List实现特价商品列表,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下
    2021-08-08
  • Redis分片集群的实现

    Redis分片集群的实现

    Redis 分片集群是一种将 Redis数据库分散到多个节点上的方式,以提供更高的性能和可伸缩性,本文主要介绍了Redis分片集群的实现,具有一定的参考价值,感兴趣的可以了解一下
    2025-04-04
  • 在redis中防止消息丢失的机制

    在redis中防止消息丢失的机制

    在项目中,由于网络问题,我们很难保证生产者发送的消息能100%到达消息队列服务器,也就是说有消息丢失的可能性,因 此,生产者就必须具有消息丢失检测和重发机制,这篇文章主要介绍了如何在redis中防止消息丢失,需要的朋友可以参考下
    2023-02-02
  • Redis+Lua脚本实现计数器接口防刷功能(升级版)

    Redis+Lua脚本实现计数器接口防刷功能(升级版)

    这篇文章主要介绍了Redis+Lua脚本实现计数器接口防刷功能,使用脚本使得set命令和expire命令一同达到Redis被执行且不会被干扰,在很大程度上保证了原子操作,对Redis实现计数器接口防刷功能感兴趣的朋友一起看看吧
    2022-02-02
  • RedisTemplate中boundHashOps的使用小结

    RedisTemplate中boundHashOps的使用小结

    redisTemplate.boundHashOps(key) 是 RedisTemplate 类的一个方法,本文主要介绍了RedisTemplate中boundHashOps的使用小结,具有一定的参考价值,感兴趣的可以了解一下
    2024-04-04

最新评论