当内存不足时,Redis 会做什么? [英] What does Redis do when it runs out of memory?

查看:34
本文介绍了当内存不足时,Redis 会做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This might be easy question but I am having a hard time finding the answer. How does Redis 2.0 handle running out of maximum allocated memory? How does it decide which data to remove or which data to keep in memory?

推荐答案

如果您打开了虚拟内存功能(现在已弃用),那么 Redis 开始存储不那么-经常使用"内存耗尽时数据到磁盘.

If you have virtual memory functionality turned on ( now deprecated), then Redis starts to store the "not-so-frequently-used" data to disk when memory runs out.

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

If virtual memory in Redis is disabled (the default) and the maxmemory parameter is set (the default), Redis will not use any more memory than maxmemory allows. If you turn maxmemory off, Redis will start using virtual memory (i.e. swap), and performance will drop tremendously.

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

Newer versions of Redis have various policies when maxmemory is reached:

  • volatile-lru - 删除一个键那些有过期集的,试图删除最近未使用的密钥.
  • volatile-ttl - 删除一个键那些有过期集的,试图移除剩余时间较短的钥匙活下去.
  • volatile-random - 删除一个随机密钥过期集.
  • allkeys-lru - 像volatile-lru,但会删除每个一种钥匙,普通钥匙或钥匙带有过期设置.
  • allkeys-random -类似于 volatile-random,但会删除各种键,普通键和设置了过期时间的键.
  • volatile-lru - remove a key among the ones with an expire set, trying to remove keys not recently used.
  • volatile-ttl - remove a key among the ones with an expire set, trying to remove keys with short remaining time to live.
  • volatile-random - remove a random key among the ones with an expire set.
  • allkeys-lru - like volatile-lru, but will remove every kind of key, both normal keys or keys with an expire set.
  • allkeys-random - like volatile-random, but will remove every kind of keys, both normal keys and keys with an expire set.

如果你选择一个只删除设置了 EXPIRE 的键的策略,那么当 Redis 内存不足时,看起来程序只是中止了 malloc() 操作.也就是说,如果您尝试存储更多数据,则写入操作只会失败.

If you pick a policy that only removes keys with an EXPIRE set, then when Redis runs out of memory, it looks like the program just aborts the malloc() operation. That is, if you try to store more data, the write operation simply fails.

有关更多信息的一些链接:

Some links for more info:

这篇关于当内存不足时,Redis 会做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