当前位置:网站首页>Redis memory obsolescence strategy

Redis memory obsolescence strategy

2022-06-12 05:41:00 Xicai pengyuyan

stay redid Used as a cache , When out of memory ,Redis Cache according to the configuration keys, Ensure that subsequent writes can succeed .

Memory maintenance solution

redis The maximum memory size is not set in the configuration of
If Redis The maximum memory size is not set or is set to 0, stay 64 Under the operating system Redis Do not limit the size of memory used , stay 32 Bit operating system is the most commonly used 3/4 Memory space

Memory operation command :
config get maxmemory : View maximum memory
config set maxmemory value : Set size
info memory : View the current Redis Memory usage command
 Insert picture description here
The main memory maintenance schemes are expiration strategy and memory elimination strategy
Expiration policy users process expired cache data
The memory obsolescence policy is used to Data requiring additional space when memory space is insufficient

Expiration strategy

redis Processing of expired data in , There are usually three ways :
1、 Timed expiration : Set the expiration time for each key Need to build a timer , When the expiration time is reached, it will be cleared immediately . This method is memory friendly , Yes CPU No optimization , It will take up a lot of CPU Resources to process expired data
2、 Inertia expires : Only when visiting one key when , To judge key Is it overdue , The scheme maximizes the savings CPU resources , But it will occupy memory resources ( Yes CPU friendly , Not memory friendly )
3、 Expire regularly : Every once in a while , Will scan certain data expires Some data in the dictionary , And clear the expired data

expires The dictionary will save all data that sets the expiration time .

Memory retirement strategy

When there is not enough memory ( Memory usage has exceeded mexmemory Parameter setting value is ), You need to process newly written data that requires extra space
volatile-lru: From the dataset that sets the expiration time (expries) Select the least recently used data to eliminate
volatile-ttl: From the dataset that sets the expiration time (expires) The data to be expired will be eliminated ,ttl The larger the value of, the more priority is to be eliminated
volatile-random: Set expiration time from dataset (expries) Randomly selected data in
allkeys-lru: From the data set (dict) Select the least recently used data in , The data to be eliminated by this strategy is all key Set , Not expired key
allkeys-random: From the data set (dict) Choose any data to eliminate
noeviction: It is forbidden to delete data , When out of memory , The new write operation will report an error , The request can proceed , Ensure that the inserted data will not be lost

The main strategy : It is divided into LRU Eliminate 、TTL Eliminate 、Random Eliminate
LRU Strategy ( Recently at least use ), The core idea , If the data has been accessed recently , Then the probability of being visited in the future will be higher
TTL Strategy : stay Redis Data set that sets the expiration time in expries Pick data from , according to ttl Expiration time for obsolescence , The higher the value, the earlier the elimination
Random strategy : Delete the random data in the data set
LFU Strategy :Redis 4.0 New policy in ,( Recently at least use ) The core idea : If the data is rarely accessed in the recent period of time , So it's less likely that the data will be accessed in the future , therefore , When the space is full , The data with the lowest access frequency will be eliminated first
LRU And LFU Is different :LRU Is the least recently used page replacement algorithm (Least Recently Used), That is, the first to eliminate the longest unused pages !LFU Is the most recently used page replacement algorithm (Least Frequently Used), That is, eliminate the least visited pages in a given period of time !

原网站

版权声明
本文为[Xicai pengyuyan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010614358990.html