当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
五分钟入门文本处理三剑客grep awk sed
Finished product upgrade program
C#移动OA办公系统源码(基于微信企业号)
mysql的存储过程介绍、创建、案例、删除、查看「建议收藏」
蚂蚁集团时序数据库CeresDB正式开源
如何使用 jMeter Parallel Controller - 并行控制器以及一些常犯的错误
Red5搭建直播平台
【TypeScript】深入学习TypeScript枚举
如何找到某个 ABAP structure 某字段的源头来自哪个数据库表
How to manually download and install SAP Fiori tools - Extension Pack for Visual Studio Code
How to promote the implementation of rural revitalization
Uniapp微信雪糕刺客单页小程序源码
简单理解 JS 事件循环
搭建MyCat2双主双从的MySQL读写分离
win10 uwp 使用 ScaleTransform 放大某个元素
How to carry out AI business diagnosis and quickly identify growth points for cost reduction and efficiency improvement?
vscode离线安装插件方法
awk statistical difference record
泰山OFFICE技术讲座:底纹、高亮、边框的关系
ts集成和使用








