一、Redis占用內存大小
我們知道Redis是基於內存的key-value數據庫,因為系統的內存大小有限,所以我們在使用Redis的時候可以配置Redis能使用的最大的內存大小。
1、通過配置文件配置
通過在Redis安裝目錄下面的redis.conf配置文件中添加以下配置設置內存大小
//設置Redis最大占用內存大小為100M
maxmemory 100mb
2、通過命令修改
Redis支持運行時通過命令動態修改內存大小
//設置Redis最大占用內存大小為100M
127.0.0.1:6379> config set maxmemory 100mb
//獲取設置的Redis能使用的最大內存大小
127.0.0.1:6379> config get maxmemory
如果不設置最大內存大小或者設置最大內存大小為0,在64位操作系統下不限制內存大小,在32位操作系統下最多使用3GB內存
二、Redis的內存淘汰
既然可以設置Redis最大占用內存大小,那么配置的內存就有用完的時候。那在內存用完的時候,還繼續往Redis里面添加數據不就沒內存可用了嗎?
實際上Redis定義了幾種策略用來處理這種情況:
noeviction(默認策略):對於寫請求不再提供服務,直接返回錯誤(DEL請求和部分特殊請求除外) allkeys-lru:從所有key中使用LRU算法進行淘汰 volatile-lru:從設置了過期時間的key中使用LRU算法進行淘汰 allkeys-random:從所有key中隨機淘汰數據 volatile-random:從設置了過期時間的key中隨機淘汰 volatile-ttl:在設置了過期時間的key中,根據key的過期時間進行淘汰,越早過期的越優先被淘汰
當使用volatile-lru、volatile-random、volatile-ttl這三種策略時,如果沒有key可以被淘汰,則和noeviction一樣返回錯誤
三、如何獲取及設置內存淘汰策略
獲取當前內存淘汰策略:
127.0.0.1:6379> config get maxmemory-policy
通過配置文件設置淘汰策略(修改redis.conf文件):
maxmemory-policy allkeys-lru
通過命令修改淘汰策略:
127.0.0.1:6379> config set maxmemory-policy allkeys-lru
四、LRU算法
什么是LRU?
上面說到了Redis可使用最大內存使用完了,是可以使用LRU算法進行內存淘汰的,那么什么是LRU算法呢?
LRU(Least Recently Used),即最近最少使用,是一種緩存置換算法。在使用內存作為緩存的時候,緩存的大小一般是固定的。當緩存被占滿,這個時候繼續往緩存里面添加數據,就需要淘汰一部分老的數據,釋放內存空間用來存儲新的數據。這個時候就可以使用LRU算法了。其核心思想是:如果一個數據在最近一段時間沒有被用到,那么將來被使用到的可能性也很小,所以就可以被淘汰掉。
其原理是維護一個雙向鏈表,key -> node,其中node保存鏈表前后節點關系及數據data。新插入的key時,放在頭部,並檢查是否超出總容量,如果超出則刪除最后的key;訪問key時,無論是查找還是更新,將該Key被調整到頭部。
使用php實現一個簡單的LRU算法
代碼地址:
https://github.com/rogeriopvl/php-lrucache

