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

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

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

概述

Redis也會(huì)因?yàn)閮?nèi)存不足而產(chǎn)生錯(cuò)誤 , 也可能因?yàn)榛厥者^久而導(dǎo)致系統(tǒng)長期的停頓,因此掌握?qǐng)?zhí)行回收策略十分有必要。在 Redis 的配置文件中,當(dāng) Redis 的內(nèi)存達(dá)到規(guī)定的最大值時(shí),允許配置 6 種策略中的一種進(jìn)行淘汰鍵值,并且將一些鍵值對(duì)進(jìn)行回收。

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

主動(dòng)清理策略

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

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

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

【 針對(duì)所有的key做處理】

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

【 不處理 (默認(rèn))】

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

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

策略選擇

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

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

需要指出的是 : LRU 算法或者 TTL 算法都是不是很精確算法,而是一個(gè)近似的算法。 Redis 不會(huì)通過對(duì)全部的鍵值對(duì)進(jìn)行比較來確定最精確的時(shí)間值,從而確定刪除哪個(gè)鍵值對(duì) , 因?yàn)檫@將消耗太多的時(shí)間 , 導(dǎo)致回收垃圾執(zhí)行的時(shí)間太長 , 造成服務(wù)停頓.

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

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

maxmemory-sample

而在Redis 的默認(rèn)配置文件中 , 存在著參數(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

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

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

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

對(duì)于垃圾回收的策略,還需要注意的是回收的時(shí)間,因?yàn)樵?Redis 對(duì)垃圾的回收期間, 會(huì)造成系統(tǒng)緩慢。

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

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

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

相關(guān)文章

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

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

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

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

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

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

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

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

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

    Redis使用ZSET實(shí)現(xiàn)消息隊(duì)列的項(xiàng)目實(shí)踐

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

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

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

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

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

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

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

    redis列表類型_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

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

    IDEA中的Redis插件連接Redis服務(wù)器

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

最新評(píng)論