当前位置:网站首页>Redis 做缓存场景引发的问题
Redis 做缓存场景引发的问题
2022-06-21 18:41:00 【InfoQ】

1. 数据一致性

- 先更新缓存,在更新数据库;
- 先更新数据库,在更新缓存;
- 先删除缓存,在更新数据库;
- 先更新数据库,在删除缓存。
1.1 先更新缓存,在更新数据库
1.2 先更新数据库,在更新缓存
- 线程 A 更新数据库;
- 线程 B 更新数据库;
- 线程 B 更新缓存;
- 线程 A 更新缓存。
cache miss1.3 先删除缓存,在更新数据库
- 如果有两个请求,请求 A(更新操作) 和请求 B(查询操作);
- 请求 A 会先删除 Redis 中的数据,然后去数据库进行更新操作;
- 此时请求 B 看到 Redis 中的数据时空的,会去数据库中查询该值,补录到 Redis 中。
- 先淘汰缓存;
- 再写数据库;
- 休眠 1 秒,再次淘汰缓存。
1.4 先更新数据库,在删除缓存
Cache Aside Pattern- 先读缓存,在读数据库;
- 若存在,则返回;
- 若不存在,读取数据库,然后取出数据后放入缓存,同时返回响应。
- 先更新数据库;
- 再删除缓存。
- 缓存刚好失效;
- 请求 A 查询数据库,得一个旧值;
- 请求 B 将新值写入数据库;
- 请求 B 删除缓存;
- 请求 A 将查到的旧值写入缓存。
- 给缓存设计一个过期时间;
- 异步延时删除。
1.5 删除策略存在的问题

- 先对数据库进行更新操作;
- 在对 Redis 进行删除操作的时候发现报错,删除失败;
- 此时将 Redis 的 key 作为消息体发送到消息队列中;
- 系统接收到消息队列发送的消息后;
- 再次对 Redis 进行删除操作。
2. 缓存穿透
2.1 原因分析
2.2 解决方案
- 空值做了缓存,意味着缓存层中存了更多的键,需要更多的内存空间(如果是攻击,问题更严重),比较有效的方法是针对这类数据设置一个较短的过期时间,让其自动剔除。
- 缓存层和存储层的数据会有一段时间窗口的不一致,可能会对业务有一定影响。例如过期时间设置为 5 分钟,如果此时存储层添加了这个数据,那此段时间就会出现缓存层和存储层数据的不一致,此时可以利用数据一致性方案处理。
3. 缓存击穿
3.1 原因分析
3.2 解决方案
public String get(key) {
String value = redis.get(key);
if (value == null) {//代表缓存值过期
// 设置 3min 的超时,防止 del 操作失败的时候,下次缓存过期一直不能 load db
if (redis.setnx(key_mutex, 1, 3 * 60) == 1) {
//代表设置成功
value = db.get(key);
redis.set(key, value, expire_secs);
redis.del(key_mutex);
} else {
//这个时候代表同时候的其他线程已经 load db 并回设到缓存了,这时候重试获取缓存值即可
sleep(50);
get(key); //重试
}
} else {
return value;
}
}- 从 Redis 上看,确实没有设置过期时间,这就保证了,不会出现热点 key 过期问题,也就是物理不过期。
- 从功能上看,如果不过期,那不就成静态的了吗?所以我们把过期时间存在 key 对应的 value 里,如果发现要过期了,通过一个后台的异步线程进行缓存的构建,也就是逻辑不过期。
4. 缓存雪崩
4.1 原因分析
4.2 解决方案
- 保证缓存层服务高可用性
- 和飞机都有多个引擎一样,如果缓存层设计成高可用的,即使个别节点、个别机器、甚至是机房宕掉,依然可以提供服务,例如 Redis Sentinel 和 Redis Cluster 都实现了高可用;
- 设置 key 永不失效(热点数据);
- 设置 key 缓存失效时候尽可能错开;
- 使用多级缓存机制,比如同时使用 Redis 和 Memcache 缓存,请求->redis->memcache->db。
5. Hot Key
5.1 原因
- 用户消费的数据量远超预期
- 在日常工作生活中一些突发的事件,例如:双十一期间某些热门商品的降价促销,当这其中的某一件商品被数万次点击浏览或者购买时,会形成一个较大的需求量,这种情况下就会造成热点问题。同理,被大量刊发、浏览的热点新闻、热点评论、明星直播等,这些典型的读多写少的场景也会产生热点问题。
- 请求分片集中,超过单 Server 的性能极限
- 在服务端读数据进行访问时,往往会对数据进行分片切分,此过程中会在某一主机 Server 上对相应的 Key 进行访问,当访问超过 Server 极限时,就会导致热点 Key 问题的产生。
5.2 危害
5.3 解决方案
6. Big Key
6.1 发现
redis-cli --bigkeys6.2 危害
- 内存空间不均匀
- 例如在 Redis Cluster 中,bigkey 会造成节点的内存空间使用不均匀。
- 超时阻塞
- 由于 Redis 单线程的特性,操作 bigkey 比较耗时,也就意味着阻塞 Redis 可能性增大。
- 网络拥塞
- 每次获取 bigkey 产生的网络流量较大,假设一个 bigkey 为 1MB,每秒访问量为 1000,那么每秒产生 1000MB 的流量,对于普通的千兆网卡(按照字节算是 128MB/s)的服务器来说简直是灭顶之灾,而且一般服务器会采用单机多实例的方式来部署,也就是说一个 bigkey 可能会对其他实例造成影响,其后果不堪设想。
6.3 解决方案
7. Redis 脑裂
7.1 哨兵主从集群脑裂


