当前位置:网站首页>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 .
边栏推荐
- 超级哇塞的快排,你值得学会!
- 729. 我的日程安排表 I :「模拟」&「线段树(动态开点)」&「分块 + 位运算(分桶)」
- 想进阿里必须啃透的12道MySQL面试题
- Creation and use of thymeleaf template
- 我想咨询一下,mysql一个事务对于多张表的更新,怎么保证数据一致性的?
- Leetcode: Shortest Word Distance II
- Select sort and bubble sort
- Garbage collection mechanism of PHP (theoretical questions of PHP interview)
- Talk about your understanding of microservices (PHP interview theory question)
- Differences between IPv6 and IPv4 three departments including the office of network information technology promote IPv6 scale deployment
猜你喜欢

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

你童年的快乐,都是被它承包了

Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology

Creation and use of thymeleaf template

做研究无人咨询、与学生不交心,UNC助理教授两年教职挣扎史

qt creater断点调试程序详解

Interview shock 62: what are the precautions for group by?

FR练习题目---综合题

爱可可AI前沿推介(7.5)

可视化任务编排&拖拉拽 | Scaleph 基于 Apache SeaTunnel的数据集成
随机推荐
Handwriting promise and async await
Shanghai under layoffs
MongDB学习笔记
Huiyuan, 30, is going to have a new owner
MySQL----函数
Install and configure Jenkins
想进阿里必须啃透的12道MySQL面试题
Run faster with go: use golang to serve machine learning
【華為機試真題詳解】歡樂的周末
Thymeleaf uses background custom tool classes to process text
B站做短视频,学抖音死,学YouTube生?
JS bright blind your eyes date selector
PostgreSQL 13 installation
Crud of MySQL
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
一键更改多个文件名字
Ecotone technology has passed ISO27001 and iso21434 safety management system certification
30岁汇源,要换新主人了
【数组和进阶指针经典笔试题12道】这些题,满足你对数组和指针的所有幻想,come on !
How can I quickly check whether there is an error after FreeSurfer runs Recon all—— Core command tail redirection