当前位置:网站首页>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 .
边栏推荐
- mapper.xml文件中的注释
- JS bright blind your eyes date selector
- DVWA range clearance tutorial
- CPU设计实战-第四章实践任务二用阻塞技术解决相关引发的冲突
- Brief introduction of machine learning framework
- Anaconda uses China University of science and technology source
- Crud de MySQL
- 30岁汇源,要换新主人了
- Detailed explanation of usememo, memo, useref and other relevant hooks
- Photoshop plug-in - action related concepts - actions in non loaded execution action files - PS plug-in development
猜你喜欢
【数组和进阶指针经典笔试题12道】这些题,满足你对数组和指针的所有幻想,come on !
Photoshop plug-in action related concepts actionlist actiondescriptor actionlist action execution load call delete PS plug-in development
PyTorch二分类时BCELoss,CrossEntropyLoss,Sigmoid等的选择和使用
Crud of MySQL
Two Bi development, more than 3000 reports? How to do it?
729. My schedule I: "simulation" & "line segment tree (dynamic open point) &" block + bit operation (bucket Division) "
百亿按摩仪蓝海,难出巨头
可视化任务编排&拖拉拽 | Scaleph 基于 Apache SeaTunnel的数据集成
"Sequelae" of the withdrawal of community group purchase from the city
Ctfshow web entry explosion
随机推荐
[C question set] of Ⅷ
STM32+BH1750光敏传感器获取光照强度
useMemo,memo,useRef等相关hooks详解
NBA赛事直播超清画质背后:阿里云视频云「窄带高清2.0」技术深度解读
当代人的水焦虑:好水究竟在哪里?
Microframe technology won the "cloud tripod Award" at the global Cloud Computing Conference!
B站做短视频,学抖音死,学YouTube生?
Photoshop plug-in - action related concepts - actions in non loaded execution action files - PS plug-in development
Type declaration of all DOM elements in TS
How to paste the contents copied by the computer into mobaxterm? How to copy and paste
Drive brushless DC motor based on Ti drv10970
FR练习题目---简单题
Reconnaissance des caractères easycr
超越PaLM!北大碩士提出DiVeRSe,全面刷新NLP推理排行榜
Your childhood happiness was contracted by it
PostgreSQL 13 installation
Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology
Super wow fast row, you are worth learning!
Thymeleaf uses background custom tool classes to process text
Live broadcast preview | how to implement Devops with automatic tools (welfare at the end of the article)