1 <?php 2 namespace LRUCache; 3 /** 4 * Class that implements the concept of an LRU Cache 5 * using an associative array as a naive hashmap, and a doubly linked list 6 * to control the access and insertion order. 7 * 8 * @author Rogério Vicente 9 * @license MIT (see the LICENSE file for details) 10 */ 11 class LRUCache { 12 // object Node representing the head of the list 13 private $head; 14 // object Node representing the tail of the list 15 private $tail; 16 // int the max number of elements the cache supports 17 private $capacity; 18 // Array representing a naive hashmap (TODO needs to pass the key through a hash function) 19 private $hashmap; 20 /** 21 * @param int $capacity the max number of elements the cache allows 22 */ 23 public function __construct($capacity) { 24 $this->capacity = $capacity; 25 $this->hashmap = array(); 26 $this->head = new Node(null, null); 27 $this->tail = new Node(null, null); 28 $this->head->setNext($this->tail); 29 $this->tail->setPrevious($this->head); 30 } 31 /** 32 * Get an element with the given key 33 * @param string $key the key of the element to be retrieved 34 * @return mixed the content of the element to be retrieved 35 */ 36 public function get($key) { 37 if (!isset($this->hashmap[$key])) { return null; } 38 $node = $this->hashmap[$key]; 39 if (count($this->hashmap) == 1) { return $node->getData(); } 40 // refresh the access 41 $this->detach($node); 42 $this->attach($this->head, $node); 43 return $node->getData(); 44 } 45 /** 46 * Inserts a new element into the cache 47 * @param string $key the key of the new element 48 * @param string $data the content of the new element 49 * @return boolean true on success, false if cache has zero capacity 50 */ 51 public function put($key, $data) { 52 if ($this->capacity <= 0) { return false; } 53 if (isset($this->hashmap[$key]) && !empty($this->hashmap[$key])) { 54 $node = $this->hashmap[$key]; 55 // update data 56 $this->detach($node); 57 $this->attach($this->head, $node); 58 $node->setData($data); 59 } 60 else { 61 $node = new Node($key, $data); 62 $this->hashmap[$key] = $node; 63 $this->attach($this->head, $node); 64 // check if cache is full 65 if (count($this->hashmap) > $this->capacity) { 66 // we're full, remove the tail 67 $nodeToRemove = $this->tail->getPrevious(); 68 $this->detach($nodeToRemove); 69 unset($this->hashmap[$nodeToRemove->getKey()]); 70 } 71 } 72 return true; 73 } 74 /** 75 * Removes a key from the cache 76 * @param string $key key to remove 77 * @return bool true if removed, false if not found 78 */ 79 public function remove($key) { 80 if (!isset($this->hashmap[$key])) { return false; } 81 $nodeToRemove = $this->hashmap[$key]; 82 $this->detach($nodeToRemove); 83 unset($this->hashmap[$nodeToRemove->getKey()]); 84 return true; 85 } 86 /** 87 * Adds a node to the head of the list 88 * @param Node $head the node object that represents the head of the list 89 * @param Node $node the node to move to the head of the list 90 */ 91 private function attach($head, $node) { 92 $node->setPrevious($head); 93 $node->setNext($head->getNext()); 94 $node->getNext()->setPrevious($node); 95 $node->getPrevious()->setNext($node); 96 } 97 /** 98 * Removes a node from the list 99 * @param Node $node the node to remove from the list 100 */ 101 private function detach($node) { 102 $node->getPrevious()->setNext($node->getNext()); 103 $node->getNext()->setPrevious($node->getPrevious()); 104 } 105 } 106 /** 107 * Class that represents a node in a doubly linked list 108 */ 109 class Node { 110 /** 111 * the key of the node, this might seem reduntant, 112 * but without this duplication, we don't have a fast way 113 * to retrieve the key of a node when we wan't to remove it 114 * from the hashmap. 115 */ 116 private $key; 117 // the content of the node 118 private $data; 119 // the next node 120 private $next; 121 // the previous node 122 private $previous; 123 /** 124 * @param string $key the key of the node 125 * @param string $data the content of the node 126 */ 127 public function __construct($key, $data) { 128 $this->key = $key; 129 $this->data = $data; 130 } 131 /** 132 * Sets a new value for the node data 133 * @param string the new content of the node 134 */ 135 public function setData($data) { 136 $this->data = $data; 137 } 138 /** 139 * Sets a node as the next node 140 * @param Node $next the next node 141 */ 142 public function setNext($next) { 143 $this->next = $next; 144 } 145 /** 146 * Sets a node as the previous node 147 * @param Node $previous the previous node 148 */ 149 public function setPrevious($previous) { 150 $this->previous = $previous; 151 } 152 /** 153 * Returns the node key 154 * @return string the key of the node 155 */ 156 public function getKey() { 157 return $this->key; 158 } 159 /** 160 * Returns the node data 161 * @return mixed the content of the node 162 */ 163 public function getData() { 164 return $this->data; 165 } 166 /** 167 * Returns the next node 168 * @return Node the next node of the node 169 */ 170 public function getNext() { 171 return $this->next; 172 } 173 /** 174 * Returns the previous node 175 * @return Node the previous node of the node 176 */ 177 public function getPrevious() { 178 return $this->previous; 179 } 180 }
假如一次訪問key 1,5,1,3,5,2,4,1,2
五、LRU在Redis中的實現
近似LRU算法
Redis使用的是近似LRU算法,它跟常規的LRU算法還不太一樣。近似LRU算法通過隨機采樣法淘汰數據,每次隨機出5(默認)個key,從里面淘汰掉最近最少使用的key。
可以通過maxmemory-samples參數修改采樣數量: 例:maxmemory-samples 10 maxmenory-samples配置的越大,淘汰的結果越接近於嚴格的LRU算法
Redis為了實現近似LRU算法,給每個key增加了一個額外增加了一個24bit的字段,用來存儲該key最后一次被訪問的時間。
Redis3.0對近似LRU的優化
Redis3.0對近似LRU算法進行了一些優化。新算法會維護一個候選池(大小為16),池中的數據根據訪問時間進行排序,第一次隨機選取的key都會放入池中,隨后每次隨機選取的key只有在訪問時間小於池中最小的時間才會放入池中,直到候選池被放滿。當放滿后,如果有新的key需要放入,則將池中最后訪問時間最大(最近被訪問)的移除。
當需要淘汰的時候,則直接從池中選取最近訪問時間最小(最久沒被訪問)的key淘汰掉就行。
LRU算法的對比
我們可以通過一個實驗對比各LRU算法的准確率,先往Redis里面添加一定數量的數據n,使Redis可用內存用完,再往Redis里面添加n/2的新數據,這個時候就需要淘汰掉一部分的數據,如果按照嚴格的LRU算法,應該淘汰掉的是最先加入的n/2的數據。
生成如下各LRU算法的對比圖:
你可以看到圖中有三種不同顏色的點:
淺灰色是被淘汰的數據 灰色是沒有被淘汰掉的老數據 綠色是新加入的數據
我們能看到Redis3.0采樣數是10的時候生成的圖最接近於嚴格的LRU。而同樣使用5個采樣數,Redis3.0也要優於Redis2.8。
Redis並沒有使用嚴格的LRU算法,因為維護一個那么大的雙向鏈表需要的內存空間較大。
顯然LRU的缺陷是明顯的,最新訪問的數據被當做熱數據顯然是不合理的,熱數據顧名思義就是被訪問頻次叫高的數據,顯然是不同的概念
六、LFU算法
LFU算法是Redis4.0里面新加的一種淘汰策略。它的全稱是Least Frequently Used,它的核心思想是根據key的最近被訪問的頻率進行淘汰,很少被訪問的優先被淘汰,被訪問的多的則被留下來。
LFU算法能更好的表示一個key被訪問的熱度。假如你使用的是LRU算法,一個key很久沒有被訪問到,只剛剛是偶爾被訪問了一次,那么它就被認為是熱點數據,不會被淘汰,而有些key將來是很有可能被訪問到的則被淘汰了。如果使用LFU算法則不會出現這種情況,因為使用一次並不會使一個key成為熱點數據。LFU原理使用計數器來對key進行排序,每次key被訪問的時候,計數器增大。計數器越大,可以約等於訪問越頻繁。具有相同引用計數的數據塊則按照時間排序。
LFU一共有兩種策略:
volatile-lfu:在設置了過期時間的key中使用LFU算法淘汰key allkeys-lfu:在所有的key中使用LFU算法淘汰數據
設置使用這兩種淘汰策略跟前面講的一樣,不過要注意的一點是這兩種策略只能在Redis4.0及以上設置,如果在Redis4.0以下設置會報錯
新加入數據插入到隊列尾部(因為引用計數為1);
隊列中的數據被訪問后,引用計數增加,隊列重新排序;
當需要淘汰數據時,將已經排序的列表最后的數據塊刪除。
l 命中率
一般情況下,LFU效率要優於LRU,且能夠避免周期性或者偶發性的操作導致緩存命中率下降的問題。但LFU需要記錄數據的歷史訪問記錄,一旦數據訪問模式改變,LFU需要更長時間來適用新的訪問模式,即:LFU存在歷史數據影響將來數據的“緩存污染”效用。
l 復雜度
需要維護一個隊列記錄所有數據的訪問記錄,每個數據都需要維護引用計數。
l 代價
需要記錄所有數據的訪問記錄,內存消耗較高;需要基於引用計數排序,性能消耗較高。
LFC算法存在兩個問題:
1、在LRU算法中可以維護一個雙向鏈表,然后簡單的把被訪問的節點移至鏈表開頭,但在LFU中是不可行的,節點要嚴格按照計數器進行排序,新增節點或者更新節點位置時,時間復雜度可能達到O(N)。
2、只是簡單的增加計數器的方法並不完美。訪問模式是會頻繁變化的,一段時間內頻繁訪問的key一段時間之后可能會很少被訪問到,只增加計數器並不能體現這種趨勢。
第一個問題很好解決,可以借鑒LRU實現的經驗,維護一個待淘汰key的pool。第二個問題的解決辦法是,記錄key最后一個被訪問的時間,然后隨着時間推移,降低計數器。
更多請參考:https://www.zhangshengrong.com/p/zD1yQg6b1r/
zz:https://blog.csdn.net/raoxiaoya/article/details/103141022