Caching 当RAM开始填满时,Redis是如何工作的?

Caching 当RAM开始填满时,Redis是如何工作的?,caching,redis,Caching,Redis,我可能完全不感兴趣,但我对缓存存储在开始添加持久性功能之前是如何工作的理解是,项目将根据其属性过期。如果存储区开始填满可用的RAM,他们每个人都会有自己的算法使存储区中最不“重要”的密钥过期 现在我读到Redis有很多功能。但是你可以关掉它们。假设您关闭了持久性,当RAM填满时会发生什么?Redis如何决定过期的内容 我希望在没有TTL的情况下拥有大量数据,并希望确保让Redis确定哪些数据将过期是安全的。请阅读Redis文档中的章节。 有关部分: 虚拟机最大内存设置 vm max memory

我可能完全不感兴趣,但我对缓存存储在开始添加持久性功能之前是如何工作的理解是,项目将根据其属性过期。如果存储区开始填满可用的RAM,他们每个人都会有自己的算法使存储区中最不“重要”的密钥过期

现在我读到Redis有很多功能。但是你可以关掉它们。假设您关闭了持久性,当RAM填满时会发生什么?Redis如何决定过期的内容

我希望在没有TTL的情况下拥有大量数据,并希望确保让Redis确定哪些数据将过期是安全的。

请阅读Redis文档中的章节。 有关部分:

虚拟机最大内存设置 vm max memory设置指定在开始交换磁盘上的值之前,Redis可自由使用的内存量

基本上,如果未达到此内存限制,则不会交换任何对象,Redis将像往常一样处理内存中的所有对象。但是,一旦达到此限制,就会交换足够的对象,以将内存返回到刚好低于此限制的位置

交换的对象主要是那些具有最高“年龄”的对象(即,自它们未被使用以来的秒数),但对象的“可交换性”也与它在内存中大小的对数成正比。因此,尽管较旧的对象是首选对象,但较大的对象在大约相同的年龄时会首先被替换掉

警告:由于密钥无法调出,如果仅密钥使用的空间超过限制,Redis将无法执行vm max memory设置

此设置的最佳值是足够的RAM来保存数据的“工作集”。实际上,只要给Redis尽可能多的内存,交换就会更好

更新 至于Redis2.4(看起来Redis站点中的官方文档没有更新到该版本),不建议使用VM

redis.conf
说:

### WARNING! Virtual Memory is deprecated in Redis 2.4
### The use of Virtual Memory is strongly discouraged.

我不认为这个问题与虚拟内存管理有关,但更多的是关于Redis中项目的过期,这是一个完全不同的主题

与memcached相反,Redis不仅仅是一个缓存。因此,用户应该使用各种机制来选择项目逐出策略。您可以逐出所有项目,也可以仅逐出部分项目

应在配置文件中使用maxmemory和maxmemory策略参数选择常规策略,如下所述:

# Don't use more memory than the specified amount of bytes.
# When the memory limit is reached Redis will try to remove keys with an
# EXPIRE set. It will try to start freeing keys that are going to expire
# in little time and preserve keys with a longer time to live.
# Redis will also try to remove objects from free lists if possible.
#
# If all this fails, Redis will start to reply with errors to commands
# that will use more memory, like SET, LPUSH, and so on, and will continue
# to reply to most read-only commands like GET.
#
# WARNING: maxmemory can be a good idea mainly if you want to use Redis as a
# 'state' server or cache, not as a real DB. When Redis is used as a real
# database the memory usage will grow over the weeks, it will be obvious if
# it is going to use too much memory in the long run, and you'll have the time
# to upgrade. With maxmemory after the limit is reached you'll start to get
# errors for write operations, and this may even lead to DB inconsistency.
#
maxmemory <bytes>

