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. 如有不对,欢迎指正!!!
相关推荐
Rain的Java大神之路5 小时前
如何快速上传10G文件
java·spring boot·redis·后端·mysql·spring cloud·面试
Wang's Blog5 小时前
Java 接入Redis: 通用命令与键管理
java·服务器·redis
Wang's Blog5 小时前
Java 接入Redis: 列表集合与有序集合操作命令
java·服务器·redis
行百里er15 小时前
5 分钟跑起 Redis(Docker 版)
redis·后端·docker
福大大架构师每日一题15 小时前
redis 8.10.2发布:安全修复集中落地,事务 ACL、集群总线与模块稳定性全面加固
redis
Pioneer0000115 小时前
我用 Redis + 网关做多模型 API 路由:缓存命中率 95%+ 的工程实践
人工智能·redis·后端·缓存·性能优化·架构
Hui Baby17 小时前
Redis Streams 发布订阅
redis
hweiyu001 天前
Redis命令:MSETNX
redis·缓存
浅念-1 天前
Redis基础详解:单线程模型、String与Hash数据结构
服务器·数据库·redis·sql·mysql·nosql数据库·nosql
张某布响丸辣1 天前
附件下载的安全边界:路径白名单、NAS 哨兵文件与 Redis Lua 一次性令牌
java·redis·redis lua·nas哨兵