当前位置:网站首页>[Redis] Resid的删除策略
[Redis] Resid的删除策略
2022-08-05 05:13:00 【来一杯奶喵】
目录
Redis中的数据特征
Redis是一种内存级数据库,所有数据均存放在内存中,内存中的数据可以通过TTL指令获取其状态
XX :具有时效性的数据
-1 :永久有效的数据
-2 :已经过期的数据或被删除的数据或未定义的数据
数据删除策略
- 定时删除
- 惰性删除
- 定期删除
2.1定时删除
创建一个定时器,当key设置有过期时间,且过期时间到达时,由定时器任务立即执行对键的删除操作
- 优点:节约内存,到时就删除,快速释放掉不必要的内存占用
- 缺点:CPU压力很大,无论CPU此时负载量多高,均占用CPU,会影响redis服务器响应时间和指令吞吐量
- 总结:用处理器性能换取存储空间(拿时间换空间)
2.2 惰性删除
数据到达过期时间,不做处理。等下次访问该数据时
- 如果未过期,返回数据
- 发现已过期,删除,返回不存在
- 优点:节约CPU性能,发现必须删除的时候才删除
- 缺点:内存压力很大,出现长期占用内存的数据
- 总结:用存储空间换取处理器性能(拿空间换时间)
2.3 定期删除
周期性轮询redis库中的时效性数据,采用随机抽取的策略,利用过期数据占比的方式控制删除频度
●Redis启动服务器初始化时,读取配置server.hz的值,默认为10
●每秒钟执行server.hz次serverCron() 调用一-次, 轮询一次所有的库
databasesCron(调用16次activateExpireCycle,检查所有的库
activeExpireCycle()检查1个库的expires
●activeExpireCycle(对每 个expires[*]逐-进行检测,每次执行250ms/server.hz
●对某个expires[*]检测时 ,随机挑选W个key检测
●如果key超时,删除key
●如果一轮中删除的key的数量>W* 25% ,循环该过程
●如果一轮中删除的key的数量≤W*25% ,检查下一个expires[*] , 0-15循环
●W取值=ACTIVE_ EXPIRE_ _CYCLE_ LOOKUPS_ PER_ LOOP属性值
●参数current _db用于记录activeExpireCycle()进入哪个expires[*]执行
●如果activeExpireCycle()执行时间到期,下次从current_ db继续向下执行
总结上述过程:
每秒钟执行server.hz次serverCron,serverCron会轮询所有的库,使用databasesCron方法对每个库进行检测,databasesCron会调用activeExpireCycle会对每个expire[]检测,一个expire[]检测250ms/server.hz
周期性抽查存储空间(查询某个库的expires时,如果这轮删除过多,则再抽取删除一轮,如果这轮删除的很少,则去检查下一个库的expires)
- 优点1:CPU性能占用设置有峰值,检测频度可自定义设置
- 优点2:内存压力不是很大,长期占用内存的冷数据会被持续清理
- 总结:周期性抽查存储空间 (随机抽查,重点抽查)
删除策略比对
| 定时删除 | 节约内存,无占用 | 不分时段占用CPU资源,频度高 | 拿时间换空间 |
| 惰性删除 | 内存占用严重 | 延时执行,CPU利用率高 | 拿空间换时间 |
| 定期删除 | 内存定期随机清理 | 每秒花费固定的CPU资源维护内存 | 随机抽查,重点抽查 |
边栏推荐
- 学习总结week2_3
- "PHP8 Beginner's Guide" A brief introduction to PHP
- "Recursion" recursion concept and typical examples
- 【过一下6】机器视觉视频 【过一下2被挤掉了】
- Do you use tomatoes to supervise your peers?Add my study room, come on together
- SQL(一) —— 增删改查
- [Go through 7] Notes from the first section of the fully connected neural network video
- jvm three heap and stack
- 【Transfer】What is etcd
- 【Untitled】
猜你喜欢
![[Student Graduation Project] Design and Implementation of the Website Based on the Web Student Information Management System (13 pages)](/img/86/9c9a2541f2b7089ae47e9832fffdb3.png)
[Student Graduation Project] Design and Implementation of the Website Based on the Web Student Information Management System (13 pages)
![coppercam primer [6]](/img/d3/a7d44aa19acfb18c5a8cacdc8176e9.png)
coppercam primer [6]

【Transfer】What is etcd

Basic properties of binary tree + oj problem analysis

数据库 单表查询

day10-字符串作业

多线程查询结果,添加List集合

解决端口占用问题

第三讲 Gradient Tutorial梯度下降与随机梯度下降

pycharm中调用Matlab配置:No module named ‘matlab.engine‘; ‘matlab‘ is not a package
随机推荐
Lecture 4 Backpropagation Essays
第二讲 Linear Model 线性模型
[Go through 3] Convolution & Image Noise & Edge & Texture
day6-列表作业
2022 The 4th C.Easy Counting Problem (EGF+NTT)
Difference between for..in and for..of
【解码工具】Bitcoin的一些在线工具
Redux
Analysis of Mvi Architecture
number_gets the specified number of decimals
【练一下1】糖尿病遗传风险检测挑战赛 【讯飞开放平台】
redis cache clearing strategy
The mall background management system based on Web design and implementation
2023 International Conference on Information and Communication Engineering (JCICE 2023)
UVA10827
[Decoding tools] Some online tools for Bitcoin
Lecture 2 Linear Model Linear Model
redis 缓存清除策略
day9-字符串作业
CAP+BASE