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. 如有不对,欢迎指正!!!
相关推荐
轻刀快马7 小时前
Redis 架构进阶:全景解析 RDB、AOF 与混合持久化机制
redis
Albert Edison11 小时前
【Redis】Centos7.9 安装 Redis 5 教程
数据库·redis·缓存
Steadfast_GG12 小时前
Redis中的通用命令
redis·缓存
小二·12 小时前
Redis 内存溢出(OOM)排查与恢复实战
数据库·redis·bootstrap
pqk6V6Vep12 小时前
Redis 分布式锁进阶第一篇讲解
数据库·redis·分布式
giaz14n9X12 小时前
Redis 分布式锁进阶第六十一篇
数据库·redis·分布式
JAVA面经实录91715 小时前
Redis 知识体系(完整版)
java·redis·nosql数据库·nosql
ManageEngine卓豪16 小时前
数据库可观测性:MySQL与Redis监控核心监控指标与全栈运维解决方案
数据库·redis·mysql·数据库性能·数据库监控
真实的菜17 小时前
Redis 从入门到精通(十四):Redis 7.x 新特性全解 —— 系列收官之作
数据库·redis·缓存
小小工匠18 小时前
Redis - 缓冲区管理:避免溢出引发的“惨案“
redis·性能优化·集群·内存管理·持久化