当前位置:网站首页>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
边栏推荐
- Integration and extension of robot programming and interdisciplinary
- 「PHP基础知识」PHP语句和语句块
- Fortex方达发布电子交易生态体系 与客户共享共赢
- Move protocol launched a beta version, and you can "0" participate in p2e
- NFT市场格局仍未变化,Okaleido能否掀起新一轮波澜?
- Mysql分组后时并行统计数量
- 「PHP基础知识」布尔型的使用
- MySQL二级索引中的主键——MySQL存在大量相同数据分页查询优化
- 选择国企背景的期货公司开户
- Seven enabling schemes of m-dao help Dao ecology move towards mode and standardization
猜你喜欢

Getaverse, a distant bridge to Web3

Graph-node部署及测试

哪个期货公司手续费低高交返?

期货公司的评级和查询详情

SSTI 模板注入

If you encounter oom online, how to solve it?

kettle的文件名通配规则

Choose futures companies with state-owned enterprise background to open accounts

选择正规的资质好的期货公司开户

How does gamefi break the circle? Aquanee shows its style by real "p2e"
随机推荐
「PHP基础知识」PHP中的注释
未来刷脸支付是能够占据市场很多的份额
Read and understand the advantages of the LAAS scheme of elephant swap
选择正规的资质好的期货公司开户
难道Redis真的变慢了吗?
Prototype and prototype chain in JS
How can I get the lowest handling charge for opening a futures account?
哪个期货公司手续费低高交返?
Which futures company do you go to and how do you open an account?
期货开户要和客户经理详谈政策
Getaverse,走向Web3的远方桥梁
我的大四
User page management
What are the traversal methods of arrays in JS
How to apply for the return of futures account opening company?
How to use for..Of to traverse objects in JS
市场冷空气来袭,SeekTiger如何逆流而上?
Seektiger's okaleido has a big move. Will the STI of ecological pass break out?
How to open a general commodity futures account
GameFi如何破圈,AQUANEE靠真正“P2E”展现风采