当前位置:网站首页>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 .
边栏推荐
猜你喜欢

【数组和进阶指针经典笔试题12道】这些题,满足你对数组和指针的所有幻想,come on !

安装配置Jenkins

"Sequelae" of the withdrawal of community group purchase from the city

Fr exercise topic - simple question

Machine learning notes - gray wolf optimization

sql server学习笔记

两个BI开发,3000多张报表?如何做的到?

There is a powerful and good-looking language bird editor, which is better than typora and developed by Alibaba

超级哇塞的快排,你值得学会!

729. My schedule I: "simulation" & "line segment tree (dynamic open point) &" block + bit operation (bucket Division) "
随机推荐
Isn't it right to put money into the external market? How can we ensure safety?
长列表优化虚拟滚动
Thymeleaf uses background custom tool classes to process text
Under the crisis of enterprise development, is digital transformation the future savior of enterprises
可转债打新在哪里操作开户是更安全可靠的呢
黑马程序员-软件测试-10阶段2-linux和数据库-44-57为什么学习数据库,数据库分类关系型数据库的说明Navicat操作数据的说明,Navicat操作数据库连接说明,Navicat的基本使用,
Cartoon: programmers don't repair computers!
【招聘岗位】软件工程师(全栈)- 公共安全方向
Stm32+bh1750 photosensitive sensor obtains light intensity
Mongdb learning notes
"Sequelae" of the withdrawal of community group purchase from the city
JS bright blind your eyes date selector
Differences between IPv6 and IPv4 three departments including the office of network information technology promote IPv6 scale deployment
MySQL----函数
sql server学习笔记
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面试题
PostgreSQL 13 installation
面试突击62:group by 有哪些注意事项?
R 熵权法计算权重及综合得分