redis是内存数据库,内存快要占满时,可以通过设置一定的淘汰策略去保证缓存的命中率。主要通过设置内存大小,淘汰策略,以及其key删除方式等。
通过以下配置可以设置内存大小以及内存淘汰策略:
############################## MEMORY MANAGEMENT ################################
# 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 replicas attached to an instance with maxmemory on,
# the size of the output buffers needed to feed the replicas 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 replicas 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 replicas attached it is suggested that you set a lower
# limit for maxmemory so that there is some free RAM on the system for replica
# 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 one from the following behaviors:
#
# volatile-lru -> Evict using approximated LRU, only keys with an expire set.
# allkeys-lru -> Evict any key using approximated LRU.
# volatile-lfu -> Evict using approximated LFU, only keys with an expire set.
# allkeys-lfu -> Evict any key using approximated LFU.
# volatile-random -> Remove a random key having 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
# 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: 配置Redis存储数据时指定限制的内存大小,当缓存消耗的内存超过这个数值时, 将触发数据淘汰。
maxmemory_policy:触发数据淘汰后的淘汰策略。
maxmemory_samples:随机采样的精度,也就是随即取出key的数目。该数值配置越大, 越接近于真实的LRU算法,但是数值越大,相应消耗也变高,对性能有一定影响,样本值默认为5。
内存淘汰策略设置
如上配置文件所示,redis提供八种内存淘汰策略,可以分为四类,分别是lru、lfu、random、ttl,还有一个默认不开启淘汰策略。
下面按照分类逐一介绍:
lru类:
volatile-lru -> 对设置了过期时间的key使用近似lru进行删除。
allkeys-lru -> 对所有的key使用近似lru进行删除。
lfu类:
volatile-lfu -> 对设置了过期时间的key使用近似lfu进行删除。
allkeys-lfu -> 对所有的key使用近似lfu进行删除。
random类:
volatile-random -> 对设置了过期时间的key使用随机删除。
allkeys-random -> 对所有的key使用随机删除。
ttl类:
volatile-ttl -> 删除设置了过期时间,并且距离过期时间最小的key。
默认,不做处理
noeviction -> 不做任务操作,仅在写操作时返回错误信息。
KEY删除机制
redis删除key的机制主要分为惰性删除和定期删除,当开启淘汰策略时,内存占用达到参数设置时,会触发淘汰机制,执行清理策略。
1、惰性删除
在每次获取键时,都要先判断当前键是否过期,如果过期,则删除该键;没过期则返回该键对应的值。
缺点:当前过期可以一直不被访问,则会一直占用内存空间。
2、定期删除
鉴于惰性删除的缺点,增加了定期删除。每隔一段时间,就定期的删除里面过期的键。
3、淘汰策略删除(主动删除)
当达到设置的内存上限时,触发淘汰策略。
关于LRU和LFU的算法详解,请参考数据结构与算法中的分享。
网友评论