当前位置:网站首页>Cache consistency solution - how to ensure the consistency between the cache and the data in the database when changing data
Cache consistency solution - how to ensure the consistency between the cache and the data in the database when changing data
2022-07-02 04:24:00 【Bald and weak.】
List of articles
Write it at the front
We all know , Caching is to improve the reading speed of data , The scenario to deal with is the scenario of reading more and writing less .
When reading data, we usually read the cache first , If the cache does not read the database again, then update the cache .
From query database performance optimization to redis cache - Talk about cache penetration 、 An avalanche 、 breakdown
When the cached data needs to be modified , To modify the cache , You have to modify the database , How to ensure that the cache is consistent with the data in the database ?
How to cache data consistency
Double write mode
The double write mode is to write the database first , Write cache again :
disadvantages
High and low , Due to Caton and other reasons , Causes write caching 2 At the front , Write cache 1 rearwards , This leads to the inconsistency between the cache and the database data , That is, there is dirty data .
however , This kind of dirty data is only temporary , Depends on the expiration time of the cache , When the cache expires , Reading the database again will get the latest correct data . in other words , The latest data is that there may be a certain delay , But the final result is consistent .
Failure mode
disadvantages
In the case of high concurrent read and write , Write db2 The deletion cache of is executed in read db1 In front of the update cache of , This will cause inconsistency between the cache and the database , That is, there is dirty data .
however , This kind of dirty data is only temporary , Depends on the expiration time of the cache , When the cache expires , Reading the database again will get the latest correct data . in other words , The latest data is that there may be a certain delay , But the final result is consistent .
analysis
Whether it's dual write mode or failure mode , Will cause cache inconsistencies . That is, multiple instances ( database 、 cache ) Updating at the same time will cause dirty data . What do I do ?
1、 If it's user dimension data ( Order data 、 User data ), This concurrency rate is very small ( The action that the user clicks won't be fast ), Don't think about it , Even if there is dirty data , Cache data plus expiration time , Trigger the active update of read at regular intervals .
2、 If it's a menu , Basic data such as commodity introduction , If the demand for real-time is high , You can also use canal subscribe binlog The way .
3、 Cache data + The expiration time is also sufficient to solve the cache requirements of most businesses ( Business is not about heart data , Allow temporary dirty data to be ignored ).
4、 Ensure concurrent read and write by locking , Line up as you write . It doesn't matter to read . So it's suitable to use read-write lock .
5、 Locking will make the system bulky , So can we not add it or not , But the requirements for data consistency are very high , That can only be locked .
summary
1、 The data we can put into the cache should not be real-time 、 The requirement of consistency is very high . So when caching data Plus the expiration time , Ensure to get the latest data every day or after a period of time .
2、 We should not over design , Increase the complexity of the system , For distributed locks , Don't add if you can ; For cached dirty data , If you can tolerate dirty data for a period of time, you don't need to deal with , Otherwise, the system will be too heavy to maintain .
3、 When it comes to real-time 、 Data with high consistency requirements , And write more scenes , You should check the database , Even if you slow down .
4、 Reading more and writing less , If you have high requirements for data consistency , You can guarantee concurrent reading and writing by locking , Line up as you write . It doesn't matter to read . So it is suitable for Read-write lock .
5、 Use canal subscribe binlog Can solve the problem of data consistency , But new middleware has been introduced .
The way we usually use
1、 All cached data will be added with expiration time , When the data is out of date, the next query triggers active update .
2、 Reading and writing data , Plus distributed read-write locks .
expand : Use canal subscribe binlog
binlog yes mysql Binary log , For statements that operate the database , All in this form .Canal It's Ali MySQL database Binlog Incremental subscription of & Consumer component . database-based Binlog It can monitor the changes of database data and then be used for data synchronization and other services .
In short , Is to subscribe to the database binlog, If the database is modified , Get this modification event and modify the cache at the same time , Ensure cache consistency .
This method introduces new middleware , Make the system more complicated . But perfect ensures cache consistency , Usually it only needs to be maintained once , Subsequent maintenance is not required .
边栏推荐
- C language practice - binary search (half search)
- Sorted out an ECS summer money saving secret, this time @ old users come and take it away
- Go branch and loop
- Federal learning: dividing non IID samples according to Dirichlet distribution
- CorelDRAW Graphics Suite2022免费图形设计软件
- Play with concurrency: draw a thread state transition diagram
- Force buckle 540 A single element in an ordered array
- Use a mask to restrict the input of the qlineedit control
- Arbre binaire pour résoudre le problème (2)
- 【leetcode】34. Find the first and last positions of elements in a sorted array
猜你喜欢
Introduction to vmware workstation and vSphere
Pytoch --- use pytoch for image positioning
66.qt quick-qml自定义日历组件(支持竖屏和横屏)
office_ Delete the last page of word (the seemingly blank page)
CorelDRAW Graphics Suite2022免费图形设计软件
Deeply understand the concepts of synchronization and asynchrony, blocking and non blocking, parallel and serial
Yolov5网络修改教程(将backbone修改为EfficientNet、MobileNet3、RegNet等)
66.qt quick QML Custom Calendar component (supports vertical and horizontal screens)
Three years of experience in Android development interview (I regret that I didn't get n+1, Android bottom development tutorial
Thinkphp內核工單系統源碼商業開源版 多用戶+多客服+短信+郵件通知
随机推荐
Force buckle 540 A single element in an ordered array
Go language naming specification
Binary tree problem solving (1)
Play with concurrency: what's the use of interruptedexception?
Introduction to vmware workstation and vSphere
pip 安装第三方库
Wechat applet - realize the countdown of 60 seconds to obtain the mobile verification code (mobile number + verification code login function)
One click generation and conversion of markdown directory to word format
Wechat applet pull-down loading more waterfall flow loading
Okcc why is cloud call center better than traditional call center?
Bitmap principle code record
Installation et utilisation du lac bleu
Alibaba cloud polkit pkexec local rights lifting vulnerability
Shenzhen will speed up the cultivation of ecology to build a global "Hongmeng Oula city", with a maximum subsidy of 10million yuan for excellent projects
Wechat applet JWT login issue token
初识P4语言
How to solve the problem that objects cannot be deleted in Editor Mode
PR zero foundation introductory guide note 2
如何解决在editor模式下 无法删除物体的问题
How muddy is the water in the medical beauty industry with a market scale of 100 billion?