当前位置:网站首页>The elimination strategy of redis
The elimination strategy of redis
2022-07-05 15:04:00 【Back end regular developers】
List of articles
Redis The data has been set TTL, It has been deleted after expiration ? Why is there a so-called elimination strategy ? For this reason, we need to start from redis We're talking about .
One 、 Expiration strategy
1. Delete periodically
redis Will set the expiration time for each key Put it in a separate dictionary , In the future, the dictionary will be periodically traversed to delete the expired key.
Redis By default, there will be ten expiration scans per second (100ms once ), Expiration scan does not traverse all of the key, It's a simple greedy strategy .
Randomly... From out of date dictionaries 20 individual key;
Delete this 20 individual key Which has expired key;
If it's overdue key Ratio of more than 1/4, Then repeat the steps 1;
redis The default is every time 100ms Just randomly select some key, Check if it is overdue , Delete if expired . Note that this is a random sample . Why random ? Think about what if redis Hundreds of thousands of... Have been saved key , every other 100ms Just go through all the key Words , Will give you CPU Bring a lot of load .
2. Lazy deletion
The so-called lazy policy is to access this on the client side key When ,redis Yes key Check the expiration date of , Delete immediately if expired , I'm not going to give you anything back .
Regular deletion can cause many expiration key The time has not been deleted . So there's lazy delete . If your expired key, By regular deletion has not been deleted , Still in memory , Unless your system checks that key, Will be redis Delete . This is called lazy delete , That is, when you take the initiative to check the expired key when , If you find that key Out of date , Delete it immediately , Don't return anything .
summary : Periodic deletion is centralized processing , Lazy delete is a piecemeal process .
Two 、 Why the elimination strategy is needed
With the above expiration policy explanation , It's easy to understand why elimination strategies are needed , Because neither regular sampling deletion nor lazy deletion is a completely accurate deletion , There will still be key Scenes that have not been deleted , So we need to replace the memory strategy .
In the use of Redis In the process of , When Redis After the cache is full ,Redis The data will be eliminated according to the configured elimination strategy . from Redis4.0 After that, there are altogether 8 Kind of elimination strategy . Let's take a look at each of them .
3、 ... and 、 Memory retirement strategy
1. noeviction
No data obsolescence , It's also Redis Default configuration . At this time , When the cache is full , Another request to write in ,Redis No more services , Direct return error .
2.volatile-random
When the cache is full , Randomly delete key value pairs with expiration time set .
3.volatile-ttl
When the cache is full , The key value alignment with expiration time is set , Delete according to the order of expiration time , The earlier the expiration date, the earlier it is deleted .
4.volatile-lru
When the cache is full , For key value pairs with expiration time set , use LRU The algorithm is eliminated , Not familiar with LRU You can read this article .
5.volatile-lfu
When the cache is full , For key value pairs with expiration time set , use LFU Algorithm for elimination .
6.allkeys-random
When the cache is full , Randomly select and delete data from all key value pairs .
7.allkeys-lru
After the cache is full , Use LRU The algorithm filters and deletes all data .
8.allkeys-lfu
When the cache is full , Use LRU The algorithm filters and deletes all data .
In daily use , Configure corresponding strategies mainly according to your data requirements , Here I give you three suggestions .
- We give priority to allkeys-lru Strategy . such , We can use LRU Algorithm to eliminate those less commonly used data , Put the most commonly used ones in the cache , So as to improve the performance of the application . If your data has a clear distinction between hot and cold , I suggest you use allkeys-lru Strategy .
- If the access frequency of your data is not different , There is no difference between hot and cold , Use it directly allkeys-random Strategy , Just randomly select the data to be eliminated .
- If your data has top requirements , Such as top news . Then we choose volatile-lru Strategy , At the same time, no expiration time is set for the top data , thus , Top data will never be deleted , And other data with expiration time , It will be more LRU The algorithm is eliminated .
边栏推荐
- Run faster with go: use golang to serve machine learning
- Au - delà du PARM! La maîtrise de l'Université de Pékin propose diverse pour actualiser complètement le classement du raisonnement du NLP
- PostgreSQL 13 installation
- webRTC SDP mslabel lable
- Stm32+bh1750 photosensitive sensor obtains light intensity
- easyOCR 字符識別
- Anaconda uses China University of science and technology source
- Detailed explanation of usememo, memo, useref and other relevant hooks
- How can I quickly check whether there is an error after FreeSurfer runs Recon all—— Core command tail redirection
- Handwriting promise and async await
猜你喜欢
Photoshop插件-动作相关概念-ActionList-ActionDescriptor-ActionList-动作执行加载调用删除-PS插件开发
Thymeleaf uses background custom tool classes to process text
安装配置Jenkins
MySQL之CRUD
Talk about your understanding of microservices (PHP interview theory question)
Two Bi development, more than 3000 reports? How to do it?
如何将电脑复制的内容粘贴进MobaXterm?如何复制粘贴
Super wow fast row, you are worth learning!
No one consults when doing research and does not communicate with students. UNC assistant professor has a two-year history of teaching struggle
Crud de MySQL
随机推荐
Isn't it right to put money into the external market? How can we ensure safety?
Your childhood happiness was contracted by it
市值蒸发超百亿美元,“全球IoT云平台第一股”赴港求生
Detailed explanation of usememo, memo, useref and other relevant hooks
你童年的快乐,都是被它承包了
Ecotone technology has passed ISO27001 and iso21434 safety management system certification
Can I pass the PMP Exam in 20 days?
IPv6与IPv4的区别 网信办等三部推进IPv6规模部署
DVWA range clearance tutorial
Does maxcompute have SQL that can query the current storage capacity (KB) of the table?
[C question set] of Ⅷ
在Pytorch中使用Tensorboard可视化训练过程
leetcode:881. lifeboat
[12 classic written questions of array and advanced pointer] these questions meet all your illusions about array and pointer, come on!
B站做短视频,学抖音死,学YouTube生?
Change multiple file names with one click
Under the crisis of enterprise development, is digital transformation the future savior of enterprises
Using tensorboard to visualize the training process in pytoch
超越PaLM!北大碩士提出DiVeRSe,全面刷新NLP推理排行榜
可转债打新在哪里操作开户是更安全可靠的呢