当前位置:网站首页>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语言猜数字游戏
- Introduction to JSON usage scenarios and precautions
- The difference between vectorresize and reverse.
- 二叉树解题(二)
- Play with concurrency: draw a thread state transition diagram
- Document declaration and character encoding
- Mysql中常见的锁
- Websites that it people often visit
- Pytoch --- use pytoch to predict birds
- Yolov5 network modification tutorial (modify the backbone to efficientnet, mobilenet3, regnet, etc.)
猜你喜欢

Document declaration and character encoding

66.qt quick-qml自定义日历组件(支持竖屏和横屏)

Force buckle 540 A single element in an ordered array

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

初识P4语言

Ognl和EL表达式以及内存马的安全研究

Pytoch --- use pytoch for image positioning

Dare to go out for an interview without learning some distributed technology?

Déchirure à la main - tri

How much can a job hopping increase? Today, I saw the ceiling of job hopping.
随机推荐
【leetcode】81. Search rotation sort array II
Www2022 | know your way back: self training method of graph neural network under distribution and migration
Sorted out an ECS summer money saving secret, this time @ old users come and take it away
Pytorch-Yolov5從0運行Bug解决:
Arbre binaire pour résoudre le problème (2)
Hand tear - sort
Feature Engineering: summary of common feature transformation methods
Recyclerview add header
藍湖的安裝及使用
Free drawing software recommended - draw io
深圳打造全球“鸿蒙欧拉之城”将加快培育生态,优秀项目最高资助 1000 万元
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
CY7C68013A之keil编译代码
PR zero foundation introductory guide note 2
10 minutes to understand CMS garbage collector in JVM
【提高课】ST表解决区间最值问题【2】
Pytoch --- use pytoch for image positioning
[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)
蓝湖的安装及使用
How to solve the code error when storing array data into the database