当前位置:网站首页>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.
边栏推荐
- Shell executes SQL to send emails
- 基于tika实现对文件类型进行判断
- 走进音视频的世界——mp3封装格式
- leetcode-6133: maximum number of groupings
- pytest interface automation testing framework | pass in parameter values in the form of function return values
- centos 安装php7.4,搭建hyperf,转发RDS
- 华为深度学习课程第九章——卷积神经网络以及案例实践
- HoloView -- Tabular Datasets
- 将aof文件转换为命令waoffle安装和使用
- Analysis of High Availability Solution Based on MySql, Redis, Mq, ES
猜你喜欢
随机推荐
A problem with writing to the database after PHP gets the timestamp
ogg同步oracle到mysql,字段里面可能有需要转义的字符,怎么配置转义?
Shell执行SQL发邮件
巧妙利用unbuffer实时写入
leetcode-6132: Make all elements in array equal to zero
leetcode 42. Catch the rain
Optimal dazzle Oracle database support what kinds of type of the time and date
mysql查看cpu使用情况
C语言学习概览(一)
国内外最顶级的8大plm项目管理系统
数据分析5
pytest接口自动化测试框架 | 单个/多个参数
力扣周赛304 6135. 图中的最长环 内向基环树
Graduation thesis writing skills
Mysql database deployment and initialization steps
flink sql-client,怎么处理源端与目标增加端,sql-client包括映射表与JOB如
小程序全面屏手势配置案例
Go supports OOP: use struct instead of class
nodetype中值1、2、3分别代表什么意思
various network protocols