当前位置:网站首页>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 淘汰策略

边栏推荐
- 互斥锁、读写锁、自旋锁,以及原子操作指令xaddl、cmpxchg的使用场景剖析
- jmeter分布式压测
- Getting started with MATLAB 3D drawing command plot3
- WMS仓储管理系统能解决电子行业哪些仓库管理问题
- 卡尔曼滤波器KF
- .NET Static Code Weaving - Rougamo Release 1.1.0
- MATLAB三维绘图命令plot3入门
- 建木DevOps流程的快速运用
- Spinnaker调用Jenkins API 返回403错误
- [Miscellaneous] How to install the specified font into the computer and then use the font in the Office software?
猜你喜欢
![[Miscellaneous] How to install the specified font into the computer and then use the font in the Office software?](/img/15/23b0724f9c9672c61b91320f1b84d8.png)
[Miscellaneous] How to install the specified font into the computer and then use the font in the Office software?

Web3 security risks daunting?How should we respond?

jmeter distributed stress test

Modulo operation (MOD)

The Beijing E-sports Metaverse Forum was successfully held

Nanoprobes 棕榈酰纳米金相关说明书

微服务的简单介绍

Getting started with MATLAB 3D drawing command plot3

Vant3—— 点击对应的name名称跳转到下一页对应的tab栏的name的位置

LeetCode 19:删除链表的倒数第 N 个结点
随机推荐
C语言 函数递归
手撕Gateway源码,今日撕工作流程、负载均衡源码
【性能优化】MySQL性能优化之存储引擎调优
c语言分层理解(c语言操作符)
分布式事务框架 seata
【链路聚合原理及配置】
2015年开源大事件汇总
通过whl安装第三方包
After building the pytorch environment, the pip and conda commands cannot be used
机器学习——库
虚拟机CentOS7中无图形界面安装Oracle
.NET静态代码织入——肉夹馍(Rougamo) 发布1.1.0
Mvc, Mvp and Mvvm
微服务的简单介绍
研究生新生培训第四周:MobileNetV1, V2, V3
Web3 安全风险令人生畏?应该如何应对?
数据库扩容也可以如此丝滑,MySQL千亿级数据生产环境扩容实战
Shell编程之循环语句(for、while)
Read FastDFS in one article
jmeter跨平台运行csv等文件