淺談redis的maxmemory設(shè)置以及淘汰策略
redis的maxmemory參數(shù)用于控制redis可使用的最大內(nèi)存容量。如果超過maxmemory的值,就會動用淘汰策略來處理expaire字典中的鍵。
關(guān)于redis的淘汰策略:
Redis提供了下面幾種淘汰策略供用戶選擇,其中默認的策略為noeviction策略:
· noeviction:當內(nèi)存使用達到閾值的時候,所有引起申請內(nèi)存的命令會報錯。
· allkeys-lru:在主鍵空間中,優(yōu)先移除最近未使用的key。
· volatile-lru:在設(shè)置了過期時間的鍵空間中,優(yōu)先移除最近未使用的key。
· allkeys-random:在主鍵空間中,隨機移除某個key。
· volatile-random:在設(shè)置了過期時間的鍵空間中,隨機移除某個key。
· volatile-ttl:在設(shè)置了過期時間的鍵空間中,具有更早過期時間的key優(yōu)先移除。
PS:
關(guān)于maxmemory的設(shè)置,如果redis的應(yīng)用場景是作為db使用,那不要設(shè)置這個選項,因為db是不能容忍丟失數(shù)據(jù)的。
如果作為cache使用,則可以啟用這個選項(其實既然有淘汰策略,那就是cache了。。。)
但是在集群環(huán)境下(尤其是有多個slavers的情形),maxmeomory的值并不是實際redis使用的內(nèi)存,這個選項值并沒有包括slaver的output buffer。
redis早期版本出過一個bug,在多個slaver的情形下,設(shè)置了maxmemory值,同時設(shè)定了淘汰策略,會造成master上的數(shù)據(jù)被漸漸擦除。
antirez先生給出了這個問題的原因:
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.
簡單說來,刪除過期鍵,需要產(chǎn)生del命令發(fā)送給slaver,如果slaver足夠多,output buffer將會占用足夠多的內(nèi)存,導(dǎo)致更多的鍵過期,如此往復(fù),陷入了無線循環(huán)。
解決方案有多種,比如output buffer可以不計入maxmemory。
因此,在3.0版本的配置說明中有了以下表述:
# 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>
由此可見,如果有slaver的情況下,建議適當調(diào)低maxmemory,給output buffer留出一定的可用空間是合理的。
以上這篇淺談redis的maxmemory設(shè)置以及淘汰策略就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Redis實現(xiàn)分布式鎖(setnx、getset、incr)以及如何處理超時情況
本文主要介紹了Redis實現(xiàn)分布式鎖(setnx、getset、incr)以及如何處理超時情況,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11redis集群搭建_動力節(jié)點Java學(xué)院整理
這篇文章主要介紹了redis集群搭建,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08手把手教你用Redis 實現(xiàn)點贊功能并且與數(shù)據(jù)庫同步
本文主要介紹了Redis 實現(xiàn)點贊功能并且與數(shù)據(jù)庫同步,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05redis底層數(shù)據(jù)結(jié)構(gòu)之skiplist實現(xiàn)示例
這篇文章主要為大家介紹了redis底層數(shù)據(jù)結(jié)構(gòu)之skiplist實現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12Spring?Boot?整合Redis?實現(xiàn)優(yōu)惠卷秒殺?一人一單功能
這篇文章主要介紹了Spring?Boot?整合Redis?實現(xiàn)優(yōu)惠卷秒殺?一人一單,在分布式系統(tǒng)下,高并發(fā)的場景下,會出現(xiàn)此類庫存超賣問題,本篇文章介紹了采用樂觀鎖來解決,需要的朋友可以參考下2022-09-09