Redis在内存不足时做什么?

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

如果开启了虚拟内存function(2.0或2.2版本的新function),那么当内存用完时,Redis会开始将“不常用的”数据存储到磁盘中。

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

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

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

  • volatile-lru删除那些过期设置的密钥,试图删除最近不使用的密钥。
  • volatile-ttl删除一个过期集合中的一个键,试图删除与短暂的剩余时间生存的键。
  • 随机随机删除一个随机密钥中的一个到期的设置。
  • allkeys-lru就像volatile-lru一样,但是会删除每种types的密钥,包括普通密钥或者过期集合的密钥。
  • allkeys-random类似于volatile-random,但是会移除每种types的密钥,包括普通密钥和过期密钥。

如果您select的策略只能删除EXPIRE集的密钥,那么当Redis内存不足时,程序看起来像是中止了malloc()操作。 也就是说,如果你试图存储更多的数据,操作只会失败。

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

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 

我刚刚开始阅读关于Redis的内容,所以我不积极。 但是,我遇到了一些可能有用的小技巧。

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

使用Redis作为caching的另一种方法是maxmemory指令,该指令允许指定要使用的最大内存量。 当新数据被添加到服务器,并且已经达到内存限制时,服务器将删除一些旧数据,删除一个易失密钥,也就是说,一个设置了EXPIRE(超时)的密钥,即使密钥仍然很远从自动过期。

另外,Redis 2.0有一个虚拟机模式,所有密钥都必须放在内存中,但是很less使用的密钥的值可以在磁盘上:

如果您想知道Redis(2.8)在达到configuration所定义的最大值时实际响应了什么,则看起来像这样:

 $ 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'. 

我最近经历了一个没有释放内存的情况,而且我的应用程序停止了(不可能写入,读取是可能的),运行PHP脚本在中途停下来,不得不手动kill -9 (甚至内存可用后)。

我认为发生了数据丢失(或数据不一致),所以我做了flushdb并从备份中恢复。 学过的知识? 备份是你的朋友。

更新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 

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