当前位置:网站首页>How to ensure the consistency of database and cache data?
How to ensure the consistency of database and cache data?
2022-08-01 08:35:00 【Shum technology curtilage】
Foreword
In the actual project development, in order to improve the response speed, the hot data is usually saved in the cache, which reduces the query of the database and effectively improves the response speed of the server. However, after adding the cache, the consistency between the cache and the database is also introduced.This article will explain in detail how to ensure the consistency between the database and the cache.
Cache usage policy
When using cache, the usual cache rates are as follows:
- Cache-Aside Pattern (bypass cache, commonly used in business systems)
- Read-Through Pattern
- Write-Through Pattern
- Write-Behind Pattern
Cache-Aside Pattern
Cache-Aside Pattern is referred to as the cache-aside pattern. The operations of reading the cache, reading the database and updating the cache are all completed in the application system, and it is also the most commonly used caching strategy for business systems.However, the bypass routing strategy is divided into read cache and write cache.
Read cache

Description:
- When the application needs to read data from the database, it first checks whether the cached data is hit.
- If the cache misses, query the database to get the data, and write the data to the cache at the same time, so that subsequent reads of the same data will hit the cache, and finally return the data to the caller.
- If the cache hits, return directly to the caller.
Write cache

The process of writing the cache is relatively simple, first update the data in the database, and then delete the old cache.
Cache-Aside Pattern consistency problem scenario analysis
The Cache-Aside Pattern (cache bypass) mode is most used in actual projects. Under this strategy, the client reads the cache first, and returns if it hits. If it does not hit, the database is queried and data is written to the cache concurrently., because both the database and the cache need to be modified, in high concurrency scenarios, data inconsistency may be caused. For the data inconsistency scenarios, four update schemes are provided as follows:
- Update the cache first, then update the database.
- Update the database first, then update the cache.
- Delete the cache before updating the database.
- Update the database first, then delete the cache.
Next, I will explain the four solutions and the problems of the four solutions.
Update the cache first, then update the database.

Process description: Thread 1: First update the cache successfully, but the network fails to write to the database, which will cause the cache to be the latest data, and the database data to be old data, then the cache is dirty data, Thread 2: Read the cacheData, but this data does not exist in the database, the data does not exist in the database, it is meaningless to cache and return to the client.
This solution is not recommended in actual production.
Update the database first, then update the cache.

Process description:
- Thread 1 successfully updated the database first, but failed to update the cache due to network lag, resulting in the old data in the cache
- Thread 2 reads data from the cache, and the data in the cache is the old number, which causes the database to be inconsistent with the cached data.
This solution is not recommended in actual production.
Delete the cache before updating the database.

Process description:
- Thread 1, delete the cache successfully first, but the update database is abnormal due to network lag.
- Thread 2, since the cache data is empty, the read cache will query the data in the database. The query is successful and written to the cache, resulting in inconsistent data.
This solution is not recommended in actual production.
Update the database first, then delete the cache.

Process description:
- Thread 1 succeeded in writing to the database, but failed to delete cached data due to network lag
- Thread 2 reads the data and reads it as the data in the cache, but when the network returns to normal, the data in the cache will be deleted, so there may be temporary data inconsistency.
Although there is temporary data inconsistency, when bypassing the cache strategy, for write operations: first update the database, and then delete the cache.
Database and cache coherence solutions
Cache Delay Double Delete
Cache delay double deletion is optimized for the third scenario. The specific process is as follows:

Description: First delete the cache, update the database, ensure that the database transaction is successfully submitted, and then sleep for a period of time before deleting the cache.We all know that the third situation is that the database update fails due to network lag. When the network returns to normal, we perform the update database operation, and then delete the cache, then the situation of data inconsistency is the short period of hibernation.Inside.
How to deal with failure to delete cache
Remove cache retry mechanism

The keys that need to be deleted are stored in the message queue, and the deletion is performed asynchronously. If the number of deletion failures has exceeded the maximum number of times, a warning email will be sent, and manual intervention is required.
Summary
This article provides a detailed explanation of the consistency between the database and the cache. Due to the complexity of the database and cache consistency scenarios, each scheme cannot guarantee absolute consistency. According to the CAP theory, we know that the use of the cache system is not strong.Consistent scenarios, in line with APs in CAP, if you have any questions, please provide feedback in time.

边栏推荐
- Chapter 9 of Huawei Deep Learning Course - Convolutional Neural Network and Case Practice
- codeforces每日5题(均1600)-第二十七天
- LevelSequence源码分析
- 数据分析6
- pytest接口自动化测试框架 | 跳过模块
- Shell执行SQL发邮件
- Leicester Weekly 304 6135. The longest ring in the picture Inward base ring tree
- 力扣周赛304 6135. 图中的最长环 内向基环树
- pytest接口自动化测试框架 | 跳过测试类
- Data Analysis 5
猜你喜欢

Redis中间件(从搭建到弃坑)

The soul asks: How does MySQL solve phantom reads?
![[Tear AHB-APB Bridge by hand]~ Why aren't the lower two bits of the AHB address bus used to represent the address?](/img/fb/c95c5857024db001638cd484c5e78f.png)
[Tear AHB-APB Bridge by hand]~ Why aren't the lower two bits of the AHB address bus used to represent the address?

最新的Cesium和Three的整合方法(附完整代码)

【编程之外】当遮羞布被掀开,当人们开始接受一切

22牛客多校1 C.Grab the Seat (几何 + 暴力)

LabVIEW RT中的用户界面更新速度

云原生FAQ

Idea 常用插件

企业数据虚拟化综合指南
随机推荐
力扣周赛304 6135. 图中的最长环 内向基环树
类似 MS Project 的项目管理工具有哪些
C语言中编译时出现警告C4013(C语言不加函数原型产生的潜在错误)
22牛客多校1 J.Serval and Essay (启发式合并)
解析MySQL数据库:“SQL优化”与“索引优化”
搜索框字符自动补全
Ogg synchronizes oracle to mysql, there may be characters that need to be escaped in the field, how to configure escape?
Delphi MDI appliction 文档最大化显示、去掉最大化最小化等按钮
将aof文件转换为命令waoffle安装和使用
Microsoft Azure & NVIDIA IoT developers season I | Azure IoT & NVIDIA Jetson development foundation
zip打包目录所有文件(含隐藏文件/夹)
LevelSequence源码分析
Redis学习
Parsing MySQL Databases: "SQL Optimization" vs. "Index Optimization"
七夕来袭——属于程序员的浪漫
Redis learning
请问用flinksql写入数据到clickhouse需要引入什么依赖吗?
Do I need to introduce any dependencies to write data to clickhouse using flinksql?
[Tear AHB-APB Bridge by hand]~ Why aren't the lower two bits of the AHB address bus used to represent the address?
HoloView 在 jyputer lab/notebook 不显示总结