当前位置:网站首页>redis探索之缓存击穿、缓存雪崩、缓存穿透
redis探索之缓存击穿、缓存雪崩、缓存穿透
2022-07-01 12:27:00 【青铜大神】
在如今的互联网环境中,似乎每个系统都会用到缓存,大数据横行,大量数据因为着传统的mysql硬盘查询会变成系统瓶颈。所以我们引入缓存的目的就是提高系统响应速度,提高吞吐量。将热门数据放入缓存降低查库次数。既然缓存举足轻重,那么应用缓存时,遇到的问题和解决办法,就变成了程序员必备、面试必会。
系统中使用的基本缓存逻辑

第一板斧:缓存穿透
缓存穿透是指查询一个压根不存在的数据。因为缓存中并不存在,所以请求会查询数据库,由于不存在数据,也不会存入缓存。

解决办法
1.缓存空值:这个很简单,也很容易想到,有问题的数据我也给你存在缓存中,告诉缓存,它是有问题的,别往后边传了。避免请求穿透缓存发到数据库。
2.采用布隆过滤器:使用bloom过滤器,将数据库数据,映射在布隆过滤器。作为第一道安全门,过滤无效请求。
第二板斧:缓存击穿
缓存击穿是指一个Key热点缓存数据在某个时间点过期后,恰好这个时间有大量请求查询这个Key,这些请求就都被发送到数据库。

解决办法
提前将热点数据放置到缓存,并且设置永不过期或者使用定时任务刷新缓存数据与过期时间。
使用分布式锁,保障在缓存失效时不会大量并发都发送到数据库。
缓存穿透与缓存击穿的区别
缓存穿透请求的是缓存中没有,数据库中也没有的数据,加上大量的并发,导致的系统瘫痪。
缓存击穿请求的是缓存中没有,但是数据库中有的数据。
第三板斧:缓存雪崩
缓存雪崩是指缓存中的key在同一时间大面积失效,导致大量请求发送到数据库。是缓存击穿的特殊情况。
常见的原因是:1.redis服务器挂了;2.大量缓存数据设置了相同的过期时间,在某个时间点暴雷。

解决办法
将过期时间分散开,设置为固定时间+随机数,减少一起过期的可能性。
使用锁或消息队列,限制并发量。
设置多级缓存,像CPU三级缓存那样,层层筛选。
边栏推荐
- ASP.NET Core 6 从入门到企业级实战开发应用技术汇总
- 队列操作---
- Like the three foot platform
- Fatal error: execution: there is no such file or directory
- [Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 3
- NOV Schedule for . Net to display and organize appointments and recurring events
- 【20211129】Jupyter Notebook遠程服務器配置
- Onenet Internet of things platform - create mqtts products and devices
- AI抠图工具
- MySQL workbench data modeling function
猜你喜欢
![[datawhale202206] pytorch recommendation system: precision model deepfm & DIN](/img/4f/8799016731a2c1647b6f2f4d96b754.png)
[datawhale202206] pytorch recommendation system: precision model deepfm & DIN
![[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 5](/img/f5/9c68b3dc30362d3776c262fdc13fd0.jpg)
[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 5

迅为i.MX8Mmini开发板离线构建Yocto系统

消息队列之监控退款任务批处理过程

Chained storage of queues

Machine learning - Data Science Library - day two

The operation process of using sugar to make a large data visualization screen

Sleep quality today 79 points

91. (cesium chapter) cesium rocket launch simulation

Onenet Internet of things platform - mqtt product devices send messages to message queues MQ
随机推荐
【MAUI】为 Label、Image 等控件添加点击事件
[JS advanced] promise explanation
比特熊直播间一周年,英雄集结令!邀你来合影!
Arm GIC (V) how arm TrustZone supports security interrupt analysis notes.
栈-------
[datawhale202206] pytorch recommendation system: multi task learning esmm & MMOE
CPI教程-异步接口创建及使用
巩固-C#运算符
usb peripheral 驱动 - cable connect/disconnect
【datawhale202206】pyTorch推荐系统:召回模型 DSSM&YoutubeDNN
JS related interview questions and answers (1)
I wish you all a happy reunion
The operation process of using sugar to make a large data visualization screen
Use of easyexcel
JPA and criteria API - select only specific columns - JPA & criteria API - select only specific columns
[datawhale202206] pytorch recommendation system: precision model deepfm & DIN
队列的链式存储
[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 5
硬阈值(Hard Thresholding)函数解读[通俗易懂]
【20211129】Jupyter Notebook遠程服務器配置