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

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

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

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

解决办法
将过期时间分散开,设置为固定时间+随机数,减少一起过期的可能性。
使用锁或消息队列,限制并发量。
设置多级缓存,像CPU三级缓存那样,层层筛选。
边栏推荐
- 腾讯安全发布《BOT管理白皮书》|解读BOT攻击,探索防护之道
- Ansible相关内容梳理
- Onenet Internet of things platform - mqtts product equipment connected to the platform
- MySQL workbench data modeling function
- ANSI/UL 94 VTM薄质材料垂直燃烧测试
- JS related interview questions and answers (1)
- 强大、好用、适合程序员/软件开发者的专业编辑器/笔记软件综合评测和全面推荐
- 腾讯安全联合毕马威发布监管科技白皮书,解析“3+3”热点应用场景
- Leetcode force buckle (Sword finger offer 31-35) 31 Stack push pop-up sequence 32i II. 3. Print binary tree from top to bottom 33 Post order traversal sequence 34 of binary search tree The path with a
- MySQL data table creation
猜你喜欢

循环链表--

Sort out relevant contents of ansible

Ansible相关内容梳理

Switch basic experiment
![[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 5](/img/f5/9c68b3dc30362d3776c262fdc13fd0.jpg)
[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 5
![[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 6](/img/0e/0900e386f3baeaa506cc2c1696e22a.jpg)
[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 6
![[some notes]](/img/91/7657f90b50f012736579b1585b4ade.jpg)
[some notes]

【20220605】文献翻译——虚拟现实中的可视化:一个系统的回顾
![[20211129] configuration du serveur distant du carnet de notes jupyter](/img/7c/79c9fcb91bde75e954dc3ecf9f5afd.png)
[20211129] configuration du serveur distant du carnet de notes jupyter
![[datawhale202206] pytorch recommendation system: precision model deepfm & DIN](/img/4f/8799016731a2c1647b6f2f4d96b754.png)
[datawhale202206] pytorch recommendation system: precision model deepfm & DIN
随机推荐
栈的应用——括号匹配问题
[JS advanced] promise explanation
手把手教你完成图像分类实战——基于卷积神经网络的图像识别
Machine learning - Data Science Library Day 3 - Notes
AI抠图工具
Like the three foot platform
顺序表有关操作
【MAUI】为 Label、Image 等控件添加点击事件
Leetcode (Sword finger offer) - 58 - ii Rotate string left
C # dependency injection (straight to the point) will be explained as soon as you see the series
91.(cesium篇)cesium火箭發射模擬
BIM and safety in road maintenance-buildSmart Spain
Ansible Playbook
迅为i.MX8Mmini开发板离线构建Yocto系统
MySQL的零拷贝技术
Switch basic experiment
[20211129] configuration du serveur distant du carnet de notes jupyter
腾讯安全联合毕马威发布监管科技白皮书,解析“3+3”热点应用场景
Talk about biological live broadcast - genovis Zhang Hongyan antibody specific enzyme digestion technology helps to characterize the structure of antibody drugs
Sleep quality today 79 points