min-replicas-to-write 1
min-replicas-max-lag 57.2 集群脑裂
边栏推荐
- Snake game project full version
- mysql有没有触发器
- Uniapp obtains login authorization and mobile number authorization (sorting)
- Recycleview lazy load failure (II)
- 技术实践 | 场景导向的音视频通话体验优化
- 第十七届全国大学 RT-Thread创新专项奖
- Custom code template
- Post to asp Net core length limitation and solution when transferring data
- Is there any difference between MySQL and Oracle
- 点云转深度图:转化,保存,可视化
猜你喜欢

Leetcode personal question solution (Sword finger offer 21-25) 21 Adjust the array order so that odd numbers precede even numbers, 22 The penultimate node in the linked list, 24 Reverse linked list, 2

【微信小程序】协同工作和发布 数据绑定

uniapp小程序打开地图选择位置demo效果wx.chooseLocation(整理)

Selected articles of the research paper | interpretation of the trend of McKinsey's China's Digital Innovation future
![[wechat applet] collaboration and publishing data binding](/img/9a/a986fe169cf9bee0bb109750590a22.png)
[wechat applet] collaboration and publishing data binding

A simple architecture design logic | acquisition technology
![[complete course of time series prediction] take temperature prediction as an example to illustrate the composition of the paper and the construction of pytorch code pipeline](/img/fe/c204ea78051fb0544b27d44e6d1297.png)
[complete course of time series prediction] take temperature prediction as an example to illustrate the composition of the paper and the construction of pytorch code pipeline

Comment MySQL additionne les colonnes

mysql如何對列求和

Recycleview lazy load failure (II)
随机推荐
【CVPR2022】CMU《多模态机器学习》教程,200+页阐述表示、对齐、推理、迁移、生成与量化六大挑战的多模态学习系统知识
break和continue的区别
NSIS 运行bat的方法汇总
mysql如何查询最大id值
1157 Anniversary
如何查询mysql中所有表
[complete course of time series prediction] take temperature prediction as an example to illustrate the composition of the paper and the construction of pytorch code pipeline
How MySQL sums columns
07_那些可以改变你认知的知识
Using fastjson to deserialize simplegrantedauthority in the security framework
Taoist Zhang Zhishun's self narration
Manjaro installs the downloaded TTF font file
MySQL must know - Chapter 9 - Search with regular expressions
mysql增加的语句是什么
How to debug reorganization in jetpack compose
某大厂第二轮裁员来袭,套路满满
Uniapp obtains login authorization and mobile number authorization (sorting)
A simple architecture design logic | acquisition technology
RecycleView拖动效果
inno setup 窗口拖动学习