Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Nosql Redis在内存不足时做什么?_Nosql_Redis - Fatal编程技术网

Nosql Redis在内存不足时做什么?

Nosql Redis在内存不足时做什么?,nosql,redis,Nosql,Redis,这可能是一个简单的问题,但我很难找到答案。Redis 2.0如何处理最大分配内存不足的问题?它如何决定删除哪些数据或将哪些数据保留在内存中?Redis与memcached不同,默认情况下(其中maxmemory policy参数设置为noeviction),您放入Redis的所有数据都不会被删除,唯一的例外是使用EXPIRE。我最近刚开始阅读有关Redis的文章,所以我不是很肯定。但是,我确实发现了一些可能有用的花边新闻 以下是一段来自: 将Redis用作缓存的另一种方法是 maxmemory指

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

Redis与memcached不同,默认情况下(其中
maxmemory policy
参数设置为
noeviction
),您放入Redis的所有数据都不会被删除,唯一的例外是使用EXPIRE。

我最近刚开始阅读有关Redis的文章,所以我不是很肯定。但是,我确实发现了一些可能有用的花边新闻

以下是一段来自:

将Redis用作缓存的另一种方法是 maxmemory指令,一个特性 它允许指定最大值 要使用的内存量。当新数据 已添加到服务器,并且内存 已达到限制,服务器 将删除一些旧数据删除一个 易失性键,即具有 过期(超时)设置,即使 密钥还远未到期 自动地

此外,Redis 2.0有一个VM模式,其中所有密钥都必须放在内存中,但很少使用的密钥的值可以放在磁盘上:


如果已启用虚拟内存功能(编辑:现在已弃用),则当内存耗尽时,Redis会开始将“不常用”的数据存储到磁盘

如果禁用了Redis中的虚拟内存(默认),并设置了
maxmemory
参数(默认),Redis将不会使用超出
maxmemory
允许的内存。如果关闭
maxmemory
,Redis将开始使用虚拟内存(即交换),性能将大幅下降

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

  • 易失性lru
    -在 有过期设置的,正在尝试 删除最近未使用的密钥
  • volatile ttl
    -在 有过期设置的,正在尝试 取下剩余时间短的钥匙 活着
  • volatile random
    -删除 在具有 过期设置
  • 所有键lru
    -类似
    易失性lru
    ,但将删除每个 钥匙类型,普通钥匙或钥匙 使用过期设置
  • allkeys random
    - 类似于volatile random,但将删除 每种钥匙,都是普通钥匙 和具有过期设置的密钥
如果您选择的策略只删除过期设置的键,那么当Redis内存不足时,程序似乎只是中止malloc()操作。也就是说,如果试图存储更多数据,写入操作就会失败

有关更多信息,请访问以下链接:


我最近遇到了一个没有可用内存的情况,我的应用程序停止运行(不可能写入,也可能读取),运行PHP脚本中途停止,必须手动执行
kill-9
(即使在内存可用之后)

我假设发生了数据丢失(或数据不一致),所以我进行了
flushdb
,并从备份中恢复。吸取的教训?备份是您的朋友。

来自,版本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将尝试删除密钥
#根据选择的逐出策略(请参阅maxmemory策略)。
#
#如果Redis无法根据策略删除密钥,或者如果策略为
#设置为“noeviction”,Redis将开始回复命令错误
#这将使用更多内存,如SET、LPUSH等,并将继续
#回复GET之类的只读命令。
#
#当使用Redis作为LRU缓存或设置
#实例的硬内存限制(使用“noeviction”策略)。
#
#警告:如果将从属服务器连接到maxmemory打开的实例,
#减去馈送从属设备所需的输出缓冲区的大小
#从已用内存计数中删除,以便网络问题/重新同步
#不触发一个循环,其中键被逐出,并依次输出
#从机缓冲区已满,退出的密钥的增量触发删除
#等等,直到数据库完全清空。
#
#简言之。。。如果已连接从属服务器,建议您设置较低的
#限制maxmemory,以便系统上有一些可用RAM供从机使用
#输出缓冲区(但如果策略为“noeviction”,则不需要此缓冲区)。
#
#最大内存
#MAXMEMORY策略:当使用MAXMEMORY时,Redis将如何选择要删除的内容
#到达。您可以从五种行为中进行选择:
#
#volatile lru->使用lru算法删除带有过期集的密钥
#allkeys lru->根据lru算法删除任何密钥
#volatile random->删除带有过期集的随机密钥
#allkeys 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
如果您想知道当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'.
更新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"
127.0.0.1:6379>内存帮助
1) “内存医生-输出内存问题报告”
2) “内存使用情况[示例]-估计密钥的内存使用情况”
3) “内存统计
############################## 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