当前位置:网站首页>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
边栏推荐
- JS new一个构造器发生了什么?从零手写一个new方法
- SAP UI5 ensures that the control id is globally unique implementation method
- SAP UI5 确保控件 id 全局唯一的实现方法
- 动态数组底层是如何实现的
- vs Code runs a local web server
- [TypeScript] In-depth study of TypeScript enumeration
- mysql的存储过程介绍、创建、案例、删除、查看「建议收藏」
- Use "green computing" technology to promote sustainable development of computing power
- 使用 Chrome 开发者工具的 lighthouse 功能分析 web 应用的性能问题
- awk statistical average max min
猜你喜欢
随机推荐
How to use the Chrome DevTools performance tab
新式茶饮,卷完水果还能卷什么?
[Awards for Essays] Autumn recruitment special training to create your exclusive product experience
刷题-洛谷-P1304 哥德巴赫猜想
Use "green computing" technology to promote sustainable development of computing power
win10 uwp 使用 ScaleTransform 放大某个元素
MYSQL gets the table name and table comment of the database
C语言基础[通俗易懂]
Ant Group's time series database CeresDB is officially open source
Desthiobiotin-PEG4-Azide_脱硫生物素-叠氮化物 100mg
带你了解数据分布式存储原理
c sqlite...
MySQL字段类型
Tensorflow2 环境搭建
KubeSphere简介,功能介绍,优势,架构说明及应用场景
面试官:Redis中过期的key是怎么被删除的?
How to carry out AI business diagnosis and quickly identify growth points for cost reduction and efficiency improvement?
"WAIC 2022 · hackers marathon" two ants wealth competition invited you to fight!
idea源码无法下载
Apache服务器的配置[通俗易懂]









