Redis記憶體淘汰策略配置翻譯

奕鵬發表於2021-01-19

官方譯文

設定指定的記憶體位元組數限制。

 Set a memory usage limit to the specified(指定) amount of bytes.

一旦記憶體使用達到上限,Redis會根據選定的回收策略(參見:maxmemmory-policy)刪除key

When the memory limit is reached Redis will try to remove keys
 according to the eviction(收回、收回) policy(策略) selected (see maxmemory-policy).

如果因為刪除策略Redis無法刪除key,或者策略設定為 “noeviction”,Redis會回覆需要更多記憶體的錯誤資訊給命令終端。例如,SET,LPUSH等等,但持續響應只讀命令,例如get。

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.

在使用Redis作為LRU快取,或者為例項設定了硬性記憶體限制的時候(使用 “noeviction” 策略)的時候,這個選項通常事很有用的。

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).

警告:如果有多個slave連上達到記憶體上限時,master為同步slave的輸出緩衝區所需記憶體不計算在使用記憶體中。
這樣當移除key時,就不會因網路問題 / 重新同步事件觸發移除key的迴圈,反過來slaves的輸出緩衝區充滿了key被移除的DEL命令,這將觸發刪除更多的key,直到這個資料庫完全被清空為止。

 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.

總之,如果你需要新增多個slave,建議你設定一個最低記憶體限制,這樣的話,就有空閒的記憶體為slave的輸出快取區(但是最大記憶體策略設定為”noeviction”的話就沒必要了).

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>
maxmemory 記憶體大小(位元組數)
 MAXMEMORY POLICY: how Redis will select what to remove when maxmemory
 is reached. You can select among(...中) five behaviors:
最大記憶體策略:當記憶體大小達到最大限制時,redis實在下面五中行為中選擇:
 volatile-lru -> Evict using approximated LRU among the keys with an expire set.
根據LRU演算法刪除設定過期時間的key
 allkeys-lru -> Evict any(任何的,一些) key using approximated LRU.
根據LRU演算法刪除任何key
 volatile-lfu -> Evict using approximated LFU among the keys with an expire set.
根據LFU演算法刪除設定過期時間的key
 allkeys-lfu -> Evict any key using approximated LFU.
根據LFU刪除任何key
 volatile-random -> Remove a random key among the ones with an expire set.
隨機移除設定過過期時間的key
 allkeys-random -> Remove a random key, any key.
隨機移除任何key
 volatile-ttl -> Remove the key with the nearest expire time (minor TTL)
移除即將過期的key(minor TTL)
 noeviction -> Don't evict anything, just return an error on write operations.
當記憶體不足以容納新寫入資料時,新寫入操作會報錯。
 LRU means Least(最少,最小) Recently Used
LRU是指最少被使用的
 LFU means Least Frequently(最頻繁地,時常地) Used
LFU是指最不常被使用的
 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.
記住:使用以上的回收策略,在沒有合適的回收鍵,將在寫的操作上返回一個錯誤資訊。

這些寫的命令有:set setnx setex append……(上面的這些Redis操作命令)
The default is:
maxmemory-policy noeviction
快取粗略 策略名字

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

LRU、LFU和最小TTL演算法的實現都不是很精確,但是很接近(為了節約記憶體),所以你可以用樣本量做檢測。
預設Redis會檢查5個key然後取最舊的那個,你可以通過下面的配置指令來設定樣本的個數。

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.

預設值為5,數字越大結果越精確但是會消耗更多CPU,3是最快的但不是最精確的。

The default
 maxmemory-samples 5of 5 produces good enough results. 10 Approximates very closely
 true LRU but costs more CPU. 3 is faster but not very accurate(精確,準確).

從Redis5版本開始,在預設的從節點上,將忽略最大記憶體限制(除非從節點在故障切換或者手動地切換為主節點)。
這將意味著刪除命令通過在主節點實現,然後將命令傳送給從節點。

 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).