# MAXMEMORY POLICY: how Redis will select what to remove when maxmemory
# is reached? You can select among five behavior:
#
# volatile-lru -> remove the key with an expire set using an LRU algorithm
# allkeys-lru -> remove any key accordingly to the LRU algorithm
# volatile-random -> remove a random key with 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 expire at all, just return an error on write operations
#
# Note: with all the kind of policies, Redis will return an error on write
#       operations, when there are not suitable keys for eviction.
#
#       At the date of writing this commands are: set setnx setex append
#       incr decr rpush lpush rpushx lpushx linsert lset rpoplpush sadd
#       sinter sinterstore sunion sunionstore sdiff sdiffstore zadd zincrby
#       zunionstore zinterstore hset hsetnx hmset hincrby incrby decrby
#       getset mset msetnx exec sort
#
# The default is:
#
maxmemory-policy volatile-lru

# LRU and minimal TTL algorithms are not precise algorithms but approximated
# algorithms (in order to save memory), so you can select as well the sample
# size to check. For instance for default Redis will check three keys and
# pick the one that was used less recently, you can change the sample size
# using the following configuration directive.
#
maxmemory-samples 3
#使用的内存不要超过指定的字节数。
#当达到内存限制时,Redis将尝试使用
#过期设置。它将尝试开始释放即将过期的密钥
#在很短的时间内保存密钥并延长其使用寿命。
#如果可能,Redis还将尝试从空闲列表中删除对象。
#
#如果所有这些都失败了,Redis将开始以错误回复命令
#这将使用更多内存,如SET、LPUSH等,并将继续
#回复大多数只读命令,如GET。
#
#警告:maxmemory可能是一个好主意,主要是如果您想将Redis用作
#“状态”服务器或缓存,而不是真正的数据库。当Redis被用作真实的
#数据库内存使用量将在几周内增长,如果
#从长远来看,它将占用太多内存,而您将有足够的时间
#升级。使用maxmemory,在达到限制后,您将开始
#写入操作出错,甚至可能导致数据库不一致。
#
最大内存
#MAXMEMORY策略:当使用MAXMEMORY时,Redis将如何选择要删除的内容
#到达了吗?您可以从五种行为中进行选择:
#
#volatile lru->使用lru算法删除带有过期集的密钥
#allkeys lru->根据lru算法删除任何密钥
#volatile random->删除带有过期集的随机密钥
#所有键->随机->删除随机键,任意键
#volatile ttl->删除过期时间最近的密钥(次要ttl)
#noeviction->根本不过期,只需在写操作中返回一个错误即可
#
#注意:对于所有类型的策略,Redis都会在写入时返回错误
#操作,当没有合适的键进行逐出时。
#
#在编写本文之日,这些命令是:set setnx setex append
#增量说明rpush lpushx lpushx linsert lset RPOPLPPUSH sadd
#烧结店sunion sunionstore sdiff sdiffstore zadd zincrby
#zunionstore zinterstore hset hsetnx hmset hincrby incrby decrby
#getset mset msetnx执行排序
#
#默认值为:
#
maxmemory策略易失性lru
#LRU和最小TTL算法不是精确算法,而是近似算法
#算法(为了节省内存),所以您也可以选择示例
#要检查的尺寸。例如,默认Redis将检查三个键和
#选择最近较少使用的样本,您可以更改样本大小
#使用以下配置指令。
#
maxmemory示例3
然后,可以使用以下命令设置单个项目的过期时间: 每项过期属性对于volatile-*策略很有用。 也可以使用删除过期

expiration属性会增加少量内存开销,因此只有在需要时才应使用它


最后,值得一提的是,对象的一部分不能过期,只有整个对象本身才能过期。例如,与密钥对应的整个列表或集合可以过期,但单个列表或集合项不能过期。

要么设置TTL(并让Redis为您处理过期),要么使用您自己的账龄数据发布您的项目,可能存储为ZSET of(timestamp,key)元组,您可以根据自己的需要从中执行自己的缓存清理。

Didier正确地指出了如何执行此操作。只是指出一些额外的元素(其中一个似乎从他的帖子中被省略):

  • 指定占用大部分可用内存的最大内存大小 在那上面没有
    maxmemory-samples 10