当前位置:网站首页>Headache delayed double deletion
Headache delayed double deletion
2022-07-04 04:56:00 【#HashMap#】
I often see a problem redis and mysql There are several ways of data synchronization , Which is more applicable , Although personal feeling is not difficult , But sometimes I can't understand , So I made a summary , And the way of using diagrams is easy to understand .
redis and mysql There are five cases of data synchronization :
1. Update the database , Update cache
2. Update cache , Update the database
3. Delete cache , Update the database
4. Update the database , Delete cache
5. Delete cache , Update the database , Delay deleting cache
1. Update the database , Update cache

The scene in the picture is a mysql and redis The initial values of the database are 10, user 1( Users above ) At this time, the update operation is performed , hold num Change to 9, here mysql The database is updated to 9, But before updating the cache, the user 2( The following users ) Query operation , At this time, the reading is redis The old value of is 10,mysql and redis Data inconsistency . In conclusion, it is step 2 And steps 3 Insert a thread access , In this way, we can better understand .
2. Update cache , Update the database
This situation is similar to the first principle .
3. Delete cache , Update the database


First picture user 1 To update the data num=9, Perform step at this time 1 Delete cache ,redis Medium 10 Be deleted , But at this time, the user 2 Execute the query operation because redis It was deleted before, so I will query mysql, At this time mysql Because it hasn't been updated yet, the user 2 Got The value is still 10, And cache to redis in , Finally, perform the database update operation ,mysql The data becomes 9.mysql and redis Data inconsistency .
4. Update the database , Delete cache


Similar to the above , user 1 Request update operation num=9, Perform step at this time 1, Update the database ,mysql The value of the into 9,
But at this time, the user 2 Query operation , Find out redis It's worth returning 10, Although at this time mysql and redis The data are inconsistent , But the last step 4, After deleting the cache, the data accessed by other threads is consistent , That is to say, this process is the final consistency , In the middle, only part of the data is out of sync , Is there a solution ? Delayed double deletion can solve !
5. Delete cache , Update the database , Delay deleting cache


Finally, it's time to delay double deletion ! user 1 Update data num=9, Then follow the steps 2 Delete cache , At this time, the user 2 To query data , because redis The cache was deleted in the previous step , So search mysql Database access num=10, And cache to redis in , Then follow the steps 4 Update the database mysql obtain num=9, At this time, delay for a period of time before deleting the cache , The next time other users visit mysql The data in is synchronized to redis Achieve data synchronization ! But due to the delay , Resulting in poor performance , Throughput is not high , Not suitable for high concurrency scenarios .
Why delay ?
Compare with the second picture , here redis No value , user 2 Go to query mysql database , Then return the value and build the cache , If you delete the cache directly instead of delaying it , Then it is possible that the return value construction cache has not been completed , In this case, delete the cache directly , And will turn from mysql Return value ( The old value ) Build cache , It is equivalent to having no effect . To put it bluntly , The purpose of delay is to let users 2 There is time to mysql The queried value is built to redis in , Only after it is built can it be deleted Ensure final redis No cache in .
additional :
MQ Retry mechanism

Use canal

summary : commonly redis and mysql Update the database before using data synchronization , Delete the cache again , Reach final consistency , Although delayed double deletion can ensure data consistency, it is not suitable for high concurrency scenarios due to the need for delay , In high concurrency scenarios, you can use MQ Retry mechanism , If deleting the cache fails, try again consistently , But high coupling . The low coupling solution is to use canal.canal Disguised as a mysql Slave of , Monitor host mysql Binary file , Send to... When data changes MQ.
边栏推荐
- Operate the server remotely more gracefully: the practice of paramiko Library
- 通过dd创建asm disk
- 关于solidworks standard无法获得许可 8544问题的总结
- Flutter ‘/usr/lib/libswiftCore.dylib‘ (no such file)
- 【MATLAB】MATLAB 仿真 — 模拟调制系统 之 AM 调制过程
- Beipiao programmer, 20K monthly salary, 15W a year, normal?
- RAC delete damaged disk group
- [cloud native] those lines of code that look awesome but have a very simple principle
- Unity中RampTex介绍和应用: 溶解特效优化
- 【MATLAB】MATLAB 仿真 — 窄带高斯白噪声
猜你喜欢

Zhengzhou zhengqingyuan Culture Communication Co., Ltd.: seven marketing skills for small enterprises

YoloV6实战:手把手教你使用Yolov6进行物体检测(附数据集)

每日刷题记录 (十二)

The "functional art" jointly created by Bolang and Virgil abloh in 2021 to commemorate the 100th anniversary of Bolang brand will debut during the exhibition of abloh's works in the museum

关于solidworks standard无法获得许可 8544问题的总结

如何构建属于自己的知识引擎?社群开放申请

Deep parsing structured exception handling (SEH) - by Matt Pietrek

Unity中RampTex介绍和应用: 溶解特效优化

2022年6月总结

Formatted text of Kivy tutorial (tutorial includes source code)
随机推荐
PostgreSQL 正式超越 MySQL,这家伙也太强了吧!
在代碼中使用度量單比特,從而生活更美好
《Cross-view Transformers for real-time Map-view Semantic Segmentation》论文笔记
Unity中RampTex介绍和应用: 溶解特效优化
Kivy tutorial 07 component and attribute binding implementation button button click to modify the label component (tutorial includes source code)
【无标题】
附件2-2保密承诺书.docx
【MATLAB】通信信号调制通用函数 — 傅里叶逆变换
令人头痛的延时双删
【MATLAB】通信信号调制通用函数 — 插值函数
【MATLAB】通信信号调制通用函数 — 窄带高斯白噪声的生成
Can closed data be deleted by DBCA? can
Cmake compilation option setting in ros2
The five pictures tell you: why is there such a big gap between people in the workplace?
附件三:防守方评分标准.docx
qt下开发mqtt的访问程序
Acwing game 58
[go] database framework Gorm
【MATLAB】MATLAB 仿真模拟调制系统 — SSB 系统
红队视角下的防御体系突破之第二篇案例分析