redis的maxmemory使用达到上限

最近在项目中遇到redis的maxmemory达到使用上限,特地又回过头来看了下关于maxmemory的配置。


关于maxmemory的设置,redis.config说明如下:

如果设定了maxmemory,使用redis的时候,redis的内存使用量不能超过设定的值,一旦redis的内存使用量达到了最大值,redis将会尝试按照选择的eviction policy(回收策略)移除相应的keys

如果redis不能根据回收策略移除keys,或者回收策略设置成noeviction,那么redis将对需要写操作的命令返回错误信息,如SET,LPUSH操作,对GET这样的只读操作会继续响应。

在redis.conf中给出了一个警告信息,如果一个设置了maxmemory的实例连接了从redis,那么预留给redis使用的内存除了redis实例本身占用的内存外还要加上用于主从复制的输出缓冲区大小(the output buffers need to feed the slaves),这样,才不会触发移除keys的死循环,因为当内存达到最大内存限制后,会根据eviction policy移除相应的keys,这时,从redis也会同步移除keys操作,最终所有数据都被清空。

总之,一句话,对一个连接了从redis的redis实例设置maxmemory时,建议设置一个较高的值,使系统有多余的内存用于主从同步,当然,如果eviction policy设置成noevcition,则不需要这么设置。

# volatile-lru -> remove the key with an expire set using an LRU algorithm
# allkeys-lru -> remove any key according to the LRU algorithm
# volatile-random -> remove a random key with an expire set
# allkeys-random -> remove a random key, any key
# volatile-ttl -> remove the key with the nearest expire time (minor TTL)
# noeviction -> don't expire at all, just return an error on write operations 

查看配置 ,我们线上redis_version:2.6.14,最大内存设置的4g,没有设置MAXMEMORY POLICY,采用的默认配置。

maxmemory 4g

# The default is:
# maxmemory-policy volatile-lru 


查看info,最大时使用了3.73G

# Memory
used_memory:37306816
used_memory_human:35.58M
used_memory_rss:38125568
used_memory_peak:3999999912
used_memory_peak_human:3.73G
used_memory_lua:31744
mem_fragmentation_ratio:1.02
mem_allocator:jemalloc-3.2.0 


通过Redis-live查看历史情况如下:


可以发现,达到maxmemory后,被key设置的回收策略(volatile-lru)回收了。


后来,查看新部署的redis集群,采用的redis_version:3.0.5 ,默认的回收策略变更了。

# The default is:
# maxmemory-policy noeviction 

当再次set值的时候报错:

(error) OOM command not allowed when used memory > 'maxmemory'. 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值