当前位置:网站首页>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 
边栏推荐
- Seura 2 test code summary
- Qtableview gets all currently selected cells
- 为什么信息化 ≠ 数字化?终于有人讲明白了
- DevOps笔记-05:IT行业中BA、SM、PO、PM、PD、Dev、Ops、QA都是什么角色
- [FPGA mathematical formula] use FPGA to realize common mathematical formulas
- 二叉树序列化与反序列化(leetcode(困难))
- Mobileone: the mobile terminal only needs 1ms of high-performance backbone
- Seekbar custom pictures are not displayed completely up, down, left, right / bitmaptodrawable / bitmaptodrawable inter rotation / paddingstart/paddingend /thumboffset
- 【TcaplusDB知识库】批量复制游戏区
- Source code analysis of go redsync distributed lock
猜你喜欢

分布式id解决方案

ssm项目环境初步搭建

The second meeting of the Second Council of Euler open source community was held, and Xinhua III, hyperfusion and Godson Zhongke became members of the Council

基于可变参模板实现的线程池

Wechat applet development Basics

VG4131SxxxN0S1无线模块硬件规格书
![[tcapulusdb knowledge base] modify business modify cluster](/img/a6/e8067809c8a5d8a222b0ad0d545591.png)
[tcapulusdb knowledge base] modify business modify cluster

4种分布式session解决方案

【C语言】 详解线程退出函数 pthread_exit

Baidu AI Cloud service grid product CSM release 𞓜 hot public beta
随机推荐
Data statistical analysis (SPSS) [3]
【TcaplusDB知识库】查看tcapdir目录服务器
Efficientnetv2 - get smaller models and faster training with NAS, scaling, and fused mbconv
欧拉开源社区第二届理事会第二次会议召开,新华三、超聚变和龙芯中科成为理事会成员单位
云原生周报 | Grafana 9正式发布;云原生词汇表中文版现已上线
Draft competition process of Intelligent Vision Group
迅为龙芯开发板pmon下Ejtag-设置硬件断点指令
logstash启动过慢甚至卡死
【TcaplusDB知识库】TcaplusDB-tcapsvrmgr工具介绍(一)
[interview guide] AI algorithm interview
Data statistical analysis (SPSS) [7]
DevOps笔记-05:IT行业中BA、SM、PO、PM、PD、Dev、Ops、QA都是什么角色
【C语言】 详解线程退出函数 pthread_exit
87.(cesium篇)cesium热力图(贴地形)
go-redsync分布式锁源码解析
88.(cesium篇)cesium聚合图
An annotation elegant implementation of interface data desensitization
go实现分布式锁
2022年 6月27号 《暑假感悟篇一》路程的选择权。
[MCU framework][dfu] DFU upgrade example with CRC verification + timeout mechanism +led indicator + chip locking + chip self erasure