当内存耗尽时,Redis会做什么?

时间:2011-02-21 16:32:10

标签: nosql redis

这可能是一个简单的问题,但我很难找到答案。 Redis 2.0如何处理最大分配内存的耗尽?它如何决定要删除哪些数据或将哪些数据保留在内存中?

7 个答案:

答案 0 :(得分:82)

如果您打开了虚拟内存功能(我认为是2.0或2.2版本中的新功能),那么当内存耗尽时,Redis会开始将“不那么频繁使用”的数据存储到磁盘上。

如果Redis中的虚拟内存被禁用,则看起来好像操作系统的虚拟内存开始耗尽(即交换),性能大幅下降。

现在,您还可以使用maxmemory参数配置Redis,以防止Redis使用更多内存(默认设置)。

当达到maxmemory时,较新版本的Redis有各种策略:

  • volatile-lru删除其中的一个键 那些有过期集,试图 删除最近未使用过的密钥。
  • volatile-ttl删除其中的一个键 那些有过期集,试图 短暂的剩余时间删除键 生活。
  • volatile-random删除a 带有随机密钥的随机密钥 到期集。
  • allkeys-lru喜欢 volatile-lru,但会删除所有 钥匙,普通钥匙或钥匙 过期集。
  • allkeys随机 像volatile-random,但会删除 各种键,都是普通键 和过期设置的密钥。

如果您选择仅删除设置了EXPIRE的键的策略,那么当Redis内存不足时,程序似乎只是中止malloc()操作。也就是说,如果您尝试存储更多数据,那么操作就会失败。

更多信息的一些链接(因为你不应该只是接受我的话):

答案 1 :(得分:17)

来自redis.conf,版本2.8

# Don't use more memory than 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 cache, or to set
# a hard memory limit for an instance (using the 'noeviction' policy).
#
# WARNING: If you have slaves attached to an instance with maxmemory on,
# the size of the output buffers needed to feed the slaves 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 slaves 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 slaves attached it is suggested that you set a lower
# limit for maxmemory so that there is some free RAM on the system for slave
# output buffers (but this is not needed if the policy is 'noeviction').
#
# maxmemory <bytes>

# MAXMEMORY POLICY: how Redis will select what to remove when maxmemory
# is reached. You can select among five behaviors:
#
# volatile-lru -> remove the key with an expire set using an LRU algorithm
# allkeys-lru -> remove any key according 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 any of the above policies, Redis will return an error on write
#       operations, when there are no suitable keys for eviction.
#
#       At the date of writing these 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

答案 2 :(得分:4)

我刚刚开始阅读关于Redis的内容,所以我不是正面的。但是,我确实遇到了一些可能有用的花絮。

以下是http://antirez.com/post/redis-as-LRU-cache.html的摘录:

  

将Redis用作缓存的另一种方法是   maxmemory指令,一个功能   允许指定最大值   要使用的内存量。当新数据   被添加到服务器和内存中   已达到限制,服务器   将删除一些旧数据删除   volatile键,即带键的键   EXPIRE(超时)设置,即使是   关键还远未到期   自动。

此外,Redis 2.0具有VM模式,其中所有键必须适合内存,但很少使用的键的值可以在磁盘上:

答案 3 :(得分:2)

如果你想知道当Redis(2.8)达到其配置定义的最大值时实际响应的是什么,它看起来像这样:

$ redis-cli
127.0.0.1:6379> GET 5
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
127.0.0.1:6379> SET 5 a
(error) OOM command not allowed when used memory > 'maxmemory'.

答案 4 :(得分:2)

更新redis 4.0

127.0.0.1:6379> MEMORY HELP
1) "MEMORY DOCTOR                        - Outputs memory problems report"
2) "MEMORY USAGE <key> [SAMPLES <count>] - Estimate memory usage of key"
3) "MEMORY STATS                         - Show memory usage details"
4) "MEMORY PURGE                         - Ask the allocator to release memory"
5) "MEMORY MALLOC-STATS                  - Show allocator internal stats"
  

/usr/local/etc/redis.conf

############################## 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 slaves attached to an instance with maxmemory on,
# the size of the output buffers needed to feed the slaves 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 slaves 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 slaves attached it is suggested that you set a lower
# limit for maxmemory so that there is some free RAM on the system for slave
# output buffers (but this is not needed if the policy is 'noeviction').
#
# maxmemory <bytes>

# MAXMEMORY POLICY: how Redis will select what to remove when maxmemory
# is reached. You can select among five behaviors:
#
# volatile-lru -> Evict using approximated LRU among the keys with an expire set.
# allkeys-lru -> Evict any key using approximated LRU.
# volatile-lfu -> Evict using approximated LFU among the keys with an expire set.
# allkeys-lfu -> Evict any key using approximated LFU.
# volatile-random -> Remove a random key among the ones 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 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, Redis will return an error on write
#       operations, when there are no suitable keys for eviction.
#
#       At the date of writing these 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 noeviction

# LRU, LFU and minimal TTL algorithms are not precise algorithms but approximated
# algorithms (in order to save memory), so you can tune it for speed or
# accuracy. For default Redis will check five keys and pick the one that was
# used less recently, you can change the sample size using the following
# configuration directive.
#
# The default of 5 produces good enough results. 10 Approximates very closely
# true LRU but costs more CPU. 3 is faster but not very accurate.
#
# maxmemory-samples 5

答案 5 :(得分:1)

我最近经历了一个无内存的情况,我的应用程序停止运行(写入不可能,读取成为可能),运行PHP脚本在中途停止运行并且必须为kill -9手动(即使在内存可用后)。

我假设发生了数据丢失(或数据不一致),因此我执行了flushdb并从备份中恢复。学过的知识?备份是你的朋友。

答案 6 :(得分:-3)

Redis不是像memcached这样的缓存,默认情况下(maxmemory-policy参数设置为noeviction)所有放入redis的数据都不会被删除,唯一的例外是使用EXPIRE。