淺談redis的maxmemory設(shè)置以及淘汰策略
redis的maxmemory參數(shù)用于控制redis可使用的最大內(nèi)存容量。如果超過(guò)maxmemory的值,就會(huì)動(dòng)用淘汰策略來(lái)處理expaire字典中的鍵。
關(guān)于redis的淘汰策略:
Redis提供了下面幾種淘汰策略供用戶選擇,其中默認(rèn)的策略為noeviction策略:
· noeviction:當(dāng)內(nèi)存使用達(dá)到閾值的時(shí)候,所有引起申請(qǐng)內(nèi)存的命令會(huì)報(bào)錯(cuò)。
· allkeys-lru:在主鍵空間中,優(yōu)先移除最近未使用的key。
· volatile-lru:在設(shè)置了過(guò)期時(shí)間的鍵空間中,優(yōu)先移除最近未使用的key。
· allkeys-random:在主鍵空間中,隨機(jī)移除某個(gè)key。
· volatile-random:在設(shè)置了過(guò)期時(shí)間的鍵空間中,隨機(jī)移除某個(gè)key。
· volatile-ttl:在設(shè)置了過(guò)期時(shí)間的鍵空間中,具有更早過(guò)期時(shí)間的key優(yōu)先移除。
PS:
關(guān)于maxmemory的設(shè)置,如果redis的應(yīng)用場(chǎng)景是作為db使用,那不要設(shè)置這個(gè)選項(xiàng),因?yàn)閐b是不能容忍丟失數(shù)據(jù)的。
如果作為cache使用,則可以啟用這個(gè)選項(xiàng)(其實(shí)既然有淘汰策略,那就是cache了。。。)
但是在集群環(huán)境下(尤其是有多個(gè)slavers的情形),maxmeomory的值并不是實(shí)際redis使用的內(nèi)存,這個(gè)選項(xiàng)值并沒(méi)有包括slaver的output buffer。
redis早期版本出過(guò)一個(gè)bug,在多個(gè)slaver的情形下,設(shè)置了maxmemory值,同時(shí)設(shè)定了淘汰策略,會(huì)造成master上的數(shù)據(jù)被漸漸擦除。
antirez先生給出了這個(gè)問(wèn)題的原因:
The issue happens for the following reason: Redis reached the configured limit, so it tries to expire keys. Evicting keys turns into explicit DELs sent to slaves, since masters control the eviction of slaves for well known reasons. But this way if there are enough slaves, emitting the protocol in the output buffers will actually take more memory than the amount freed removing keys... So the key eviction process starts to enter into an infinite loop. Up to a given point the fact that there is a static buffer part in the output queue of every client (including slaves) mitigate this in certain conditions, but once Redis can't use the output buffer but must use the queue of objects the infinite loop is triggered.
簡(jiǎn)單說(shuō)來(lái),刪除過(guò)期鍵,需要產(chǎn)生del命令發(fā)送給slaver,如果slaver足夠多,output buffer將會(huì)占用足夠多的內(nèi)存,導(dǎo)致更多的鍵過(guò)期,如此往復(fù),陷入了無(wú)線循環(huán)。
解決方案有多種,比如output buffer可以不計(jì)入maxmemory。
因此,在3.0版本的配置說(shuō)明中有了以下表述:
# 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></bytes>
由此可見(jiàn),如果有slaver的情況下,建議適當(dāng)調(diào)低maxmemory,給output buffer留出一定的可用空間是合理的。
以上這篇淺談redis的maxmemory設(shè)置以及淘汰策略就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Redis實(shí)現(xiàn)分布式鎖(setnx、getset、incr)以及如何處理超時(shí)情況
本文主要介紹了Redis實(shí)現(xiàn)分布式鎖(setnx、getset、incr)以及如何處理超時(shí)情況,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11redis集群實(shí)現(xiàn)清理前綴相同的key
這篇文章主要介紹了redis集群實(shí)現(xiàn)清理前綴相同的key,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10redis集群搭建_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了redis集群搭建,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08手把手教你用Redis 實(shí)現(xiàn)點(diǎn)贊功能并且與數(shù)據(jù)庫(kù)同步
本文主要介紹了Redis 實(shí)現(xiàn)點(diǎn)贊功能并且與數(shù)據(jù)庫(kù)同步,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05redis底層數(shù)據(jù)結(jié)構(gòu)之skiplist實(shí)現(xiàn)示例
這篇文章主要為大家介紹了redis底層數(shù)據(jù)結(jié)構(gòu)之skiplist實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12Spring?Boot?整合Redis?實(shí)現(xiàn)優(yōu)惠卷秒殺?一人一單功能
這篇文章主要介紹了Spring?Boot?整合Redis?實(shí)現(xiàn)優(yōu)惠卷秒殺?一人一單,在分布式系統(tǒng)下,高并發(fā)的場(chǎng)景下,會(huì)出現(xiàn)此類庫(kù)存超賣問(wèn)題,本篇文章介紹了采用樂(lè)觀鎖來(lái)解決,需要的朋友可以參考下2022-09-09