Redis内存回收策略

 更新时间:2021年11月24日 16:03:28   作者:小小工匠  
Redis也会因为内存不足而产生错误?,?也可能因为回收过久而导致系统长期的停顿,因此掌握执行回收策略十分有必要,具有一定的参考价值,感兴趣的可以了解一下
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

概述

Redis也会因为内存不足而产生错误 , 也可能因为回收过久而导致系统长期的停顿,因此掌握执行回收策略十分有必要。在 Redis 的配置文件中,当 Redis 的内存达到规定的最大值时,允许配置 6 种策略中的一种进行淘汰键值,并且将一些键值对进行回收。

maxmemory-policy 参数

# Set a memory usage limit to the specified amount of bytes.
# When the memory limit is reached Redis will try to remove keys
# according to the eviction policy selected (see maxmemory-policy).
#
# If Redis can't remove keys according to the policy, or if the policy is
# set to 'noeviction', Redis will start to reply with errors to commands
# that would use more memory, like SET, LPUSH, and so on, and will continue
# to reply to read-only commands like GET.
#
# This option is usually useful when using Redis as an LRU or LFU cache, or to
# set a hard memory limit for an instance (using the 'noeviction' policy).
#
# 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>

# MAXMEMORY POLICY: how Redis will select what to remove when maxmemory
# is reached. You can select among five behaviors:
#
# volatile-lru -> Evict using approximated LRU among the keys with an expire set.
# allkeys-lru -> Evict any key using approximated LRU.
# volatile-lfu -> Evict using approximated LFU among the keys with an expire set.
# allkeys-lfu -> Evict any key using approximated LFU.
# volatile-random -> Remove a random key among the ones 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 evict anything, just return an error on write operations.
#
# LRU means Least Recently Used
# LFU means Least Frequently Used
#
# Both LRU, LFU and volatile-ttl are implemented using approximated
# randomized algorithms.
#
# Note: with any of the above policies, Redis will return an error on write
#       operations, when there are no suitable keys for eviction.
#
#       At the date of writing these commands are: set setnx setex append
#       incr decr rpush lpush rpushx lpushx linsert lset rpoplpush sadd
#       sinter sinterstore sunion sunionstore sdiff sdiffstore zadd zincrby
#       zunionstore zinterstore hset hsetnx hmset hincrby incrby decrby
#       getset mset msetnx exec sort
#
# The default is:
#
# maxmemory-policy noeviction

主动清理策略

主动清理策略在Redis 4.0 之前一共实现了 6 种内存淘汰策略,在 4.0 之后,又增加了 2 种策略,总共8种:

【针对设置了过期时间的key做处理】

  • volatile-ttl:在筛选时,会针对设置了过期时间的键值对,根据过期时间的先后进行删除,越早过期的越先被删除。
  • volatile-random:就像它的名称一样,在设置了过期时间的键值对中,进行随机删除。
  • volatile-lru:会使用 LRU 算法筛选设置了过期时间的键值对删除。
  • volatile-lfu:会使用 LFU 算法筛选设置了过期时间的键值对删除

【 针对所有的key做处理】

  • allkeys-random:从所有键值对中随机选择并删除数据。
  • allkeys-lru:使用 LRU 算法在所有数据中进行筛选删除。
  • allkeys-lfu:使用 LFU 算法在所有数据中进行筛选删除。

【 不处理 (默认)】

noeviction:不会剔除任何数据,拒绝所有写入操作并返回客户端错误信息"(error) OOM command not allowed when used memory",此时Redis只响应读操作。

Redis 在默认情况下会采用 noeviction 策略。换句话说,如果内存己满 , 则不再提供写入操作 , 而只提供读取操作 。 显然这往往并不能满足我们的要求,因为对于互联网系统而言 , 常常会涉及数以百万甚至更多的用户 , 所以往往需要设置回收策略。

策略选择

LRU 算法(Least Recently Used,最近最少使用):淘汰很久没被访问过的数据,以最近一次访问时间作为参考

LFU 算法(Least Frequently Used,最不经常使用):淘汰最近一段时间被访问次数最少的数据,以次数作为参考

需要指出的是 : LRU 算法或者 TTL 算法都是不是很精确算法,而是一个近似的算法。 Redis 不会通过对全部的键值对进行比较来确定最精确的时间值,从而确定删除哪个键值对 , 因为这将消耗太多的时间 , 导致回收垃圾执行的时间太长 , 造成服务停顿.

