当前位置:网站首页>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 .
边栏推荐
- Brief introduction of machine learning framework
- useMemo,memo,useRef等相关hooks详解
- The difference between abstract classes and interfaces in PHP (PHP interview theory question)
- Dark horse programmer - software testing -10 stage 2-linux and database -44-57 why learn database, description of database classification relational database, description of Navicat operation data, de
- CPU设计实战-第四章实践任务三用前递技术解决相关引发的冲突
- Mysql---- function
- Your childhood happiness was contracted by it
- go学习 ------jwt的相关知识
- Selection and use of bceloss, crossentropyloss, sigmoid, etc. in pytorch classification
- Can I pass the PMP Exam in 20 days?
猜你喜欢
超越PaLM!北大碩士提出DiVeRSe,全面刷新NLP推理排行榜
Crud of MySQL
CODING DevSecOps 助力金融企业跑出数字加速度
可视化任务编排&拖拉拽 | Scaleph 基于 Apache SeaTunnel的数据集成
Live broadcast preview | how to implement Devops with automatic tools (welfare at the end of the article)
两个BI开发,3000多张报表?如何做的到?
Huawei Hubble incarnation hard technology IPO harvester
How to paste the contents copied by the computer into mobaxterm? How to copy and paste
FR练习题目---简单题
Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology
随机推荐
想进阿里必须啃透的12道MySQL面试题
Ctfshow web entry information collection
[detailed explanation of Huawei machine test] happy weekend
Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology
Stm32+bh1750 photosensitive sensor obtains light intensity
裁员下的上海
maxcompute有没有能查询 表当前存储容量的大小(kb) 的sql?
729. My schedule I: "simulation" & "line segment tree (dynamic open point) &" block + bit operation (bucket Division) "
Using tensorboard to visualize the training process in pytoch
"Sequelae" of the withdrawal of community group purchase from the city
Ten billion massage machine blue ocean, difficult to be a giant
超越PaLM!北大硕士提出DiVeRSe,全面刷新NLP推理排行榜
CODING DevSecOps 助力金融企业跑出数字加速度
Crud de MySQL
Machine learning notes - gray wolf optimization
webRTC SDP mslabel lable
当代人的水焦虑:好水究竟在哪里?
FR练习题目---简单题
如何将电脑复制的内容粘贴进MobaXterm?如何复制粘贴
sql server char nchar varchar和nvarchar的区别