注意,由於預設情況下從節點不會主動回收,因此它可能會使用更多記憶體,甚至大於通過maxmemory設定的記憶體(有些緩衝區可能
在從節點上更大,或者資料結構有時可能佔用更多記憶體等等)。
因此,你要確保你的從節點監控器和有足夠大的記憶體空間,確保在主節點在未達到最大記憶體限制前,從節點不會出現記憶體不足的情況。
replica-ignore-maxmemory yes
配置項 配置值(Yes/No)

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.

過期刪除策略

定時過期:

1.每個設定過期時間的key都需要建立一個定時器,到過期時間就會立即清除。

2.該策略可以立即清除過期的資料,對記憶體很友好;

3.但是會佔用大量的CPU資源去處理過期的資料,從而影響快取的響應時間和吞吐量。

惰性過期:

1.只有當訪問一個key時,才會判斷該key是否已過期,過期則清除。

2.該策略可以最大化地節省CPU資源,卻對記憶體非常不友好。

3.可能出現大量的過期key沒有再次被訪問,從而不會被清除,佔用大量記憶體。

定期過期:

1.每隔一定的時間,會掃描一定數量的資料庫的expires字典中一定數量的key,並清除其中已過期的key。

2.該策略是前兩者的一個折中方案。通過調整定時掃描的時間間隔和每次掃描的限定耗時,可以在不同情況下使得CPU和記憶體資源達到最優的平衡效果。

3.expires字典會儲存所有設定了過期時間的key的過期時間資料,其中,key是指向鍵空間中的某個鍵的指標,value是該鍵的毫秒精度的UNIX時間戳表示的過期時間。鍵空間是指該Redis叢集中儲存的所有鍵。

如何配置過期策略

1.惰性過期策略為內建策略,無需配置

2.定期刪除策略

配置redis.conf 的hz選項,預設為10 (即1秒執行10次,100ms一次,值越大說明重新整理頻率越快,最Redis效能損耗也越大
第二、配置redis.conf的maxmemory最大值,當已用記憶體超過maxmemory限定時,就會觸發主動清理策略

記憶體淘汰策略

  • volatile-lru -> Evict using approximated LRU among the keys with an expire set.
    根據LRU演算法刪除設定過期時間的key

  • allkeys-lru -> Evict any(任何的,一些) key using approximated LRU.
    根據LRU演算法刪除任何key

  • volatile-lfu -> Evict using approximated LFU among the keys with an expire set.
    根據LFU演算法刪除設定過期時間的key

  • allkeys-lfu -> Evict any key using approximated LFU.
    根據LFU刪除任何key

  • volatile-random -> Remove a random key among the ones with an expire set.
    隨機移除設定過過期時間的key

  • allkeys-random -> Remove a random key, any key.
    隨機移除任何key

  • volatile-ttl -> Remove the key with the nearest expire time (minor TTL)
    移除即將過期的key(minor TTL)

  • noeviction -> Don’t evict anything, just return an error on write operations.
    當記憶體不足以容納新寫入資料時,新寫入操作會報錯。

配置記憶體淘汰策略

 maxmemory-policy noeviction

持久化對過期策略的影響

  • RDB持久化

1.持久化key之前,會檢查是否過期,過期的key不進入RDB檔案.

2.資料載入資料庫之前,會對key先進行過期檢查,如果過期,不匯入資料庫(主庫情況)

  • AOF持久化

1.從記憶體資料庫持久化資料到AOF檔案:
當key過期後,還沒有被刪除,此時進行執行持久化操作(該key是不會進入aof檔案的,因為沒有發生修改命令)

當key過期後,在發生刪除操作時,程式會向aof檔案追加一條del命令(在將來的以aof檔案恢復資料的時候該過期的鍵就會被刪掉)

2.從AOF重寫到記憶體
重寫時,會先判斷key是否過期,已過期的key不會重寫到aof檔案

主從複製對過期策略的影響

1.預設情況下,從節點是不做資料過期處理的,可以通過replica-ignore-maxmemory yes
決定開啟是否在從節點處理過期處理策略。

2主從複製預設的情況下,都是在主節點實現,主節點將對應的del命令傳送給從節點實現,從節點執行del命令。

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章