云栖号资讯:【点击查看更多行业资讯】
在这里您可以找到不同行业的第一手的上云资讯,还在等什么,快来!
文章前言
提到内存管理,我们就需要考虑Redis的内存过期策略和内存淘汰机制。该文章便从这两方面入手,分享一些在Redis内存方面相关的基础知识。
文章中使用的示例版本为Redis5.0版本。
内存过期策略
内存过期策略主要的作用就是,在缓存过期之后,能够及时的将失效的缓存从内存中删除,以减少内存的无效暂用,达到释放内存的目的。
过期策略分类
Redis内存过期策略分为三类,定时策略、惰性策略和定期策略。
定时策略
含义:在设置key的过期时间的同时,为该key创建一个定时器,让定时器在key的过期时间来临时,对key进行删除。
优点:保证内存被尽快释放,减少无效的缓存暂用内存。
缺点:若过期key很多,删除这些key会占用很多的CPU时间,在CPU时间紧张的情况下,CPU不能把所有的时间用来做要紧的事儿,还需要去花时间删除这些key。定时器的创建耗时,若为每一个设置过期时间的key创建一个定时器(将会有大量的定时器产生),性能影响严重。一般来说,是不会选择该策略模式。

定时策略
惰性策略
含义:key过期的时候不删除,每次从数据库获取key的时候去检查是否过期,若过期,则删除,返回null。
优点:删除操作只发生在从数据库取出key的时候发生,而且只删除当前key,所以对CPU时间的占用是比较少的,而且此时的删除是已经到了非做不可的地步(如果此时还不删除的话,我们就会获取到了已经过期的key了)。
缺点:若大量的key在超出超时时间后,很久一段时间内,都没有被获取过,此时的无效缓存是永久暂用在内存中的,那么可能发生内存泄露(无用的垃圾占用了大量的内存)。

惰性策略
定期策略
含义:每隔一段时间对设置了缓存时间的key进行检测,如果可以已经失效,则从内存中删除,如果未失效,则不作任何处理。
优点:通过限制删除操作的时长和频率,来减少删除操作对CPU时间的占用--处理"定时删除"的缺点
定期删除过期key--处理"惰性删除"的缺点。
缺点:在内存友好方面,不如"定时删除",因为是随机遍历一些key,因此存在部分key过期,但遍历key时,没有被遍历到,过期的key仍在内存中。在CPU时间友好方面,不如"惰性删除",定期删除也会暂用CPU性能消耗。
难点:合理设置删除操作的执行时长(每次删除执行多长时间)和执行频率(每隔多长时间做一次删除)(这个要根据服务器运行情况来定了)
该方式不是去便利所有的ky,而是随机抽取一些key做过期检测。

