文章前言
提到記憶體管理,我們就需要考慮Redis的記憶體過期策略和記憶體淘汰機制,該文章便從這兩方面入手,分享一些在Redis記憶體方面相關的基礎知識,
文章中使用的示例版本為Redis5.0版本,
記憶體過期策略
記憶體過期策略主要的作用就是,在快取過期之后,能夠及時的將失效的快取從記憶體中洗掉,以減少記憶體的無效暫用,達到釋放記憶體的目的,
過期策略分類
Redis記憶體過期策略分為三類,定時策略、惰性策略和定期策略,
定時策略
含義:在設定key的過期時間的同時,為該key創建一個定時器,讓定時器在key的過期時間來臨時,對key進行洗掉,
優點:保證記憶體被盡快釋放,減少無效的快取暫用記憶體,
缺點:若過期key很多,洗掉這些key會占用很多的CPU時間,在CPU時間緊張的情況下,CPU不能把所有的時間用來做要緊的事兒,還需要去花時間洗掉這些key,定時器的創建耗時,若為每一個設定過期時間的key創建一個定時器(將會有大量的定時器產生),性能影響嚴重,一般來說,是不會選擇該策略模式,
惰性策略
含義:key過期的時候不洗掉,每次從資料庫獲取key的時候去檢查是否過期,若過期,則洗掉,回傳null,
優點:洗掉操作只發生在從資料庫取出key的時候發生,而且只洗掉當前key,所以對CPU時間的占用是比較少的,而且此時的洗掉是已經到了非做不可的地步(如果此時還不洗掉的話,我們就會獲取到了已經過期的key了),
缺點:若大量的key在超出超時時間后,很久一段時間內,都沒有被獲取過,此時的無效快取是永久暫用在記憶體中的,那么可能發生記憶體泄露(無用的垃圾占用了大量的記憶體),
定期策略
含義:每隔一段時間對設定了快取時間的key進行檢測,如果可以已經失效,則從記憶體中洗掉,如果未失效,則不作任何處理,
優點:通過限制洗掉操作的時長和頻率,來減少洗掉操作對CPU時間的占用--處理"定時洗掉"的缺點
定期洗掉過期key--處理"惰性洗掉"的缺點,
缺點:在記憶體友好方面,不如"定時洗掉",因為是隨機遍歷一些key,因此存在部分key過期,但遍歷key時,沒有被遍歷到,過期的key仍在記憶體中,在CPU時間友好方面,不如"惰性洗掉",定期洗掉也會暫用CPU性能消耗,
難點:合理設定洗掉操作的執行時長(每次洗掉執行多長時間)和執行頻率(每隔多長時間做一次洗掉)(這個要根據服務器運行情況來定了)
該方式不是去便利所有的ky,而是隨機抽取一些key做過期檢測,
策略注意事項
過期策略對持久化存盤的影響
持久化存盤,指的是將記憶體的快取永久存在磁盤中,也就是說我們的AOF和RDB持久化存盤方式,因為該兩種方式,將記憶體中的資料寫入磁盤,這時候就需要考慮到我們過期的快取是否會被寫入到磁盤中?如果寫入磁盤又是怎么處理的?
RDB持久化
持久化key之前,會檢查是否過期,過期的key不進入RDB檔案,
資料載入資料庫之前,會對key先進行過期檢查,如果過期,不匯入資料庫(主庫情況),
AOF持久化
當key過期后,還沒有被洗掉,此時進行執行持久化操作(該key是不會進入aof檔案的,因為沒有發生修改命令),
當key過期后,在發生洗掉操作時,程式會向aof檔案追加一條del命令(在將來的以aof檔案恢復資料的時候該過期的鍵就會被刪掉),
因為AOF方式,向存盤檔案追加的是Redis的操作命令,而不是具體的資料,然而RDB確是存盤的安全的二進制內容,
重寫時,會先判斷key是否過期,已過期的key不會重寫到aof檔案,
即使在重寫時,不驗證是否過期,然而追加了del命令,測驗無效的key同樣會被洗掉,判斷的情況是為了防止沒有加入del命令的key,
記憶體淘汰機制
定義說明
記憶體淘汰機制針對是記憶體不足的情況下的一種Redis處理機制,例如,當前的Redis存盤已經超過記憶體限制了,然而我們的業務還在繼續往Redis里面追加快取內容,這時候Redis的淘汰機制就起到作用了,
淘汰機制分類
根據redis.conf的組態檔中,我們可以得出,主要分為如下六種淘汰機制,
# volatile-lru -> Evict using approximated LRU among the keys with an expire set.
# allkeys-lru -> Evict any key using approximated LRU.
# volatile-lfu -> Evict using approximated LFU among the keys with an expire set.
# allkeys-lfu -> Evict any key using approximated LFU.
# volatile-random -> Remove a random key among the ones with an expire set.
# allkeys-random -> Remove a random key, any key.
# volatile-ttl -> Remove the key with the nearest expire time (minor TTL)
# noeviction -> Don't evict anything, just return an error on write operations.
這六種機制主要是什么意思內,下面是分別針對上面的幾種機制做一個說明,
volatile-lru:當記憶體不足以容納新寫入資料時,在設定了過期時間的鍵空間中,移除最近最少使用的key,
allkeys-lru:當記憶體不足以容納新寫入資料時,在鍵空間中,移除最近最少使用的key(這個是最常用的),
volatile-lfu:當記憶體不足以容納新寫入資料時,在過期密集的鍵中,使用LFU演算法進行洗掉key,
allkeys-lfu:當記憶體不足以容納新寫入資料時,使用LFU演算法移除所有的key,
volatile-random:當記憶體不足以容納新寫入資料時,在設定了過期的鍵中,隨機洗掉一個key,
allkeys-random:當記憶體不足以容納新寫入資料時,隨機洗掉一個或者多個key,
volatile-ttl:當記憶體不足以容納新寫入資料時,在設定了過期時間的鍵空間中,有更早過期時間的key優先移除,
noeviction:當記憶體不足以容納新寫入資料時,新寫入操作會報錯,
記憶體管理配置翻譯
# Set a memory usage limit to the specified amount of bytes.
#將記憶體使用限制設定為指定的位元組數,
# When the memory limit is reached Redis will try to remove keys
#當達到記憶體限制時,Redis將嘗試洗掉密鑰
# according to the eviction policy selected (see maxmemory-policy).
#根據所選的逐出策略(請參閱maxmemory策略),
#
#
# If Redis can't remove keys according to the policy, or if the policy is
#如果Redis不能根據策略洗掉密鑰,或者如果策略是
# set to 'noeviction', Redis will start to reply with errors to commands
#設定為“noeviction”時,Redis將開始對命令進行錯誤的應答
# that would use more memory, like SET, LPUSH, and so on, and will continue
#這將使用更多記憶體,如SET、LPUSH等,并將繼續
# to reply to read-only commands like GET.
#回復像GET這樣的只讀命令,
#
#
# This option is usually useful when using Redis as an LRU or LFU cache, or to
#當將Redis用作LRU或LFU快取或
# set a hard memory limit for an instance (using the 'noeviction' policy).
#為實體設定硬記憶體限制(使用“noeviction”策略),
#
#
# WARNING: If you have replicas attached to an instance with maxmemory on,
#警告:如果將副本附加到啟用了maxmemory的實體,
# 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.
#直到資料庫完全清空,
#
# 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
#限制maxmemory,以便系統上有一些可用的RAM用于復制
# output buffers (but this is not needed if the policy is 'noeviction').
#輸出緩沖區(但如果策略為“noeviction”,則不需要此緩沖區),
# maxmemory <bytes>
#最大記憶體<位元組>
# MAXMEMORY POLICY: how Redis will select what to remove when maxmemory
#MAXMEMORY策略:當MAXMEMORY時Redis如何選擇要洗掉的內容
# is reached. You can select among five behaviors:
#已到達,您可以從五種行為中進行選擇:
#
# volatile-lru -> Evict using approximated LRU among the keys with an expire set.
#volatile lru->在具有expire集的密鑰中使用近似的lru進行逐出,
# allkeys-lru -> Evict any key using approximated LRU.
#allkeys lru->使用近似的lru逐出任何鍵,
# volatile-lfu -> Evict using approximated LFU among the keys with an expire set.
#volatile lfu->在具有expire集的密鑰中使用近似的lfu進行逐出,
# allkeys-lfu -> Evict any key using approximated LFU.
#allkeys lfu->使用近似的lfu逐出任何鍵,
# volatile-random -> Remove a random key among the ones with an expire set.
#volatile random->從具有expire集的密鑰中洗掉一個隨機密鑰,
# allkeys-random -> Remove a random key, any key.
#allkeys random->移除一個隨機鍵,任意鍵,
# volatile-ttl -> Remove the key with the nearest expire time (minor TTL)
#volatile ttl->洗掉最接近過期時間的密鑰(minor ttl)
# noeviction -> Don't evict anything, just return an error on write operations.
#noeviction->不要逐出任何內容,只要在寫操作時回傳一個錯誤,
# LRU means Least Recently Used
#LRU表示最近最少使用
# LFU means Least Frequently Used
#LFU表示使用頻率最低
# Both LRU, LFU and volatile-ttl are implemented using approximated
#LRU、LFU和volatile ttl都是用近似方法實作的
# randomized algorithms.
#隨機演算法,
# Note: with any of the above policies, Redis will return an error on write
#注意:如果使用上述任何策略,Redis將在寫入時回傳錯誤
# operations, when there are no suitable keys for eviction.
#操作,當沒有合適的鑰匙驅逐,
#
#At the date of writing these commands are: set setnx setex append
#在撰寫這些命令的日期是:set setnx setex append
#incr decr rpush lpush rpushx lpushx linsert lset rpoplpush sadd
#增加/減少脈沖低壓脈沖rpushx lpushx linsert lset RPOPPLPUSH sadd
#sinter sinterstore sunion sunionstore sdiff sdiffstore zadd zincrby
#sinter sinterstore sunion sunionstore sdiffstore zadd zincrby
#zunionstore zinterstore hset hsetnx hmset hincrby incrby decrby
#zunionstore zinterstore hset hsetnx hmset hincrby incrby遞減
#getset mset msetnx exec sort
#getset mset msetnx exec排序
#
#
# The default is:
#默認值為:
#
#
# maxmemory-policy noeviction
#maxmemory策略不可用
# LRU, LFU and minimal TTL algorithms are not precise algorithms but approximated
#LRU、LFU和最小TTL演算法不是精確演算法而是近似演算法
# 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
#準確度,對于默認的Redis將檢查五個鍵并選擇一個
# used less recently, you can change the sample size using the following
#最近使用較少,可以使用以下命令更改樣本大小
# configuration directive.
#配置指令,
#
#
# The default of 5 produces good enough results. 10 Approximates very closely
#默認值為5會產生足夠好的結果,10非常接近
# true LRU but costs more CPU. 3 is faster but not very accurate.
#真正的外場可更換單元,但占用更多的CPU,3更快,但不是很準確,
# maxmemory-samples 5
#maxmemory示例5
# Starting from Redis 5, by default a replica will ignore its maxmemory setting
#從Redis 5開始,默認情況下副本將忽略其maxmemory設定
# (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.
#DEL命令在主服務器端作為密鑰識訓,
#
# 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).
#你在做什么),
#
# 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
#記憶體大于通過maxmemory設定的記憶體(有某些緩沖區可能
# 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.
#配置的maxmemory設定,
#
#
# replica-ignore-maxmemory yes
#復制副本忽略maxmemory是
Redis命令
這里總結幾個Redis中常用的與時間有關的命令,
exists key:判斷鍵是否存在,如果存在則返回1,不存在則回傳0;
expire key:給鍵設定過期時間,單位s(秒);
ttl key:回傳鍵剩余的過期時間,單位s(秒);當鍵不存在是回傳-2;存在并且未設定過期時間,回傳-1;如果回傳≥0,則該回傳值則為過期的時間;
ptt key:回傳鍵剩余的過期時間,單位ms(毫秒);當鍵不存在是回傳-2;存在并且未設定過期時間,回傳-1;如果回傳≥0,則該回傳值則為過期的時間;
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/1180.html
標籤:其它
上一篇:dgraph 使用簡介
