redis内存淘汰策略

redis内存淘汰策略

文档

  1. redis单机安装
  2. redis集群模式 -集群搭建
  3. redis大key问题-生成大key-生成100万条测试数据

官方文档

  1. 官网操作命令指南页面:https://redis.io/docs/latest/commands/?name=get&group=string
  2. Redis cluster specification

下载地址

  1. 官网:https://redis.io/
  2. 下载列表页面:https://download.redis.io/releases/

说明

  1. 版本选择:redis-7.0.0.tar.gz
  2. 下载地址:https://download.redis.io/releases/redis-7.0.0.tar.gz

redis内存淘汰策略

安装redis
  1. 以单机版redis为例,安装redis参考文档:redis单机安装
内存淘汰策略(maxmemory-policy)
  1. 启用内存淘汰策略

    1. 当设置了 maxmemory(Redis 允许使用的内存上限),内存用满后,Redis 需要"腾空间"时会按以下策略之一淘汰 key
  2. 内存淘汰策略

    1. noeviction
      • 不淘汰,写命令(如 SETLPUSH)直接报错。
      • 适合"不能丢数据,只能报错"的场景。
    2. allkeys-lru
      • 在所有 key 中,淘汰最近最少使用的键(LRU)。
      • 常用于"纯缓存",非常常见。
    3. volatile-lru
      • 只在有过期时间的 key 集合里按 LRU 淘汰。
      • 没 TTL 的 key 永不淘汰。
    4. allkeys-random
      • 在所有 key 里随机删。
      • 简单粗暴,命中率不可控。
    5. volatile-random
      • 只在有 TTL 的 key 里随机删。
    6. volatile-ttl
      • 只在有 TTL 的 key 中,优先淘汰剩余生存时间最短的 key。
    7. allkeys-lfu
      • 在所有 key 中,淘汰访问频率最低的键(LFU)。
      • 比 LRU 更关注"长期热度"。
    8. volatile-lfu
      • 只在有 TTL 的 key 里按 LFU 淘汰。
  3. 内存淘汰策略选择

    1. 纯缓存场景(推荐最多)
      • 策略:allkeys-lru 或 新版本的 allkeys-lfu
      • 理由:所有数据都可丢,优先保留"近期/高频访问"的 key,命中率最好。
    2. 只有部分 key 允许被淘汰(缓存 + 持久数据混用)
      • 策略:volatile-lru / volatile-lfu / volatile-ttl,这些策略只关注有TTL的key
      • 做法:
        • 给"可淘汰的缓存 key"设置 TTL
        • 不可丢的数据不设置 TTL,这样只会淘汰带过期的缓存 key。
    3. 严格不能丢写入(少见,一般配合持久化存储)
      • 策略:noeviction
      • 效果:内存打满时,写请求报错而不是删数据,多用于"Redis 不做缓存、只做强一致存储"的特殊场景。
  4. 配置内存淘汰策略

    1. 在配置文件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
    2. redis-cli 中执行,立刻生效,但重启后会丢失(除非同时改了 redis.conf)。(maxmemory单位支持 kb/mb/gb/字节数)

      shell 复制代码
      CONFIG SET maxmemory 1gb
      CONFIG SET maxmemory-policy allkeys-lru

参考资料

  1. https://www.bilibili.com/video/BV13R4y1v7sP

注意事项

  1. 部分内容由AI生成
  2. 如有不对,欢迎指正!!!
相关推荐
Java开发的小李6 小时前
SpringBoot + Redis 实现分布式 Session 共享(解决多实例登录状态丢失问题)
spring boot·redis·分布式
daixin88489 小时前
cursor无法正常使用gpt5.5等模型解决方案
java·redis·cursor
小猿姐10 小时前
Redis Kubernetes Operator 实测:三个方案的真实差距
redis·容器·kubernetes
aLTttY15 小时前
Spring Boot + Redis 实现接口防抖与限流实战指南
spring boot·redis·junit
Lyyaoo.16 小时前
TreadLocal和TreadLocalMap
android·java·redis
为美好的生活献上中指17 小时前
本地虚拟机部署redis集群
前端·redis·ubuntu·bootstrap·html·redis集群
coderlin_18 小时前
Langgraph项目三 agent搭建
java·数据库·redis
XiYang-DING19 小时前
【Java EE】锁策略、锁升级、锁消除和锁粗化
java·redis·java-ee
Devin~Y20 小时前
大厂Java面试实录:Spring Boot/Cloud + Redis/Kafka + JWT + RAG/Agent(小Y翻车版)
java·spring boot·redis·spring cloud·kafka·spring security·jwt
以为你知道啊21 小时前
mini-job极简分布式延迟任务队列 — 基于 Redis,支持 Cron 周期任务、异步协程和多执行器
redis·分布式·junit