当前位置:网站首页>How to keep database and cache consistent
How to keep database and cache consistent
2022-06-29 03:54:00 【Folded biscuit】
List of articles
- The server access speed is getting worse , How to solve
- Update the database first or update the cache first
- Do not update cache , The strategy of deleting the cache
- The write strategy is to update the database first , Delete cache after , Delete the cache first , Update the database after
- Update the database first , Delete cache after , This will affect the hit rate of the cache
- Update the database first , Delete the cache , There may be a problem that deleting the cache failed
- Since deleting the cache may fail , How to ensure that both operations can be performed successfully
The server access speed is getting worse , How to solve
Add... To the server Redis, Let it be the cache of the database
This means that when the customer requests data , If you can hit data in the cache , Query cache , No need to check the database , So as to reduce the pressure of the database , Improve server performance
Update the database first or update the cache first
- Update the database first , Update the cache again

- Update cache first , Update the database

Because of concurrency , Both of these methods may cause inconsistency between database and cache data
Do not update cache , The strategy of deleting the cache
Cache Aside Strategy ( Bypass cache strategy ): Do not update cache , Instead, delete the data in the cache , When reading data , If there is no data in the cache , Then read data from the database , Update to cache
Reading strategy :
If the read data hits the cache , Then directly return the data
If the read data does not hit the cache , Read data from the database , Then write the data to the cache , And back to the user
Write strategy :
Update the data in the database
Delete data in cache
The write strategy is to update the database first , Delete cache after , Delete the cache first , Update the database after
So let's delete the cache , Update the database after
A To update database values ,B To read this value
After deleting the cache on write , Cache misses when there are read requests , Will be updated to the old database value , Then the database is updated
When reading + When writing concurrent , The cache and database are inconsistent
Update the database first , Delete cache after
The value does not exist in the cache ,A To read this value , When the cache is not written ,B To update this value
Because cache writes tend to be faster than database writes , It is very difficult to appear in practice B The database has been updated and the cache deleted , request A Is updated to the cache
Data consistency can be guaranteed
To make sure that there is no risk , You can also add... To the cached data Expiration time , Ensure the final consistency of cached data
Update the database first , Delete cache after , This will affect the hit rate of the cache
If the business has high requirements for hit rate , May adopt Update the database + Update cache The plan , Because there will be no cache miss when updating the cache
But if two threads update them concurrently , The data will be inconsistent due to the different writing order
resolvent :
- Before cache update , First plus Distributed lock
Ensure that only one request is run at the same time to update the cache , There will be no concurrency problems , But with the introduction of locks , Impact on write performance
2. When the cache is updated , Add a shorter expiration time to the cache
Even if cache data is inconsistent , Cached data will also expire very quickly , It's acceptable for business
Yes So let's delete the cache , Update the database after The solution to the concurrency problem —— Delay double delete
Delete cache
Update the database
sleep
Delete database
It is difficult to specify the specific sleep time , This method can only guarantee consistency as much as possible , It is recommended to update the database first , Then delete the cache scheme
Update the database first , Delete the cache , There may be a problem that deleting the cache failed
If delete cache fails , This will cause inconsistency between the database and the cache
Old database values , Cache save new value
If the cache has an expiration time , From the user's perspective , The data updated by the user is not changed immediately , It took a while for the changes to occur
Since deleting the cache may fail , How to ensure that both operations can be performed successfully
Adopt asynchronous operation cache
- Retry mechanism
- subscribe Mysql binlog, Reoperation cache
1. Retry mechanism
Message queues can be introduced , The second operation ( Delete cache ) The data to be manipulated is added to the message queue . Consumers manipulate data
Delete cache failed :
Adopt retry mechanism , Reread data from the message queue , Then delete the cache again
If the number of retries exceeds a certain number of times , It is necessary to report errors to the business level
Delete cache succeeded :
Remove data from the message queue , Avoid repetition
2. subscribe Mysql binlog, Reoperation cache
Update the database first , When the database is updated successfully , A change log will be generated , Recorded in the binlog in
We can subscribe binlog journal , Get the specific data to operate , Then perform cache deletion
Alibaba open source Canal Middleware is based on this implementation
Canal simulation MySQL Master slave replication interaction protocol , Disguise yourself as a MySQL The slave node , towards Mysql The master node sends dump request ,Mysql Upon receipt of the request , It's going to start pushing Binlog to Canal,Canal analysis Binlog After byte stream , Convert to easy to read structured data , For downstream application subscription 
边栏推荐
- 87. (cesium chapter) cesium thermal map (pasted with terrain)
- 4种分布式session解决方案
- 人大金仓(KingBase)导出表结构
- [nrf51][nrf52] nrfconnect installation instructions guide
- Seura 2 test code summary
- [ruoyi] ztree initialization
- 【TcaplusDB】祝大家端午安康!
- Qtableview gets all currently selected cells
- Technology: how to design zkvm circuit
- 【C语言】解决 “address of stack memory associated with local variable ‘num‘ returned”
猜你喜欢

Baidu AI Cloud service grid product CSM release 𞓜 hot public beta

【TcaplusDB知识库】TcaplusDB-tcapulogmgr工具介绍(二)

【布里渊现象】光纤布里渊温度和应变分布同时测量系统研究

在命令行登录mysql数据库以及查看版本号

访问数据库时出现错误

Whose encryption key is written in the code? (that's you)
![[data update] NPU development data based on 3568 development board is fully upgraded](/img/c3/fdbaed3ebfe2dcdeee66e509598005.jpg)
[data update] NPU development data based on 3568 development board is fully upgraded

Set hardware breakpoint instruction for ejtag under the PMON of the Godson development board

Why is informatization ≠ digitalization? Finally someone made it clear

《运营之光3.0》全新上市——跨越时代,自我颠覆的诚意之作
随机推荐
Influence of air resistance on the trajectory of table tennis
Data collection and management [7]
Draft competition process of Intelligent Vision Group
87. (cesium chapter) cesium thermal map (pasted with terrain)
Data collection and management [2]
20款IDEA 神级插件 效率提升 30 倍,写代码必备
【FPGA+sin】基于DDS(直接数字合成)的正弦信号发生器模块FPGA实现
[tcapulusdb knowledge base] tcapulusdb technical support introduction
Seura 2 test code summary
人大金仓(KingBase)导出表结构
Black screen and error reporting when loading custom models for gazebo with roslaunch
【FPGA数学公式】使用FPGA实现常用数学公式
2022年 6月27号 《暑假感悟篇一》路程的选择权。
【TcaplusDB知识库】TcaplusDB技术支持介绍
PostgreSQL 出现cross-database references are not implemented的bug
[dynamic planning] change exchange
leetcode:304. 2D area and retrieval - matrix immutable
百度智能云服务网格产品CSM发布 | 火热公测中
【C语言】详解线程回收函数 pthread_join
【TcaplusDB知识库】批量复制游戏区