当前位置:网站首页>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 .
边栏推荐
- Www 2022 | rethinking the knowledge map completion of graph convolution network
- LCM of Spreadtrum platform rotates 180 °
- C language guessing numbers game
- FAQ | FAQ for building applications for large screen devices
- CorelDRAW Graphics Suite2022免费图形设计软件
- Message mechanism -- message processing
- The core idea of performance optimization, dry goods sharing
- The confusion I encountered when learning stm32
- Spring recruitment of Internet enterprises: Kwai meituan has expanded the most, and the annual salary of technical posts is up to nearly 400000
- 蓝湖的安装及使用
猜你喜欢

Installation and use of blue lake

Target free or target specific: a simple and effective zero sample position detection comparative learning method

Hand tear - sort

Opencv learning example code 3.2.4 LUT

pip 安装第三方库

【leetcode】74. Search 2D matrix

The original author is out! Faker. JS has been controlled by the community..

CorelDRAW Graphics Suite2022免费图形设计软件

Why can't you remember when reading? Why can't you remember- My technology learning methodology

Analysis of the overall design principle of Nacos configuration center (persistence, clustering, information synchronization)
随机推荐
Pytorch---使用Pytorch实现U-Net进行语义分割
二叉树解题(一)
The original author is out! Faker. JS has been controlled by the community..
Wechat applet - realize the countdown of 60 seconds to obtain the mobile verification code (mobile number + verification code login function)
2022-07-01: at the annual meeting of a company, everyone is going to play a game of giving bonuses. There are a total of N employees. Each employee has construction points and trouble points. They nee
PR zero foundation introductory guide note 2
Wechat applet JWT login issue token
Go branch and loop
MySQL error: expression 1 of select list is not in group by claim and contains nonaggre
Yyds dry inventory compiler and compiler tools
深圳打造全球“鸿蒙欧拉之城”将加快培育生态,优秀项目最高资助 1000 万元
Three years of experience in Android development interview (I regret that I didn't get n+1, Android bottom development tutorial
Li Kou interview question 02.08 Loop detection
Go language naming specification
Binary tree problem solving (2)
Handling of inconsistency between cursor and hinttext position in shutter textfield
Exposure X8 Standard Version picture post filter PS, LR and other software plug-ins
Alibaba cloud polkit pkexec local rights lifting vulnerability
PIP installation of third-party libraries
Play with concurrency: what's the use of interruptedexception?