当前位置:网站首页>redis中常见的问题(缓存穿透,缓存雪崩,缓存击穿,redis淘汰策略)
redis中常见的问题(缓存穿透,缓存雪崩,缓存击穿,redis淘汰策略)
2022-08-04 00:38:00 【Have_MonkeyG】
一、缓存穿透
1.什么是缓存穿透?
数据库中没有该记录,缓存中也没有该记录,这时由人恶意大量访问这样的数据。这样就会导致该请求绕过缓存,直接访问数据,从而造成数据库压力过大。
2.解决方案:
[1]在controller加数据校验。
[2]我们可以在redis中存入一个空对象,而且要设置过期时间不能太长。超过5分钟
[3]我们使用布隆过滤器。底层:有一个bitmap数组,里面存储了该表的所有id.
需要重点说的是布隆过滤器
布隆过滤器:
//伪代码
String get(String key) { //布隆过滤器钟存储的是数据库表钟对应的id
String value = redis.get(key); //先从缓存获取。
if (value == null) { //缓存没有命中
if(!bloomfilter.mightContain(key)){//查看布隆过滤器钟是否存在
return null;
}else{
value = db.get(key); //查询数据库
redis.set(key, value);
}
}
return value;
}
二、缓存雪崩
1.什么是缓存雪崩?
缓存雪崩是指缓存中数据大批量到过期时间,而查询数据量巨大,引起数据库压力过大甚至down机。和缓存击穿不同的是, 缓存击穿指并发查同一条数据,缓存雪崩是不同数据都过期了,很多数据都查不到从而查数据库。
2.为什么会发生雪崩?
[1]项目刚上线,缓存中没有任何数据
[2]缓存出现大量过期。
[3]redis宕机
3.解决方案:
1.上线前预先把一些热点数据放入缓存。
2.设置过期时间为散列值
3.搭建redis集群
三、缓存击穿
1.什么是缓存击穿?
缓存击穿是指缓存中没有但数据库中有的数据(一般是缓存时间到期),这时由于并发用户特别多,同时读缓存没读到数据,又同时去数据库去取数据,引起数据库压力瞬间增大,造成过大压力。
2.解决方案:
1.设置永久不过期。【这种只适合内存】
2.使用互斥锁(mutex key)业界比较常用的做法。
四、Redis 淘汰策略
边栏推荐
猜你喜欢
数据库扩容也可以如此丝滑,MySQL千亿级数据生产环境扩容实战
Nanoprobes 棕榈酰纳米金相关说明书
typescript53-泛型约束
typescript56-泛型接口
【性能优化】MySQL常用慢查询分析工具
取模运算(MOD)
[Miscellaneous] How to install the specified font into the computer and then use the font in the Office software?
MATLAB三维绘图命令plot3入门
Vant3 - click on the corresponding name name to jump to the next page corresponding to the location of the name of the TAB bar
The 600MHz band is here, will it be the new golden band?
随机推荐
搭建好pytorch环境后,pip和conda指令不能用
The problem of disorganized data output by mnn model
七夕活动浪漫上线,别让网络拖慢和小姐姐的开黑时间
typescript56 - generic interface
哎,又跟HR在小群吵了一架!
MATLAB三维绘图命令plot3入门
机器学习——库
jmeter distributed stress test
Web3 security risks daunting?How should we respond?
Nanoprobes丨Nanogold-抗体和链霉亲和素偶联物
高斯推断推导
typescript51-泛型的基本使用
Electronics manufacturing enterprise deployment WMS what are the benefits of warehouse management system
Talking about the future development direction of my country's industrial parks
2022年8月份DAMA-CDGA/CDGP数据治理认证招生简章
教你如何定位不合理的SQL?并优化之
After building the pytorch environment, the pip and conda commands cannot be used
LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之三:两次优化
BioVendor人Clara细胞蛋白(CC16)Elisa试剂盒检测步骤
如何通过单步调试的方式找到引起 Fiori Launchpad 路由错误的原因试读版