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. 如有不对,欢迎指正!!!
相关推荐
blurblurblun2 小时前
Redis底层专题(1)------ sds字符串
数据库·redis·缓存
csdn_aspnet2 小时前
技术难题:高并发场景下的“超卖”现象(库存一致性)
redis·lua·秒杀
ZZZKKKRTSAE3 小时前
快速上手NoSql数据库Redis集群
redis·nosql
星轨初途3 小时前
C++入门基础指南
开发语言·c++·经验分享·redis
码云数智-大飞4 小时前
分布式锁的三种实现方案:Redis、ZooKeeper与数据库的深度对比与选型指南
数据库·redis·分布式
匀泪4 小时前
云原生(Redis配置)
数据库·redis·缓存
shuair4 小时前
redis执行lua脚本
数据库·redis·lua
indexsunny4 小时前
互联网大厂Java面试实战:微服务与Spring Boot在电商场景下的应用解析
java·spring boot·redis·docker·微服务·kubernetes·oauth2
June`5 小时前
mini-redis项目之Resp协议
数据库·redis