当前位置:网站首页>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 淘汰策略
边栏推荐
- typescript50 - type specification between cross types and interfaces
- Google Earth Engine ——利用公开的河流数据计算河流的有效宽度
- Vant3 - click on the corresponding name name to jump to the next page corresponding to the location of the name of the TAB bar
- ENS域名注册量创历史新高 逆市增长之势?光环之下存在炒作风险
- VR全景拍摄线上展馆,3D全景带你沉浸体验
- 现货白银需要注意八大事项
- 带你造轮子,自定义一个随意拖拽可吸边的悬浮View组件
- What warehouse management problems can WMS warehouse management system solve in the electronics industry?
- Nanoprobes 棕榈酰纳米金相关说明书
- "Miscellaneous" barcode by Excel as a string
猜你喜欢
虚拟机CentOS7中无图形界面安装Oracle
Justin Sun was invited to attend the 36氪 Yuan Universe Summit and delivered a keynote speech
typescript48-函数之间的类型兼容性
The problem of disorganized data output by mnn model
js函数防抖和函数节流及其使用场景
After building the pytorch environment, the pip and conda commands cannot be used
Talking about the future development direction of my country's industrial parks
pcl点云数据 转化为 Eigen::Map
现货白银需要注意八大事项
Using matlab to solve the linear optimization problem based on matlab dynamic model of learning notes _11 】 【
随机推荐
搭建好pytorch环境后,pip和conda指令不能用
共享新能源充电桩充电站建设需要些什么流程及资料?
无代码7月热讯 | 微软首推数字联络中心平台;全球黑客马拉松...
C语言 函数递归
ML18-自然语言处理
MPLS Comprehensive Experiment
电子组装行业对MES管理系统的需求分析
iframe通信
typescript53 - generic constraints
boot issue
带你造轮子,自定义一个随意拖拽可吸边的悬浮View组件
高斯推断推导
【详细教程】一文参透MongoDB聚合查询
伦敦银最新均线分析系统怎么操作?
【每日一题】899. 有序队列
手撕Gateway源码,今日撕工作流程、负载均衡源码
分布式事务框架 seata
XSS-绕过for循环过滤
corn表达式 具体详解与案例
RSS订阅微信公众号初探-feed43