欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Redis內(nèi)存回收策略

 更新時間:2021年11月24日 16:03:28   作者:小小工匠  
Redis也會因為內(nèi)存不足而產(chǎn)生錯誤?,?也可能因為回收過久而導致系統(tǒng)長期的停頓,因此掌握執(zhí)行回收策略十分有必要,具有一定的參考價值,感興趣的可以了解一下

概述

Redis也會因為內(nèi)存不足而產(chǎn)生錯誤 , 也可能因為回收過久而導致系統(tǒng)長期的停頓,因此掌握執(zhí)行回收策略十分有必要。在 Redis 的配置文件中,當 Redis 的內(nèi)存達到規(guī)定的最大值時,允許配置 6 種策略中的一種進行淘汰鍵值,并且將一些鍵值對進行回收。

maxmemory-policy 參數(shù)

# Set a memory usage limit to the specified amount of bytes.
# When the memory limit is reached Redis will try to remove keys
# according to the eviction policy selected (see maxmemory-policy).
#
# 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.
#
# 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).
#
# 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>

# MAXMEMORY POLICY: how Redis will select what to remove when maxmemory
# is reached. You can select among five behaviors:
#
# 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.
#
# LRU means Least Recently Used
# LFU means Least Frequently Used
#
# 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.
#
#       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
#
# The default is:
#
# maxmemory-policy noeviction

主動清理策略

主動清理策略在Redis 4.0 之前一共實現(xiàn)了 6 種內(nèi)存淘汰策略,在 4.0 之后,又增加了 2 種策略,總共8種:

【針對設(shè)置了過期時間的key做處理】

  • volatile-ttl:在篩選時,會針對設(shè)置了過期時間的鍵值對,根據(jù)過期時間的先后進行刪除,越早過期的越先被刪除。
  • volatile-random:就像它的名稱一樣,在設(shè)置了過期時間的鍵值對中,進行隨機刪除。
  • volatile-lru:會使用 LRU 算法篩選設(shè)置了過期時間的鍵值對刪除。
  • volatile-lfu:會使用 LFU 算法篩選設(shè)置了過期時間的鍵值對刪除

【 針對所有的key做處理】

  • allkeys-random:從所有鍵值對中隨機選擇并刪除數(shù)據(jù)。
  • allkeys-lru:使用 LRU 算法在所有數(shù)據(jù)中進行篩選刪除。
  • allkeys-lfu:使用 LFU 算法在所有數(shù)據(jù)中進行篩選刪除。

【 不處理 (默認)】

noeviction:不會剔除任何數(shù)據(jù),拒絕所有寫入操作并返回客戶端錯誤信息"(error) OOM command not allowed when used memory",此時Redis只響應讀操作。

Redis 在默認情況下會采用 noeviction 策略。換句話說,如果內(nèi)存己滿 , 則不再提供寫入操作 , 而只提供讀取操作 。 顯然這往往并不能滿足我們的要求,因為對于互聯(lián)網(wǎng)系統(tǒng)而言 , 常常會涉及數(shù)以百萬甚至更多的用戶 , 所以往往需要設(shè)置回收策略。

策略選擇

LRU 算法(Least Recently Used,最近最少使用):淘汰很久沒被訪問過的數(shù)據(jù),以最近一次訪問時間作為參考

LFU 算法(Least Frequently Used,最不經(jīng)常使用):淘汰最近一段時間被訪問次數(shù)最少的數(shù)據(jù),以次數(shù)作為參考

需要指出的是 : LRU 算法或者 TTL 算法都是不是很精確算法,而是一個近似的算法。 Redis 不會通過對全部的鍵值對進行比較來確定最精確的時間值,從而確定刪除哪個鍵值對 , 因為這將消耗太多的時間 , 導致回收垃圾執(zhí)行的時間太長 , 造成服務停頓.

當存在熱點數(shù)據(jù)時,LRU的效率很好,但偶發(fā)性的、周期性的批量操作會導致LRU命中率急劇下降,緩存污染情況比較嚴重。這時使用LFU可能更好點

根據(jù)自身業(yè)務類型,配置好maxmemory-policy(默認是noeviction),推薦使用volatile-lru。

maxmemory-sample

而在Redis 的默認配置文件中 , 存在著參數(shù) maxmemory-sample

# 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.
#
# The default of 5 produces good enough results. 10 Approximates very closely
# true LRU but costs more CPU. 3 is faster but not very accurate.
#
# maxmemory-samples 5

