当前位置:网站首页>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
边栏推荐
- 零知识证明笔记——私密交易,pederson,区间证明,所有权证明
- 多用户同时远程登录连接到一台服务器
- c语言小项目(三子棋游戏实现)
- C语言——青蛙跳台阶(递归)
- Win10 uwp use ScaleTransform magnify an element
- Use "green computing" technology to promote sustainable development of computing power
- win10 uwp 使用 ScaleTransform 放大某个元素
- 遇到MapStruct后,再也不手写PO,DTO,VO对象之间的转换了
- Desthiobiotin-PEG4-Azide_脱硫生物素-叠氮化物 100mg
- KubeSphere简介,功能介绍,优势,架构说明及应用场景
猜你喜欢
AWS SES 的监控和告警
win10终端中如何切换磁盘
Seata source code analysis: various message processing processes of seata server
ELECTRA: Pre-training Text Encoders as Discriminators Rather Than Generators
C#移动OA办公系统源码(基于微信企业号)
QT(42)-QT线程-线程调用槽函数
【AGC】构建服务1-云函数示例
多用户同时远程登录连接到一台服务器
CAS :80750-24-9(脱硫生物素 NHS 酯)
Desthiobiotin衍生物Desthiobiotin-PEG4-Amine/Alkyne/Azide/DBCO
随机推荐
win10 uwp 修改图片质量压缩图片
实现菜单拖拽排序
EasyUi常用代码
The book "The Essence of Alipay Experience Design", a record of knowledge related to testing
PriorityQueue类的使用及底层原理
从卖产品到卖“链路”:20条策略 解读直播带货迭代玩法
Nuxt.js的优缺点和注意事项
【学术相关】清华教授发文劝退读博:我见过太多博士生精神崩溃、心态失衡、身体垮掉、一事无成!...
Tensorflow2 环境搭建
LED的C语言应用程序
Latex分章节、分段落编译:input{}与include{}的区别
nr part calculation
5 g NR notes
C#弹出询问对话框
node 的运行命令
MySQL stored procedure introduction, creation, case, delete, view "recommended collection"
Win10 uwp use ScaleTransform magnify an element
常用正则表达式[通俗易懂]
vim clear last search highlighting
C语言——青蛙跳台阶(递归)