当前位置:网站首页>How MySQL and redis ensure data consistency
How MySQL and redis ensure data consistency
2022-07-27 05:47:00 【Code world of super Duoduo and Liu baobao】
List of articles
Preface
How to ensure the consistency of database and cache double write , Here are a few ways , Let's discuss their advantages and disadvantages .
One 、 Update the database first , Update again redis
This program , No one should adopt .
If the database is successfully updated first , Then update redis Failure , Then the data will be inconsistent , So this method abandons
Two 、 To update redis, Updating the database
This scheme is similar to the first , It also has the same problem
If updated reids success , Database update failed , It will also cause data inconsistency
3、 ... and Update the database first , And then delete redis
This program , It will also cause data inconsistency , But compared with the previous two schemes , If he sets key The expiration time of , Then the final consistency of the data is guaranteed . If you delete after updating the database redis Failure , It is not set redis Expiration time . Then the data will be inconsistent .
If the thread A Update the database , Preparing to update redis when . Threads B Updating threads A to update redis I got redis Data in , Then the data obtained from other data is still old data , If you remove redis Failure will also cause data inconsistency
Solution :
1.mysql and redis Set up transactions , Rollback data when an exception occurs
2.redis Set retry mechanism , Enter retry mode after deletion failure
Four Delete first redis, Update the database
This program , There is also a certain probability of inconsistency , But it is close to the optimal .
If the thread A Deleted redis, Preparing to update database . Threads B The query redis No after , Query the old data of the database , And write it down redis. After that thread A To update the data successfully , Database and redis The data are inconsistent
Solution : Delay double delete
Threads A In the delete redis And after updating the database , After a period of sleep , Delete again reids Data in . This sleep time must be greater than the time of a query .
summary
There is no absolute guarantee of data consistency , Or lock at the expense of performance , Or serial . At high level , These schemes can only be optimized
边栏推荐
- Which futures company do you go to and how do you open an account?
- Fortex Fangda releases the electronic trading ecosystem to share and win-win with customers
- 个人收款码不得用于经营收款
- How can seektiger go against the cold air in the market?
- Getaverse,走向Web3的远方桥梁
- 难道Redis真的变慢了吗?
- The difference between for... Of and for... In JS
- 建设创客教育运动中的完整体系
- 我的大四
- 2022/7/26 考试总结
猜你喜欢
![[MRCTF2020]PYWebsite 1](/img/d4/2d9cd06abd7188add668cde77d3075.png)
[MRCTF2020]PYWebsite 1

The LAF protocol elephant of defi 2.0 may be one of the few profit-making means in your bear market

期货公司开户的具体事项

亚马逊测评自养号,如何进行系统性的学习?

Several ways of element positioning in page layout

如果在线上遇到了OOM,该如何解决?

Which futures company has a low handling fee and a high refund?

期货开户公司交返怎么申请?

Web3 traffic aggregation platform starfish OS interprets the "p2e" ecosystem of real business

Differences among apply, call, and bind in JS
随机推荐
原油期货开户条件和流程是什么?
Okaleido launched the fusion mining mode, which is the only way for Oka to verify the current output
How to open a general commodity futures account
The difference between deep copy and shallow copy in JS
Seektiger's okaleido has a big move. Will the STI of ecological pass break out?
手把手教你搭建钉钉预警机器人
The main advantage of face brushing payment users is their high degree of intelligence
刷脸商业逻辑重大改变商户争抢烽烟再起
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
NFT新范式,OKALEIDO创新NFT聚合交易生态
If you encounter oom online, how to solve it?
What are the conditions and procedures for opening crude oil futures accounts?
MySQL索引分析除了EXPLAIN还有什么方法
You should negotiate the handling fee before opening a futures account
Which futures company has a low handling fee and a high refund?
未来刷脸支付是能够占据市场很多的份额
Fortex Fangda releases the electronic trading ecosystem to share and win-win with customers
Fortex方达发布电子交易生态体系 与客户共享共赢
Graph node deployment
你真的了解 Session 和 Cookie 吗?