當設(shè)置 maxmemory-samples越大,則 Redis 刪除的就越精確,但是與此同時帶來不利的是, Redis 也就需要花更多的時去計算匹配更為精確的值 。

回收超時策略的缺點是必須指明超時的鍵值對 ,這會給程序開發(fā)帶來一些設(shè)置超時的代碼,無疑增加了開發(fā)者的工作量。

對所有的鍵值對進行回收,有可能把正在使用的鍵值對刪掉,增加了存儲的不穩(wěn)定性。

對于垃圾回收的策略,還需要注意的是回收的時間,因為在 Redis 對垃圾的回收期間, 會造成系統(tǒng)緩慢。

因此,控制其回收時間有一定好處,只是這個時間不能過短或過長。過短則會造成回收次數(shù)過于頻繁,過長則導致系統(tǒng)單次垃圾回收停頓時間過長,都不利于系統(tǒng)的穩(wěn)定性,這些都需要設(shè)計者在實際的工作中進行思考 。

如果不設(shè)置最大內(nèi)存,當 Redis 內(nèi)存超出物理內(nèi)存限制時,內(nèi)存的數(shù)據(jù)會開始和磁盤產(chǎn)生頻繁的交換 (swap),會讓 Redis 的性能急劇下降。

到此這篇關(guān)于 Redis內(nèi)存回收策略的文章就介紹到這了,更多相關(guān) Redis內(nèi)存回收內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!?

相關(guān)文章

  • 面試常問:如何保證Redis緩存和數(shù)據(jù)庫的數(shù)據(jù)一致性

    面試常問:如何保證Redis緩存和數(shù)據(jù)庫的數(shù)據(jù)一致性

    在實際開發(fā)過程中,緩存的使用頻率是非常高的,只要使用緩存和數(shù)據(jù)庫存儲,就難免會出現(xiàn)雙寫時數(shù)據(jù)一致性的問題,那我們又該如何解決呢
    2021-09-09
  • Linux中Redis安裝部署的操作步驟

    Linux中Redis安裝部署的操作步驟

    公司一直在使用redis集群,尋思著自己也部署一套練練手,下面這篇文章主要給大家介紹了關(guān)于Linux中Redis安裝部署的操作步驟,需要的朋友可以參考下
    2022-04-04
  • Win10配置redis服務實現(xiàn)過程詳解

    Win10配置redis服務實現(xiàn)過程詳解

    這篇文章主要介紹了Win10配置redis服務實現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-07-07
  • Redis全局ID生成器的實現(xiàn)

    Redis全局ID生成器的實現(xiàn)

    全局ID生成器,是一種在分布式系統(tǒng)下用來生成全局唯一ID的工具,本文主要介紹了Redis全局ID生成器的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-06-06
  • Redis使用ZSET實現(xiàn)消息隊列的項目實踐

    Redis使用ZSET實現(xiàn)消息隊列的項目實踐

    本文主要介紹了Redis使用ZSET實現(xiàn)消息隊列的項目實踐,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-07-07
  • Windows中Redis安裝配置流程并實現(xiàn)遠程訪問功能

    Windows中Redis安裝配置流程并實現(xiàn)遠程訪問功能

    很多在windows環(huán)境中安裝Redis總是出錯,今天小編抽空給大家分享在Windows中Redis安裝配置流程并實現(xiàn)遠程訪問功能,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2021-06-06
  • Redis監(jiān)控工具RedisInsight安裝與使用

    Redis監(jiān)控工具RedisInsight安裝與使用

    這篇文章主要為大家介紹了Redis監(jiān)控工具RedisInsight的安裝步驟與使用方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步
    2022-03-03
  • Redis 集群搭建和簡單使用教程

    Redis 集群搭建和簡單使用教程

    集群技術(shù)是構(gòu)建高性能網(wǎng)站架構(gòu)的重要手段,下面這篇文章主要介紹了Redis 集群搭建和簡單使用教程,文中通過示例代碼和圖片介紹的很想,對大家具有一定的參考價值,有需要的朋友們下面來一起看看吧。
    2017-02-02
  • redis列表類型_動力節(jié)點Java學院整理

    redis列表類型_動力節(jié)點Java學院整理

    這篇文章主要為大家詳細介紹了redis列表類型的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • IDEA中的Redis插件連接Redis服務器

    IDEA中的Redis插件連接Redis服務器

    本文主要介紹了IDEA中的Redis插件連接Redis服務器,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-02-02

最新評論