定期策略
策略注意事项
过期策略对持久化存储的影响
持久化存储,指的是将内存的缓存永久存在磁盘中。也就是说我们的AOF和RDB持久化存储方式。因为该两种方式,将内存中的数据写入磁盘,这时候就需要考虑到我们过期的缓存是否会被写入到磁盘中?如果写入磁盘又是怎么处理的?
RDB持久化
持久化key之前,会检查是否过期,过期的key不进入RDB文件。
数据载入数据库之前,会对key先进行过期检查,如果过期,不导入数据库(主库情况)。
AOF持久化
当key过期后,还没有被删除,此时进行执行持久化操作(该key是不会进入aof文件的,因为没有发生修改命令)。
当key过期后,在发生删除操作时,程序会向aof文件追加一条del命令(在将来的以aof文件恢复数据的时候该过期的键就会被删掉)。
因为AOF方式,向存储文件追加的是Redis的操作命令,而不是具体的数据,然而RDB确是存储的安全的二进制内容。
重写时,会先判断key是否过期,已过期的key不会重写到aof文件。
即使在重写时,不验证是否过期,然而追加了del命令,测试无效的key同样会被删除。判断的情况是为了防止没有加入del命令的key。
内存淘汰机制
定义说明
内存淘汰机制针对是内存不足的情况下的一种Redis处理机制。例如,当前的Redis存储已经超过内存限制了,然而我们的业务还在继续往Redis里面追加缓存内容,这时候Redis的淘汰机制就起到作用了。
淘汰机制分类
根据redis.conf的配置文件中,我们可以得出,主要分为如下六种淘汰机制。
1# volatile-lru -> Evict using approximated LRU among the keys with an expire set. 2# allkeys-lru -> Evict any key using approximated LRU. 3# volatile-lfu -> Evict using approximated LFU among the keys with an expire set. 4# allkeys-lfu -> Evict any key using approximated LFU. 5# volatile-random -> Remove a random key among the ones with an expire set. 6# allkeys-random -> Remove a random key, any key. 7# volatile-ttl -> Remove the key with the nearest expire time (minor TTL) 8# noeviction -> Don't evict anything, just return an error on write operations.
这六种机制主要是什么意思内,下面是分别针对上面的几种机制做一个说明。
1volatile-lru:当内存不足以容纳新写入数据时,在设置了过期时间的键空间中,移除最近最少使用的key。 2 3allkeys-lru:当内存不足以容纳新写入数据时,在键空间中,移除最近最少使用的key(这个是最常用的)。 4 5volatile-lfu:当内存不足以容纳新写入数据时,在过期密集的键中,使用LFU算法进行删除key。 6 7allkeys-lfu:当内存不足以容纳新写入数据时,使用LFU算法移除所有的key。 8 9volatile-random:当内存不足以容纳新写入数据时,在设置了过期的键中,随机删除一个key。 10 11allkeys-random:当内存不足以容纳新写入数据时,随机删除一个或者多个key。 12 13volatile-ttl:当内存不足以容纳新写入数据时,在设置了过期时间的键空间中,有更早过期时间的key优先移除。 14 15noeviction:当内存不足以容纳新写入数据时,新写入操作会报错。
内存管理配置翻译
1# Set a memory usage limit to the specified amount of bytes. 2#将内存使用限制设置为指定的字节数。 3# When the memory limit is reached Redis will try to remove keys 4#当达到内存限制时,Redis将尝试删除密钥 5# according to the eviction policy selected (see maxmemory-policy). 6#根据所选的逐出策略(请参阅maxmemory策略)。 7# 8# 9# If Redis can't remove keys according to the policy, or if the policy is 10#如果Redis不能根据策略删除密钥,或者如果策略是 11# set to 'noeviction', Redis will start to reply with errors to commands 12#设置为“noeviction”时,Redis将开始对命令进行错误的应答 13# that would use more memory, like SET, LPUSH, and so on, and will continue 14#这将使用更多内存,如SET、LPUSH等,并将继续 15# to reply to read-only commands like GET. 16#回复像GET这样的只读命令。 17# 18# 19# This option is usually useful when using Redis as an LRU or LFU cache, or to 20#当将Redis用作LRU或LFU缓存或 21# set a hard memory limit for an instance (using the 'noeviction' policy). 22#为实例设置硬内存限制(使用“noeviction”策略)。 23# 24# 25# WARNING: If you have replicas attached to an instance with maxmemory on, 26#警告:如果将副本附加到启用了maxmemory的实例, 27# the size of the output buffers needed to feed the replicas are subtracted 28#减去为复制副本提供数据所需的输出缓冲区的大小 29# from the used memory count, so that network problems / resyncs will 30#从网络中重新同步使用的问题 31# not trigger a loop where keys are evicted, and in turn the output 32#不触发一个循环,其中键被逐出,而反过来输出 33# buffer of replicas is full with DELs of keys evicted triggering the deletion 34#复制副本的缓冲区已满,退出的密钥将触发删除 35# of more keys, and so forth until the database is completely emptied. 36#直到数据库完全清空。 37# 38# In short... if you have replicas attached it is suggested that you set a lower 39#简而言之。。。如果您附加了副本,建议您设置较低的 40# limit for maxmemory so that there is some free RAM on the system for replica 41#限制maxmemory,以便系统上有一些可用的RAM用于复制 42# output buffers (but this is not needed if the policy is 'noeviction'). 43#输出缓冲区(但如果策略为“noeviction”,则不需要此缓冲区)。 44# maxmemory <bytes> 45#最大内存<字节> 46# MAXMEMORY POLICY: how Redis will select what to remove when maxmemory 47#MAXMEMORY策略:当MAXMEMORY时Redis如何选择要删除的内容 48# is reached. You can select among five behaviors: 49#已到达。您可以从五种行为中进行选择: 50# 51# volatile-lru -> Evict using approximated LRU among the keys with an expire set. 52#volatile lru->在具有expire集的密钥中使用近似的lru进行逐出。 53# allkeys-lru -> Evict any key using approximated LRU. 54#allkeys lru->使用近似的lru逐出任何键。 55# volatile-lfu -> Evict using approximated LFU among the keys with an expire set. 56#volatile lfu->在具有expire集的密钥中使用近似的lfu进行逐出。 57# allkeys-lfu -> Evict any key using approximated LFU. 58#allkeys lfu->使用近似的lfu逐出任何键。 59# volatile-random -> Remove a random key among the ones with an expire set. 60#volatile random->从具有expire集的密钥中删除一个随机密钥。 61# allkeys-random -> Remove a random key, any key. 62#allkeys random->移除一个随机键,任意键。 63# volatile-ttl -> Remove the key with the nearest expire time (minor TTL) 64#volatile ttl->删除最接近过期时间的密钥(minor ttl) 65# noeviction -> Don't evict anything, just return an error on write operations. 66#noeviction->不要逐出任何内容,只要在写操作时返回一个错误。 67# LRU means Least Recently Used 68#LRU表示最近最少使用 69# LFU means Least Frequently Used 70#LFU表示使用频率最低 71# Both LRU, LFU and volatile-ttl are implemented using approximated 72#LRU、LFU和volatile ttl都是用近似方法实现的 73# randomized algorithms. 74#随机算法。 75# Note: with any of the above policies, Redis will return an error on write 76#注意:如果使用上述任何策略,Redis将在写入时返回错误 77# operations, when there are no suitable keys for eviction. 78#操作,当没有合适的钥匙驱逐。 79# 80#At the date of writing these commands are: set setnx setex append 81#在编写这些命令的日期是:set setnx setex append 82#incr decr rpush lpush rpushx lpushx linsert lset rpoplpush sadd 83#增加/减少脉冲低压脉冲rpushx lpushx linsert lset RPOPPLPUSH sadd 84#sinter sinterstore sunion sunionstore sdiff sdiffstore zadd zincrby 85#sinter sinterstore sunion sunionstore sdiffstore zadd zincrby 86#zunionstore zinterstore hset hsetnx hmset hincrby incrby decrby 87#zunionstore zinterstore hset hsetnx hmset hincrby incrby递减 88#getset mset msetnx exec sort 89#getset mset msetnx exec排序 90# 91# 92# The default is: 93#默认值为: 94# 95# 96# maxmemory-policy noeviction 97#maxmemory策略不可用 98# LRU, LFU and minimal TTL algorithms are not precise algorithms but approximated 99#LRU、LFU和最小TTL算法不是精确算法而是近似算法 100# algorithms (in order to save memory), so you can tune it for speed or 101#算法(为了节省内存),所以你可以调整它的速度或 102# accuracy. For default Redis will check five keys and pick the one that was 103#准确度。对于默认的Redis将检查五个键并选择一个 104# used less recently, you can change the sample size using the following 105#最近使用较少,可以使用以下命令更改样本大小 106# configuration directive. 107#配置指令。 108# 109# 110# The default of 5 produces good enough results. 10 Approximates very closely 111#默认值为5会产生足够好的结果。10非常接近 112# true LRU but costs more CPU. 3 is faster but not very accurate. 113#真正的外场可更换单元,但占用更多的CPU。3更快,但不是很准确。 114# maxmemory-samples 5 115#maxmemory示例5 116# Starting from Redis 5, by default a replica will ignore its maxmemory setting 117#从Redis 5开始,默认情况下副本将忽略其maxmemory设置 118# (unless it is promoted to master after a failover or manually). It means 119#(除非在故障转移后或手动升级为主节点)。意思是 120# that the eviction of keys will be just handled by the master, sending the 121#密钥的收回将由主机处理,发送 122# DEL commands to the replica as keys evict in the master side. 123#DEL命令在主服务器端作为密钥收回。 124# 125# This behavior ensures that masters and replicas stay consistent, and is usually 126#这种行为可以确保主机和副本保持一致,并且通常 127# what you want, however if your replica is writable, or you want the replica to have 128#但是,如果您的复制副本是可写的,或者您希望复制副本具有 129# a different memory setting, and you are sure all the writes performed to the 130#不同的内存设置,并且您确定对 131# replica are idempotent, then you may change this default (but be sure to understand 132#复制副本是幂等的,那么您可以更改这个默认值(但是一定要理解 133# what you are doing). 134#你在做什么)。 135# 136# Note that since the replica by default does not evict, it may end using more 137#请注意,由于复制副本在默认情况下不会逐出,它可能会使用更多 138# memory than the one set via maxmemory (there are certain buffers that may 139#内存大于通过maxmemory设置的内存(有某些缓冲区可能 140# be larger on the replica, or data structures may sometimes take more memory and so 141#复制副本越大,或者数据结构有时可能占用更多内存,因此 142# forth). So make sure you monitor your replicas and make sure they have enough 143#第四)。所以一定要监控你的复制品,确保它们有足够的 144# memory to never hit a real out-of-memory condition before the master hits 145#内存在主机命中之前永远不会遇到内存不足的情况 146# the configured maxmemory setting. 147#配置的maxmemory设置。 148# 149# 150# replica-ignore-maxmemory yes 151#复制副本忽略maxmemory是
Redis命令
这里总结几个Redis中常用的与时间有关的命令。
- exists key:判断键是否存在,如果存在则返回1,不存在则返回0;
- expire key:给键设置过期时间,单位s(秒);
- ttl key:返回键剩余的过期时间,单位s(秒);当键不存在是返回-2;存在并且未设置过期时间,返回-1;如果返回≥0,则该返回值则为过期的时间;
- ptt key:返回键剩余的过期时间,单位ms(毫秒);当键不存在是返回-2;存在并且未设置过期时间,返回-1;如果返回≥0,则该返回值则为过期的时间;
【云栖号在线课堂】每天都有产品技术专家分享!
课程地址:https://yqh.aliyun.com/live立即加入社群,与专家面对面,及时了解课程最新动态!
【云栖号在线课堂 社群】https://c.tb.cn/F3.Z8gvnK
原文发布时间:2020-07-01
本文作者:卡二条
本文来自:“掘金”,了解相关信息可以关注“掘金”