当前位置:网站首页>Redis expiration key deletion strategy [easy to understand]
Redis expiration key deletion strategy [easy to understand]
2022-07-25 23:06:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
List of articles
Redis There is a function to set time expiration in , That is, to store in redis The value in the database can set an expiration time . As a cached database , It's very practical . As in our general project token Or some login information , Especially SMS verification codes are time limited , According to the traditional way of database processing , Generally, it's self judgment that is overdue , This will undoubtedly have a serious impact on project performance .
We set key When , You can give me one expire time, It's the expiration date , We can specify this by expiration time key Time to live .
notes : For the hash table container , The expiration time can only be set for the entire key ( The whole hash table ), You can't set an expiration time for a single element in a key .
If a key is expired , Is it deleted from the memory immediately after the expiration time ? If not , When will it be deleted after expiration ?
There are actually three different deletion strategies : (1): Delete... Now . When setting the expiration time of the key , Create a callback event , When the expiration time reaches , Key deletion is performed automatically by the time processor . (2): Lazy deletion . The key is out of date , No matter . Each time from dict In the dictionary, press key When the value is taken , Check this first key Has it expired , If it's expired, delete it , And back to nil, If it doesn't expire , Return the key value . (3): Delete regularly . Every once in a while , Yes expires Dictionary check , Delete the expiration key inside . You can see , The second is passive deletion , The first and third are active deletion , And the first one is more real-time . The following is a specific analysis of these three deletion strategies .
Delete... Now
Immediate deletion can ensure the maximum freshness of data in memory , Because it guarantees that the expired key value will be deleted immediately after expiration , The memory occupied by it will also be released . however Delete right now cpu Is the most unfriendly . Because deletion will occupy cpu Time for , If it happens to be cpu When you're busy , For example, when doing intersection or sorting calculations , Will give you cpu Cause additional stress .
And now redis How the event handler handles time events – Disordered list , Find a key The time complexity of is O(n), So it is not suitable for dealing with a large number of time events .
Lazy deletion
Inert deletion means , After a key value expires , This key value will not be deleted immediately , But wait until the next time it is used , Will be checked to expire , Only then can it be deleted . So the disadvantage of lazy deletion is obvious : Waste of memory .dict Dictionary and expires Dictionaries should store the information of this key value .
for instance , For some data updated by time point , such as log journal , After expiration, you may not get access for a long time , In this way, during this period of time, we will waste so much memory to save log. This is very memory size dependent for performance redis Come on , It's more lethal .
Delete regularly
From the above analysis , Deleting immediately will take up a lot of... In a short time cpu, Lazy deletion will waste memory for a period of time , So scheduled deletion is a compromise . Scheduled deletion is : Delete every other period of time , And by limiting the execution time and frequency of deletion operations , To reduce the number of delete operations cpu Influence . On the other hand, scheduled deletion also effectively reduces the memory waste caused by lazy deletion .
Redis The strategy used
redis The expired key deletion policy used is : Lazy deletion plus regular deletion , Combination of the two .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/127895.html Link to the original text :https://javaforall.cn
边栏推荐
- Why is Google's internal tools not suitable for you?
- Hj9 extract non duplicate integers
- Code shoe set precision barrage
- 自定义mvc原理
- 驱动板网线直连电脑共享网络配置
- Vs2017 compilation encountered the error HResult e returned by the call of COM component_ FAIL
- Express framework
- Opencv compile and call GPU version
- 第二周学习:卷积神经网络
- Network Security Learning (XV) ARP
猜你喜欢

Day 3 experiment

Mysql数据类型

Network security and level protection

Network Security Learning (XII) OSI and TCP

Take root downward, grow upward, and explore the "root" power of Huawei cloud AI

Details of notification (status bar notification)

Enabling partners, how can Amazon cloud technology "get on the horse and get a ride"?
![[paper notes] robot dynamic tracking and grasping method based on online prediction and planning](/img/0f/ca7f3cd3332069185a2dad397766b4.png)
[paper notes] robot dynamic tracking and grasping method based on online prediction and planning

VisualBox启动虚拟机报错:The VM session was closed before any attempt to power it on.

How to obtain the cash flow data of advertising services to help analyze the advertising effect?
随机推荐
Take root downward, grow upward, and explore the "root" power of Huawei cloud AI
QT operation to solve large amount of duplicate data
Understanding of forward proxy and reverse proxy
每周推荐短视频:需要协同的智能设备越来越多,给物联网开发提出更大挑战?
【接口性能优化】索引失效的原因以及如何进行SQL优化
Madness. MySQL learning.
Network Security Learning (XII) OSI and TCP
Explain in detail the addition (+) operation in JS, basic data type addition, reference data type addition, and the underlying operation rules, [] + {}, {} + []
Learning notes of technical art hundred people plan (2) -- vector
Websocket summary
Solve the problem phpstudy failed to import the database
IPFs of Internet Protocol
Shanghai Second Polytechnic University - Health Daily autocheck
Experience of machine learning with Google Lab
【论文笔记】基于在线预测和规划的机器人动态跟踪抓取方法
理解的英文(言语理解)
TFrecord写入与读取
Recommend short videos every week: more and more smart devices need collaboration, posing a greater challenge to the development of the Internet of things?
校验码生成
SSH服务器CBC加密模式漏洞(CVE-2008-5161)