当前位置:网站首页>MySQL和Redis的双写一致性
MySQL和Redis的双写一致性
2022-07-29 01:58:00 【Meme_xp】
什么是双写一致性问题?
先看看我们redis读取过程:
(1)先读cache,如果数据命中则返回
(2)如果数据未命中则读db
(3)将db中读取出来的数据入缓存
策略1:先更新缓存,再更新数据库
如果跟新缓存成功更新数据库失败,那么就会造成缓存脏数据
策略2:先更新数据库,再更新缓存
高并发会出现这种情况:

线程A跟新数据库,由于网络或者其他原因,这个时候没有来得及跟新缓存的时候,线程跟新了数据库,并且跟新了缓存,但是A 又跟新了缓存。导致我们的线程B跟新丢失
策略3:先删除缓存,再更新数据库
先删除缓存再跟新数据库,这种就是以及缓解了策略2的缓存跟新丢失情况,但是依然可能不一致
A要删除缓存,B去读操作,发现再redis中没有命中,所以B线程要去数据库中读取,并将这个数据写到redis中,然后线程A完成对数据库的跟新,就出现了数据库和redis缓存不一致的情况

如何解决:
只需要在线程A完成数据库跟新后,再删除 一次缓存,这就叫做延迟双删,这里的延迟时间一定要大于我们的业务的读取的时间
策略4:先更新数据库,再删除缓存
当我们A查询数据,正准备写入缓存的时候,线程B更新了数据库,然后执行了删除缓存的操作,然后A才把之前的旧值写入到redis。出现概率很低,因为写操作时间大概率大于读操作时间
解决方法:
延迟双删,但是删除失败怎么办?
那就不断地循环删除了,我们可以将要删除的key放入队列,然后重复尝试删除,直到成功
边栏推荐
- 开启TLS加密的Proftpd安全FTP服务器安装指南
- 密码安全如何保障?安全浏览器如何管理密码?
- QT qstackedwidget multi interface switching
- Never pass a request to an asynchronous thread. There is a hole
- 外包公司“混”了2年,我只认真做了5件事,如今顺利拿到字节 Offer。
- What if there is not enough time for adequate testing?
- 如何在多御安全浏览器中自定义新标签页?
- ES6 syntax extension
- Kubesphere-多节点安装
- 聊聊 Feign 的实现原理
猜你喜欢

结合Retrofit 改造OKHttp 缓存

MySQL基本操作和基于MySQL基本操作的综合实例项目

PS + PL heterogeneous multicore case development manual for Ti C6000 tms320c6678 DSP + zynq-7045 (2)

Meeting notice of meeting OA

Responsive dream weaving template hotel room website

I was stunned by this question that I browsed 746000 times

Never pass a request to an asynchronous thread. There is a hole

ES6详解 快速上手!

Explain the four asynchronous solutions of JS in detail: callback function, promise, generator, async/await

The first of the five tips for using browsers efficiently is the most practical
随机推荐
裂开了,一次连接池参数导致的雪崩问题
ES2022 的 8 个实用的新功能
The first of the five tips for using browsers efficiently is the most practical
我被这个浏览了 746000 次的问题惊住了
一文理解分布式开发中的服务治理
How awesome is the architecture of "12306"?
Summarize in the middle of the year | talk to yourself, live in the present, and count every step
Responsive dream weaving template hotel room website
聊聊 Feign 的实现原理
ResNet50+k折交叉验证+数据增强+画图(准确率、召回率、F值)
Responsive Zhimeng template decoration design website
Data query of MySQL (multi table query)
Esbuild Bundler HMR
响应式织梦模板酒店客房类网站
Talk about 11 tips for interface performance optimization
Talk about the implementation principle of feign
After 4 years of testing experience, I finally entered Alibaba. Two months later, I chose to resign naked
线上3d数字展厅制作方案及优点
[RT learning note 1] RT thread peripheral routine - control LED light flashing
详解异步任务:任务的状态及生命周期管理