最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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種:

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

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

【 針對所有的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ù)以百萬甚至更多的用戶 , 所以往往需要設置回收策略。

策略選擇

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

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

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

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

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

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

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

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

相關文章

  • Redis緩存常用4種策略原理詳解

    Redis緩存常用4種策略原理詳解

    這篇文章主要介紹了Redis緩存常用4種策略原理詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-08-08
  • Redis的數(shù)據(jù)復制過程詳解

    Redis的數(shù)據(jù)復制過程詳解

    Redis 的復制功能分為同步(sync)和命令傳播(command propagate)這兩個操作,這篇文章主要介紹了Redis的數(shù)據(jù)復制,需要的朋友可以參考下
    2022-12-12
  • Redis Sentinel服務配置流程(詳解)

    Redis Sentinel服務配置流程(詳解)

    下面小編就為大家?guī)硪黄猂edis Sentinel服務配置流程(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • Redis MCP 安裝與配置指南

    Redis MCP 安裝與配置指南

    本文將詳細介紹如何安裝和配置 Redis MCP,包括快速啟動、源碼安裝、Docker 安裝、以及相關的配置參數(shù)和環(huán)境變量設置,感興趣的朋友一起看看吧
    2025-07-07
  • redis實現(xiàn)加鎖的幾種方法示例詳解

    redis實現(xiàn)加鎖的幾種方法示例詳解

    這篇文章主要給大家介紹了關于redis實現(xiàn)加鎖的幾種方法,加鎖命令分別是INCR、SETNX和SET,文中給出了詳細的示例代碼,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。
    2017-09-09
  • 如何使用jwt+redis實現(xiàn)單點登錄

    如何使用jwt+redis實現(xiàn)單點登錄

    文章介紹基于Redis的單點登錄實現(xiàn),通過JWT攔截器管理token生成、驗證、更新及注銷,利用cookie與Redis同步確保異地登錄安全,防止token被偽造使用,感興趣的朋友跟隨小編一起看看吧
    2025-08-08
  • Redis數(shù)據(jù)結(jié)構(gòu)SortedSet的底層原理解析

    Redis數(shù)據(jù)結(jié)構(gòu)SortedSet的底層原理解析

    這篇文章主要介紹了Redis數(shù)據(jù)結(jié)構(gòu)SortedSet的底層原理解析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • Redis如何優(yōu)雅的刪除特定前綴key

    Redis如何優(yōu)雅的刪除特定前綴key

    這篇文章主要給大家介紹了關于Redis如何優(yōu)雅的刪除特定前綴key的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Redis具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-06-06
  • 解決Redis開啟遠程訪問及密碼問題

    解決Redis開啟遠程訪問及密碼問題

    這篇文章主要介紹了Redis開啟遠程訪問及密碼的教程,文中給大家提到了Redis啟動報錯解決方法,需要的朋友可以參考下
    2019-10-10
  • Redis之ZipList壓縮列表的使用

    Redis之ZipList壓縮列表的使用

    這篇文章主要介紹了Redis之ZipList壓縮列表的使用,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-06-06

最新評論

香河县| 曲沃县| 株洲县| 汨罗市| 环江| 准格尔旗| 孝昌县| 巴青县| 绩溪县| 新宾| 吴江市| 景德镇市| 邵阳市| 吉安县| 秦皇岛市| 亳州市| 宁国市| 扎兰屯市| 延寿县| 安乡县| 河间市| 吉水县| 乐平市| 灵石县| 康定县| 滁州市| 瑞丽市| 额敏县| 克拉玛依市| 昭通市| 沂源县| 彰化县| 恩平市| 烟台市| 邻水| 合肥市| 北安市| 景泰县| 泰州市| 布拖县| 香港|