当前位置:网站首页>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
- 明明加了唯一索引,为什么还是产生了重复数据?
- PriorityQueue类的使用及底层原理
- 如何找到某个 ABAP structure 某字段的源头来自哪个数据库表
- Differences in the working mechanism between SAP E-commerce Cloud Accelerator and Spartacus UI
- To -.-- -..- -
- 手撕SparkSQL五大JOIN的底层机制
- vs Code 运行一个本地WEB服务器
- vscode离线安装插件方法
- The difference between Client Side Cache and Server Side Cache
猜你喜欢
随机推荐
Unreal 本地化 国家化 多语言
力扣题(5)—— 最长回文子串
Desthiobiotin-PEG4-Azide_脱硫生物素-叠氮化物 100mg
DICOM医学影像协议
[TypeScript] In-depth study of TypeScript enumeration
Go study notes (Part 1) Configuring the Go development environment
WIN10系统如何开启终端
win10 uwp modify picture quality compress picture
Oreo域名授权验证系统v1.0.6公益开源版本网站源码
Chrome安装zotero connector 插件
简单理解 JS 事件循环
win10 uwp 修改图片质量压缩图片
刷题-洛谷-P1200 你的飞碟在这儿Your Ride Is Here
Ant Group's time series database CeresDB is officially open source
【有奖征文】秋招特训,打造你的专属产品体验
ts集成和使用
【学术相关】清华教授发文劝退读博:我见过太多博士生精神崩溃、心态失衡、身体垮掉、一事无成!...
QT(42)-QT线程-线程调用槽函数
面试官:JVM运行时数据区包含哪几部分?作用是啥?
在vs code中进行本地调试和开启本地服务器