当前位置:网站首页>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
边栏推荐
- MySQL field type
- Differences in the working mechanism between SAP E-commerce Cloud Accelerator and Spartacus UI
- Initialization process of SAP UI5
- win10终端中如何切换磁盘
- 【有奖征文】秋招特训,打造你的专属产品体验
- Latex分章节、分段落编译:input{}与include{}的区别
- Go study notes (Part 1) Configuring the Go development environment
- win10 uwp 修改图片质量压缩图片
- 【CAS:2306109-91-9 |胺-PEG4-脱硫生物素】价格
- IIC驱动OLED
猜你喜欢
随机推荐
Seata source code analysis: various message processing processes of seata server
really time ntp service start command
Client Side Cache 和 Server Side Cache 的区别
ELECTRA: Pre-training Text Encoders as Discriminators Rather Than Generators
AWS SES 的监控和告警
awk statistical difference record
密码学系列之:PEM和PKCS7,PKCS8,PKCS12
如何使用 jMeter Parallel Controller - 并行控制器以及一些常犯的错误
在vs code中进行本地调试和开启本地服务器
简单理解 JS 事件循环
zynq records
JS new一个构造器发生了什么?从零手写一个new方法
【SQL】触发器同步表数据
动态规划_双数组字符串
win10 uwp modify picture quality compress picture
面试官:索引为什么会失效?
小软件大作用 | 如何省时省力进行Gerber图层快速对比?
ts集成和使用
如何手动下载并安装 Visual Studio Code 的 SAP Fiori tools - Extension Pack
如何推动乡村振兴的落地









