雪崩是大量key同时失效 导致大量请求未命中 导致数据库压力剧增 进而宕机
击穿是某个key 高热 redis没缓存 导致所有对这个key的请求 都发到了数据库 导致宕机
解决方案
定时刷新 延长一些访问量大的key 的过期时间
对某些高热key 设置为永久 人为的的维护
分级缓存 请求优先从 L1 缓存获取数据,如果 L1缓存未命中则加锁,只有 1 个线程获取到锁,这个线程再从数据库中读取数据并将数据再更新到到 L1 缓存和 L2 缓存中,而其他线程依旧从 L2 缓存获取数据并返回。
加锁( 慎用 )
这种方式确实能够防止缓存失效时高并发到数据库,但是缓存没有失效的时候,在从缓存中拿数据时需要排队取锁,这必然会大大的降低了系统的吞吐量.