当前位置:网站首页>Interviewer: How is the expired key in Redis deleted?
Interviewer: How is the expired key in Redis deleted?
2022-08-04 20:25:00 【51CTO】

Introduction
We can set the expiration time for the key in Redis, then when the key expires, when will it be deleted?
If we write the Redis expiration policy, we will think of the following three options
- Delete regularly, create a timer while setting the expiration time of the key.When the expiration time of the key comes, delete the key immediately
- Lazy delete, every time you get a key, determine whether the key expires, if it expires, delete the key, if not, return the key
- Delete regularly, check the keys every once in a while, and delete the expired keys in it
The timing deletion strategy is not friendly to the CPU. When there are many expired keys, the Redis thread is used to delete the expired keys, which will affect the response of normal requests
Lazy delete read CPU is better, but it will waste a lot of memory.If a key is set to expire in memory, but it is not accessed, it will always exist in memory
Remove policy periodically is more CPU and memory friendly
Key expiration policy in Redis
The following two strategies are selected for the deletion of redis expired keys
- Lazy delete
- Remove periodically
Lazy delete
When the client accesses the key, it checks the expiration time of the key, and deletes it immediately if it expires
Remove periodically
Redis will put keys with expiration time set in a separate dictionary, and traverse this dictionary regularly to delete expired keys. The traversal strategy is as follows
- Expiration scan is performed 10 times per second, and 20 keys are randomly selected from the expired dictionary each time
- Delete expired keys among the 20 keys
- If the ratio of expired keys exceeds 1/4, go to step one
- The upper limit of each scan time does not exceed 25ms by default to avoid thread stuck
Because the expired key in Redis is deleted by the main thread, in order not to block the user's request, the expired key is deleted a small number of times.The source code can refer to the activeExpireCycle method in expire.c
Why should I understand the deletion policy of redis expired keys?
There is only one purpose, let you know Set the expiration time of the key to a random range, not all at the same time, otherwise frequent scanning of expired dictionaries will causeCauses the client's request to be stuck
Reference Blog
"Redis Deep Adventure" old money
边栏推荐
猜你喜欢

零知识证明——zkSNARK证明体系

PriorityQueue类的使用及底层原理

大资本已开始逃离加密领域?

Use "green computing" technology to promote sustainable development of computing power

37.轮播图

面试官:索引为什么会失效?

How to carry out AI business diagnosis and quickly identify growth points for cost reduction and efficiency improvement?

Desthiobiotin衍生物Desthiobiotin-PEG4-Amine/Alkyne/Azide/DBCO

多商户商城系统功能拆解22讲-平台端分销商品

C语言小笔记+题
随机推荐
五分钟入门文本处理三剑客grep awk sed
How to carry out AI business diagnosis and quickly identify growth points for cost reduction and efficiency improvement?
使用百度EasyDL实现森林火灾预警识别
带你了解数据分布式存储原理
Go study notes (Part 1) Configuring the Go development environment
AWS SES 的监控和告警
「 WAIC 2022 · 黑客马拉松」蚂蚁财富两大赛题邀你来战!
Desthiobiotin-PEG4-Azide_脱硫生物素-叠氮化物 100mg
MySQL字段类型
如何推动乡村振兴的落地
【CAS:2306109-91-9 |胺-PEG4-脱硫生物素】价格
长时间序列遥感数据处理及在全球变化、物候提取、植被变绿与固碳分析、生物量估算与趋势分析等领域中的应用
c sqlite...
C语言——青蛙跳台阶(递归)
C#的Dictionary字典集合按照key键进行升序和降序排列
从卖产品到卖“链路”:20条策略 解读直播带货迭代玩法
搭建MyCat2双主双从的MySQL读写分离
零知识证明笔记——私密交易,pederson,区间证明,所有权证明
泰山OFFICE技术讲座:底纹、高亮、边框的关系
刷题-洛谷-P1307 数字反转