当前位置:网站首页>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

边栏推荐
- Su Weijie, a member of Qingyuan Association and an assistant professor at the University of Pennsylvania, won the first Siam Youth Award for data science, focusing on privacy data protection, etc
- MySQL 45 lecture learning notes (12) MySQL will "shake" for a while
- Bottom problem of figure
- tars源码分析之3
- How can the old version of commonly used SQL be migrated to the new version?
- Summary of June 2022
- 2022 where to find enterprise e-mail and which is the security of enterprise e-mail system?
- Mysql 45讲学习笔记(十四)count(*)
- CMS source code of multi wechat management system developed based on thinkphp6, with one click curd and other functions
- 2022, peut - être la meilleure année économique de la prochaine décennie, avez - vous obtenu votre diplôme en 2022? Comment est - ce prévu après la remise des diplômes?
猜你喜欢

2022 where to find enterprise e-mail and which is the security of enterprise e-mail system?

Uniapp applet subcontracting

校园网络问题

Set JTAG fuc invalid to normal IO port

what the fuck! If you can't grab it, write it yourself. Use code to realize a Bing Dwen Dwen. It's so beautiful ~!

Mysql 45讲学习笔记(七)行锁

Wechat applet scroll view component scrollable view area

Knowledge payment applet dream vending machine V2

Research on an endogenous data security interaction protocol oriented to dual platform and dual chain architecture

regular expression
随机推荐
the input device is not a TTY. If you are using mintty, try prefixing the command with ‘winpty‘
tars源码分析之4
测试用例的设计
Another company raised the price of SAIC Roewe new energy products from March 1
Vulhub vulnerability recurrence 76_ XXL-JOB
2022 where to find enterprise e-mail and which is the security of enterprise e-mail system?
Mysql 45讲学习笔记(十二)MySQL会“抖”一下
MySQL relearn 2- Alibaba cloud server CentOS installation mysql8.0
Selenium driver ie common problem solving message: currently focused window has been closed
Set JTAG fuc invalid to normal IO port
Tar source code analysis 9
【FPGA教程案例7】基于verilog的计数器设计与实现
《国民经济行业分类GB/T 4754—2017》官网下载地址
List of top ten professional skills required for data science work
Data analysis notes 09
What is the sheji principle?
Introduction to spark core components
tars源码分析之3
Deep understanding of redis -- a new type of bitmap / hyperloglgo / Geo
What is industrial computer encryption and how to do it