REDIS----配置文件----MEMORY MANAGEMENT

MEMORY MANAGEMENT部分:

1.内存上限:

 

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

将内存使用限制设置为指定的字节数。当达到内存限制时,Redis将根据所选的逐出策略(请参阅maxmemory策略)尝试删除keys;
如果Redis无法根据策略移除keys,或者策略设置为“noeviction”,Redis将开始对使用更多内存的命令(如set、LPUSH等)进行错误回复,并将继续回复GET等只读命令;
当将Redis用作LRU或LFU缓存或设置实例的硬内存限制(使用“noeviction”策略)时,此选项通常很有用;
警告:如果将副本附加到启用maxmemory的实例,则将从已用内存计数中减去馈送副本所需的输出缓冲区的大小,这样,网络问题/重新同步将不会触发收回keys的循环,而副本的输出缓冲区将充满收回的keys增量,从而触发删除更多键,依此类推,直到数据库完全清空;
简而言之。。。如果附加了副本,建议您设置maxmemory的下限,以便系统上有一些空闲RAM用于副本输出缓冲区(但如果策略为“noeviction”,则不需要此限制)

2.逐出策略:

 

# 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

当达到MAXMEMORY时,Redis将如何选择要删除的内容。您可以在八种行为中进行选择:
1.从已设置过期时间的数据集中挑选最近最少使用的数据淘汰
2.从数据集中挑选最近最少使用的数据淘汰
3.从已设置过期时间的数据集挑选使用频率最低的数据淘汰
4.从数据集中挑选使用频率最低的数据淘汰
5.从已设置过期时间的数据集中任意选择数据淘汰
6.从数据集中任意选择数据淘汰
7.从已设置过期时间的数据集中挑选将要过期的数据淘汰
8.禁止驱逐数据,这也是默认策略
LRU、LFU和volatile-ttl都是用近似随机算法实现的
当没有合适的keys回收的时候,下面命令会报错: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

3.近似 LRU 算法:

 

# 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

LRU、LFU和最小TTL算法不是精确算法,而是近似算法(为了节省内存),因此您可以调整它的速度或精度。默认情况下,Redis将检查五个键并选择最近使用较少的键,您可以使用以下配置指令更改样本大小;
默认值5产生足够好的结果。10非常接近真实的LRU,但需要更多的CPU。3更快,但不太准确

4.从服务器忽略内存限制:

 

# Starting from Redis 5, by default a replica will ignore its maxmemory setting
# (unless it is promoted to master after a failover or manually). It means
# that the eviction of keys will be just handled by the master, sending the
# DEL commands to the replica as keys evict in the master side.
#
# This behavior ensures that masters and replicas stay consistent, and is usually
# what you want, however if your replica is writable, or you want the replica to have
# a different memory setting, and you are sure all the writes performed to the
# replica are idempotent, then you may change this default (but be sure to understand
# what you are doing).
#
# Note that since the replica by default does not evict, it may end using more
# memory than the one set via maxmemory (there are certain buffers that may
# be larger on the replica, or data structures may sometimes take more memory and so
# forth). So make sure you monitor your replicas and make sure they have enough
# memory to never hit a real out-of-memory condition before the master hits
# the configured maxmemory setting.
#
 replica-ignore-maxmemory yes

从Redis 5开始,默认情况下,复制副本将忽略其maxmemory设置(除非在故障转移后升级为master或手动)。这意味着keys的收回将由主服务器处理,将DEL命令作为主服务器端的keys收回发送到副本;
此行为确保主副本和副本保持一致,并且通常是您所希望的,但是,如果您的副本是可写的,或者您希望副本具有不同的内存设置,并且您确定对副本执行的所有写入都是等幂的,则可以更改此默认值(但请确保了解您正在执行的操作);
请注意,由于默认情况下副本不会退出,因此它可能会使用比通过maxmemory设置的内存更多的内存(副本上可能有某些缓冲区更大,或者数据结构有时可能占用更多内存等等)。因此,请确保监视复制副本,并确保它们有足够的内存,在主副本达到配置的maxmemory设置之前,不会出现真正的内存不足情况;



作者:JuMinggniMuJ
链接:https://www.jianshu.com/p/c07a135598a0
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值