redis内存淘汰策略
文档
官方文档
下载地址
说明
- 版本选择:
redis-7.0.0.tar.gz - 下载地址:https://download.redis.io/releases/redis-7.0.0.tar.gz
redis内存淘汰策略
安装redis
- 以单机版redis为例,安装redis参考文档:redis单机安装
内存淘汰策略(maxmemory-policy)
-
启用内存淘汰策略
- 当设置了
maxmemory(Redis 允许使用的内存上限),内存用满后,Redis 需要"腾空间"时会按以下策略之一淘汰 key
- 当设置了
-
内存淘汰策略
noeviction:- 不淘汰,写命令(如
SET、LPUSH)直接报错。 - 适合"不能丢数据,只能报错"的场景。
- 不淘汰,写命令(如
allkeys-lru:- 在所有 key 中,淘汰最近最少使用的键(LRU)。
- 常用于"纯缓存",非常常见。
volatile-lru:- 只在有过期时间的 key 集合里按 LRU 淘汰。
- 没 TTL 的 key 永不淘汰。
allkeys-random:- 在所有 key 里随机删。
- 简单粗暴,命中率不可控。
volatile-random:- 只在有 TTL 的 key 里随机删。
volatile-ttl:- 只在有 TTL 的 key 中,优先淘汰剩余生存时间最短的 key。
allkeys-lfu:- 在所有 key 中,淘汰访问频率最低的键(LFU)。
- 比 LRU 更关注"长期热度"。
volatile-lfu:- 只在有 TTL 的 key 里按 LFU 淘汰。
-
内存淘汰策略选择
- 纯缓存场景(推荐最多)
- 策略:
allkeys-lru或 新版本的allkeys-lfu - 理由:所有数据都可丢,优先保留"近期/高频访问"的 key,命中率最好。
- 策略:
- 只有部分 key 允许被淘汰(缓存 + 持久数据混用)
- 策略:
volatile-lru/volatile-lfu/volatile-ttl,这些策略只关注有TTL的key - 做法:
- 给"可淘汰的缓存 key"设置 TTL
- 不可丢的数据不设置 TTL,这样只会淘汰带过期的缓存 key。
- 策略:
- 严格不能丢写入(少见,一般配合持久化存储)
- 策略:
noeviction - 效果:内存打满时,写请求报错而不是删数据,多用于"Redis 不做缓存、只做强一致存储"的特殊场景。
- 策略:
- 纯缓存场景(推荐最多)
-
配置内存淘汰策略
-
在配置文件
redis.conf中配置,改完后重启 Redis 生效。(maxmemory单位支持kb/mb/gb/字节数)properties############################## 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 1gb # 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, when there are no suitable keys for # eviction, Redis will return an error on write operations that require # more memory. These are usually commands that create new keys, add data or # modify existing keys. A few examples are: SET, INCR, HSET, LPUSH, SUNIONSTORE, # SORT (due to the STORE argument), and EXEC (if the transaction includes any # command that requires memory). # # The default is: # # maxmemory-policy noeviction maxmemory-policy allkeys-lru -
在
redis-cli中执行,立刻生效,但重启后会丢失(除非同时改了redis.conf)。(maxmemory单位支持kb/mb/gb/字节数)shellCONFIG SET maxmemory 1gb CONFIG SET maxmemory-policy allkeys-lru
-
参考资料
注意事项
- 部分内容由AI生成
- 如有不对,欢迎指正!!!