当存在热点数据时,LRU的效率很好,但偶发性的、周期性的批量操作会导致LRU命中率急剧下降,缓存污染情况比较严重。这时使用LFU可能更好点

根据自身业务类型,配置好maxmemory-policy(默认是noeviction),推荐使用volatile-lru。

maxmemory-sample

而在Redis 的默认配置文件中 , 存在着参数 maxmemory-sample

# LRU, LFU and minimal TTL algorithms are not precise algorithms but approximated
# algorithms (in order to save memory), so you can tune it for speed or
# accuracy. For default Redis will check five keys and pick the one that was
# used less recently, you can change the sample size using the following
# configuration directive.
#
# The default of 5 produces good enough results. 10 Approximates very closely
# true LRU but costs more CPU. 3 is faster but not very accurate.
#
# maxmemory-samples 5

当设置 maxmemory-samples越大,则 Redis 删除的就越精确,但是与此同时带来不利的是, Redis 也就需要花更多的时去计算匹配更为精确的值 。

回收超时策略的缺点是必须指明超时的键值对 ,这会给程序开发带来一些设置超时的代码,无疑增加了开发者的工作量。

对所有的键值对进行回收,有可能把正在使用的键值对删掉,增加了存储的不稳定性。

对于垃圾回收的策略,还需要注意的是回收的时间,因为在 Redis 对垃圾的回收期间, 会造成系统缓慢。

因此,控制其回收时间有一定好处,只是这个时间不能过短或过长。过短则会造成回收次数过于频繁,过长则导致系统单次垃圾回收停顿时间过长,都不利于系统的稳定性,这些都需要设计者在实际的工作中进行思考 。

如果不设置最大内存,当 Redis 内存超出物理内存限制时,内存的数据会开始和磁盘产生频繁的交换 (swap),会让 Redis 的性能急剧下降。

到此这篇关于 Redis内存回收策略的文章就介绍到这了,更多相关 Redis内存回收内容请搜索程序员之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持程序员之家!?

相关文章

  • Redis的9种数据类型用法解读

    Redis的9种数据类型用法解读

    这篇文章主要介绍了Redis的9种数据类型用法及说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-02-02
  • 如何利用Redis分布式锁实现控制并发操作

    如何利用Redis分布式锁实现控制并发操作

    这篇文章主要介绍了如何利用Redis分布式锁实现控制并发操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • Redis上实现分布式锁以提高性能的方案研究

    Redis上实现分布式锁以提高性能的方案研究

    这篇文章主要介绍了Redis上实现分布式锁以提高性能的方案研究,其中重点需要理解异步算法与锁的自动释放,需要的朋友可以参考下
    2015-12-12
  • Redis 中的布隆过滤器的实现

    Redis 中的布隆过滤器的实现

    这篇文章主要介绍了Redis 中的布隆过滤器的实现,详细的介绍了什么是布隆过滤器以及如何实现,非常具有实用价值,需要的朋友可以参考下
    2018-10-10
  • redis命令行查看中文不乱码的方法(十六进制字符串处理)

    redis命令行查看中文不乱码的方法(十六进制字符串处理)

    这篇文章主要给大家介绍了关于redis命令行查看中文不乱码的方法,其中详细介绍了十六进制字符串处理的相关资料,文中给出了详细的示例代码,供大家参考学习,下面随着小编来一起学习学习吧。
    2017-10-10
  • 基于redis集群设置密码的实例

    基于redis集群设置密码的实例

    今天小编就为大家分享一篇基于redis集群设置密码的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-05-05
  • Redis数据结构之跳跃表使用学习

    Redis数据结构之跳跃表使用学习

    这篇文章主要为大家介绍了Redis数据结构之跳跃表使用学习,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-07-07
  • 详解Redis使用认证密码登录

    详解Redis使用认证密码登录

    本篇文章主要介绍了详解Redis使用认证密码登录 。启用Redis的认证密码可以增加Redis服务器的安全性。有兴趣的可以了解下
    2017-06-06
  • redis缓存穿透解决方法

    redis缓存穿透解决方法

    在本篇文章里小编给大家分享了关于redis缓存穿透的解决方法以及相关实例内容,需要的朋友们学习下。
    2019-06-06
  • 了解Redis常见应用场景

    了解Redis常见应用场景

    Redis是一个key-value存储系统,现在在各种系统中的使用越来越多,大部分情况下是因为其高性能的特性,被当做缓存使用,这里介绍下Redis经常遇到的使用场景
    2021-06-06

最新评论

?


http://www.vxiaotou.com