当前位置:网站首页>Redis雪崩、穿透和击穿是什么?
Redis雪崩、穿透和击穿是什么?
2022-06-25 11:45:00 【帅杰IT大神】
Redis该怎么学?什么是雪崩、穿透和击穿?Redis 崩溃之后会怎么样?系统该如何应对这种情况?如何处理主从复制,哨兵模式怎么搞???...
是不是有点晕?别急,先来了解一下核心概念!
面试官心理分析
其实这是问到缓存必问的,因为缓存雪崩和穿透,是缓存最大的两个问题,要么不出现,一旦出现就是致命性的问题,所以面试官一定会问你。
面试题剖析
缓存雪崩
对于系统 A,假设每天高峰期每秒 5000 个请求,本来缓存在高峰期可以扛住每秒 4000 个请求,但是缓存机器意外发生了全盘宕机。缓存挂了,此时 1 秒 5000 个请求全部落数据库,数据库必然扛不住,它会报一下警,然后就挂了。此时,如果没有采用什么特别的方案来处理这个故障,DBA 很着急,重启数据库,但是数据库立马又被新的流量给打死了。
这就是缓存雪崩。

redis-caching-avalanche
大约在 3 年前,国内比较知名的一个互联网公司,曾因为缓存事故,导致雪崩,后台系统全部崩溃,事故从当天下午持续到晚上凌晨 3~4 点,公司损失了几千万。
缓存雪崩的事前事中事后的解决方案如下:
事前:redis 高可用,主从+哨兵,redis cluster,避免全盘崩溃。
事中:本地 ehcache 缓存 + hystrix 限流&降级,避免 MySQL 被打死。
事后:redis 持久化,一旦重启,自动从磁盘上加载数据,快速恢复缓存数据。

redis-caching-avalanche-solution
用户发送一个请求,系统 A 收到请求后,先查本地 ehcache 缓存,如果没查到再查 redis。如果 ehcache 和 redis 都没有,再查数据库,将数据库中的结果,写入 ehcache 和 redis 中。
限流组件,可以设置每秒的请求,有多少能通过组件,剩余的未通过的请求,怎么办?走降级!可以返回一些默认的值,或者友情提示,或者空白的值。
好处:
数据库绝对不会死,限流组件确保了每秒只有多少个请求能通过。
只要数据库不死,就是说,对用户来说,2/5 的请求都是可以被处理的。
只要有 2/5 的请求可以被处理,就意味着你的系统没死,对用户来说,可能就是点击几次刷不出来页面,但是多点几次,就可以刷出来一次。
缓存穿透
对于系统A,假设一秒 5000 个请求,结果其中 4000 个请求是黑客发出的恶意攻击。
黑客发出的那 4000 个攻击,缓存中查不到,每次你去数据库里查,也查不到。
举个栗子。数据库 id 是从 1 开始的,结果黑客发过来的请求 id 全部都是负数。这样的话,缓存中不会有,请求每次都“视缓存于无物”,直接查询数据库。这种恶意攻击场景的缓存穿透就会直接把数据库给打死。

redis-caching-penetration
解决方式很简单,每次系统 A 从数据库中只要没查到,就写一个空值到缓存里去,比如 set -999 UNKNOWN。然后设置一个过期时间,这样的话,下次有相同的 key 来访问的时候,在缓存失效之前,都可以直接从缓存中取数据。
缓存击穿
缓存击穿,就是说某个 key 非常热点,访问非常频繁,处于集中式高并发访问的情况,当这个 key 在失效的瞬间,大量的请求就击穿了缓存,直接请求数据库,就像是在一道屏障上凿开了一个洞。
不同场景下的解决方式可如下:
若缓存的数据是基本不会发生更新的,则可尝试将该热点数据设置为永不过期。
若缓存的数据更新不频繁,且缓存刷新的整个流程耗时较少的情况下,则可以采用基于 redis、zookeeper 等分布式中间件的分布式互斥锁,或者本地互斥锁以保证仅少量的请求能请求数据库并重新构建缓存,其余线程则在锁释放后能访问到新缓存。
若缓存的数据更新频繁或者缓存刷新的流程耗时较长的情况下,可以利用定时线程在缓存过期前主动的重新构建缓存或者延后缓存的过期时间,以保证所有的请求能一直访问到对应的缓存。
边栏推荐
- JS judge whether a number is in the set
- Manually rollback abnormal data
- Ladder side tuning: the "wall ladder" of the pre training model
- 兴业证券是国企吗?在兴业证券开户资金安全吗?
- Use of Presto visualization client-yanagishima20.0
- redis的dict的扩容机制(rehash)
- try-catch-finally
- 元素定位不到的 9 种情况
- Niuke.com: host scheduling
- What should I do to dynamically add a column and button to the gird of VFP?
猜你喜欢

Specific meanings of node and edge in Flink graph

VFP uses Kodak controls to control the scanner to solve the problem that the volume of exported files is too large

Explain websocket protocol in detail

CMU提出NLP新范式—重构预训练,高考英语交出134高分

Comment TCP gère - t - il les exceptions lors de trois poignées de main et de quatre vagues?

Old ou, a fox friend, has had a headache all day. The VFP format is always wrong when it is converted to JSON format. It is actually caused by disordered code

Leetcode 1249. Remove invalid brackets (awesome, finally made)

西山科技冲刺科创板:拟募资6.6亿 郭毅军夫妇有60%表决权

Yisheng biological sprint scientific innovation board: 25% of the revenue comes from the sales of new crown products, and it is planned to raise 1.1 billion yuan

Startups must survive
随机推荐
文献之有效阅读
Is it safe to open a stock account on your mobile phone? Who can I open an account with?
Comparator (for arrays.sort)
9 cases where elements cannot be located
Xishan technology rushes to the scientific innovation board: it plans to raise 660million yuan. Guoyijun and his wife have 60% of the voting rights
Detailed explanation of spark specification
金太阳教育美股上市:市值3.6亿美元 成小盘中概股
客从何处来
JVM shutdown hook details
How terrible is it not to use error handling in VFP?
Research on parallel computing architecture of meteorological early warning based on supercomputing platform
How PHP extracts image addresses from strings
现在网上炒股开户身份证信息安全吗?
子类A继承父类B, A a = new A(); 则父类B构造函数、父类B静态代码块、父类B非静态代码块、子类A构造函数、子类A静态代码块、子类A非静态代码块 执行的先后顺序是?
Old ou, a fox friend, has had a headache all day. The VFP format is always wrong when it is converted to JSON format. It is actually caused by disordered code
Detailed explanation of Flink checkpoint specific operation process and summary of error reporting and debugging methods
Sword finger offer II 091 Painting house: application of state machine DP
Thirty lines of code prevent VFP forms from running repeatedly, and the function supports parameter transfer
兴业证券是国企吗?在兴业证券开户资金安全吗?
redis的dict的扩容机制(rehash)