• 【Redis】关于过期数据清除的一些策略


    这里要讨论的为过期的数据是如何被清除的,也就是网上常常讨论的过期清除策略。

    需要注意的是,redis除了会对过期的数据进行淘汰,也可以通过对内存大小进行限制,并对超出内存限制后进行数据淘汰。此时淘汰的数据未必是过期的,只是因为内存达到限制而被淘汰。需要注意一下两者的区别,数据淘汰算法包括LRU、LFU等。

    好,回归过期数据清除策略这里。redis的数据清除策略包括2种。惰性清除和定期清除。

    惰性清除很简单,redis会在用户访问数据时,如果发现数据是过期的,那么就删除这个数据,并且给用户返回相应的结果。那么redis是如何发现这个数据是过期的呢?我们知道redis默认有16个库,0~15号库,每一个库对应源码中的一个db结构体,db中有一个dict类型成员变量expire,dict,即字典,也就是哈希表,是redis底层数据结构中的一种,expire哈希表中存储的键值对,key为键,value为其对应的过期时间,expire中存储了该库中所有键的过期时间。

    那么定期清除呢?我们先贴上一段redis配置文件中的文字。

    # Redis reclaims expired keys in two ways: upon access when those keys are
    # found to be expired, and also in background, in what is called the
    # "active expire key". The key space is slowly and interactively scanned
    # looking for expired keys to reclaim, so that it is possible to free memory
    # of keys that are expired and will never be accessed again in a short time.
    #
    (redis通过2种方法清除过期键:在操作时发现这些key是过期的,还有也是
    通过后台清除的,被称作'定期清除key'。key空间被缓慢的、交互式的清除
    过期键,所以它可能会清除过期的key来释放内存,并且短时间不会被再次
    访问)
    
    # The default effort of the expire cycle will try to avoid having more than
    # ten percent of expired keys still in memory, and will try to avoid consuming
    # more than 25% of total memory and to add latency to the system. However
    # it is possible to increase the expire "effort" that is normally set to
    # "1", to a greater value, up to the value "10". At its maximum value the
    # system will use more CPU, longer cycles (and technically may introduce
    # more latency), and will tolerate less already expired keys still present
    # in the system. It's a tradeoff between memory, CPU and latency.
    #
    (清除循环作为一种默认的尝试,会尝试避免在内存中存在超过10%的过期
    Key,并且避免(过期key占用的内存)超过总内存的25%,最终将内存归还系
    统。它可以增加这种清除的尝试,通常它会被设置为1,到一个更大的值,
    最大可以达到10。它的最大值意味着系统会使用更的的cpu资源,更久的循
    环时间(理论上可能会导致延迟),并且容忍更少的现在存在于系统中的过期
    的key。它是一种在内存、CPU、延迟之间的一种平衡和折衷)
    # active-expire-effort 1
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    也就是说redis定期清除会主动的进行清除。active-expire-effort控制了每次循环的时间有多长。清除达标的条件为避免在内存中存在超过10%的过期Key,并且避免(过期key占用的内存)超过总内存的25%。不达标的话会继续循环清除,直到达标为止。

    另外一个参数hz控制了诸如清除循环、关闭超时客户端等后台任务的频率。综合控制了定期清除的行为。
    hz相关注释如下:

    # Redis calls an internal function to perform many background tasks, like
    # closing connections of clients in timeout, purging expired keys that are
    # never requested, and so forth.
    #
    # Not all tasks are performed with the same frequency, but Redis checks for
    # tasks to perform according to the specified "hz" value.
    #
    # By default "hz" is set to 10. Raising the value will use more CPU when
    # Redis is idle, but at the same time will make Redis more responsive when
    # there are many keys expiring at the same time, and timeouts may be
    # handled with more precision.
    #
    # The range is between 1 and 500, however a value over 100 is usually not
    # a good idea. Most users should use the default of 10 and raise this up to
    # 100 only in environments where very low latency is required.
    hz 10
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    参考文章:
    [1],Redis(6)删除策略(定时删除、惰性删除、定期删除)和数据逐出策略

  • 相关阅读:
    JVM分析GC日志
    同款爱心代码
    紫光同创初使用
    .NET混合开发解决方案9 WebView2控件的导航事件
    leetcode-判断是不是二叉搜索树-92
    Java进阶-异常处理
    mysql故障mysqld got signal 6,由于异常断电或者系统异常重启时MySQL没有正常退出导致MySQL无法启动
    CentOS7 FTP服务创建
    【JavaScript保姆级教程】JavaScript的介绍和简单语法
    如何将heic转换成jpg呢?
  • 原文地址:https://blog.csdn.net/gengzhihao10/article/details/132889599