redis的maxmemory設定,redismaxmemory
redis的maxmemory參數用於控制redis可使用的最大記憶體容量。如果超過maxmemory的值,就會動用淘汰策略來處理expaire字典中的鍵。
關於redis的淘汰策略,很多文章都有描述,就不做解釋了。
關於maxmemory的設定,如果redis的應用情境是作為db使用,那不要設定這個選項,因為db是不能容忍遺失資料的。
如果作為cache使用,則可以啟用這個選項(其實既然有淘汰策略,那就是cache了。。。)
但是在叢集環境下(尤其是有多個slavers的情形),maxmeomory的值並不是實際redis使用的記憶體,這個選項值並沒有包括slaver的output buffer。
redis早期版本出過一個bug,在多個slaver的情形下,設定了maxmemory值,同時設定了淘汰策略,會造成master上的資料被漸漸擦除。
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.
簡單說來,刪除到期鍵,需要產生del命令發送給slaver,如果slaver足夠多,output buffer將會佔用足夠多的記憶體,導致更多的鍵到期,如此往複,陷入了無線迴圈。
解決方案有多種,比如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>
由此可見,如果有slaver的情況下,建議適當調低maxmemory,給output buffer留出一定的可用空間是合理的。
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。