当前位置:网站首页>Data double write consistency between redis and MySQL
Data double write consistency between redis and MySQL
2022-07-04 06:57:00 【No trace after wind 2018】
Redis And MySQL Data double write consistency
1. What is? Redis And MySQL Data double write consistency

2. Synchronous write through and asynchronous cache

3. Several update strategies for database and cache consistency *
Don't allow : Update cache first , The strategy of updating the database
1) Update the database first , Update the cache again
Generally do not use Update the database first , Update the cache again , If Redis Something unusual happened , There will be a problem that the cache reads dirty data
1 To update mysql The inventory of a commodity , The current stock of goods is 100, Updated to 99 individual .
2 To update mysql It is amended as follows 99 success , And then update redis.
3 At this time, it is assumed that the exception appears , to update redis failed , This leads to mysql The inventory inside is 99 and redis Inside is still 100 .
4 The above happened , Will make the database and cache redis The data inside is inconsistent , Read dirty data
2) So let's delete the cache , Update the database
Low concurrent , Small plant If the database update fails , Lead to B When a thread requests to access the cache again , Find out redis There's no data in it , Cache missing , Read again mysql when , Read old value from database
Low concurrent : Write the old data to the cache
High concurrency : Cache breakdown
technological process :
(1) request A Write operation , After deleting the cache , The work is in progress …A It hasn't been completely updated yet
(2) request B starts , Inquire about redis Found cache does not exist
(3) request B continue , Go to the database and get myslq The old value in
(4) request B Write the old value to redis cache
(5) request A Write the new value to mysql database
This leads to inconsistencies .
solve : Delay double delete strategy 
3) Update the database first , Delete the cache
If the cache deletion fails or it is too late , Cause a request to visit again redis Cache hit , What is read is the old cache value .
Enterprises often use
Solution :
1 The cache value to be deleted or the database value to be updated can be temporarily stored in the message queue ( For example, using Kafka/RabbitMQ etc. ).
2 When the program fails to successfully delete the cache value or update the database value , These values can be re read from the message queue , Then delete or update again .
3 If you can successfully delete or update , We need to remove these values from the message queue , So as not to repeat the operation , here , We can also ensure that the database and cached data are consistent , Otherwise, you need to try again
4 If you try again more than a certain number of times and still fail , We need to send an error message to the business layer , Inform the operation and maintenance personnel .
summary

边栏推荐
- 【GF(q)+LDPC】基于二值图GF(q)域的规则LDPC编译码设计与matlab仿真
- What is tweeman's law?
- tars源码分析之8
- Industrial computer anti-virus
- GoogleChromePortable 谷歌chrome浏览器便携版官网下载方式
- Summary of leetcode BFS question brushing
- leetcode 310. Minimum Height Trees
- 抽奖系统测试报告
- Cochez une colonne d'affichage dans une colonne de tableau connue
- BasicVSR++: Improving Video Super-Resolutionwith Enhanced Propagation and Alignment
猜你喜欢

The important role of host reinforcement concept in medical industry

GoogleChromePortable 谷歌chrome浏览器便携版官网下载方式

Responsive mobile web test questions

Bottom problem of figure

P26-P34 third_ template

Download kicad on Alibaba cloud image station

Four sets of APIs for queues

Review of enterprise security incidents: how can enterprises do a good job in preventing source code leakage?

Set JTAG fuc invalid to normal IO port
![[problem record] 03 connect to MySQL database prompt: 1040 too many connections](/img/bb/4d8d202cf5c6e556bc860a734e5934.png)
[problem record] 03 connect to MySQL database prompt: 1040 too many connections
随机推荐
图的底部问题
If there are two sources in the same job, it will be reported that one of the databases cannot be found. Is there a boss to answer
Shopping malls, storerooms, flat display, user-defined maps can also be played like this!
Four sets of APIs for queues
高薪程序员&面试题精讲系列119之Redis如何实现分布式锁?
Review of enterprise security incidents: how can enterprises do a good job in preventing source code leakage?
Redis interview question set
selenium驱动IE常见问题解决Message: Currently focused window has been closed.
tars源码分析之5
Cochez une colonne d'affichage dans une colonne de tableau connue
Bottom problem of figure
云Redis 有什么用? 云redis怎么用?
[MySQL] introduction, function, creation, view, deletion and modification of database view (with exercises)
How can the old version of commonly used SQL be migrated to the new version?
同一个job有两个source就报其中一个数据库找不到,有大佬回答下吗
Check and display one column in the known table column
what the fuck! If you can't grab it, write it yourself. Use code to realize a Bing Dwen Dwen. It's so beautiful ~!
tars源码分析之3
Selection (022) - what is the output of the following code?
Centos8 install mysql 7 unable to start up