当前位置:网站首页>Data consistency between redis and database
Data consistency between redis and database
2022-07-01 05:05:00 【Cicada bathes in the wind】
official account 「 Cicadas bathe in the wind 」, You are welcome to pay attention to exchanges
Catalog
It is used as a cache when data is read more and written less , Probably Redis Use the most common scenario . When using Redis As a cache , This is the general process .
- If the cache is in Redis in , Cache hit , Then directly return the data
- If Redis There is no corresponding cache in , You need to query the database directly , Then deposit Redis, Finally, return the data to
Usually , We will set a for a cache key value , And aim at key Value to set an expiration time , If the queried data corresponds to key Out of date , Then query the database directly , And store the queried data in Redis, Then reset the expiration time , Finally, return the data to , The pseudocode is as follows :
/** * Get user details according to user name * @author official account 【 Cicadas bathe in the wind 】 */
public User getUserInfo(String userName) {
User user = redisCache.getName("user:" + userName);
if (user != null) {
return user;
}
// Search directly from the database
user = selectUserByUserName(userName);
// Write data to Redis, And set expiration time
redisCache.set("user:" + userName, user, 30000);
// Return the data
return user;
}
Consistency issues
however , stay Redis Of key If the value does not expire , The user has modified his personal information , At this time, we need to operate the database data , Also operate Redis data . Now we have two choices :
- Do it first Redis The data of , Then operate the data of the database
- First operate the data of the database , Then operate Redis The data of
As for which method to choose , Ideally , Either the two operations succeed at the same time , Or fail at the same time , Otherwise, it will appear Redis Inconsistent with the database data .
Unfortunately , At present, there is no framework to guarantee Redis Complete consistency between the data of and the data of the database . We can only take certain measures to reduce the probability of data inconsistency according to the scenario and the code we need to pay , Achieve a compromise between consistency and performance .
Now let's discuss about Redis Some schemes of data consistency between and database .
Options
Delete cache or update cache ?
When the database data changes ,Redis The data also needs to be operated accordingly , So this 「 operation 」 What is to use 「 to update 」 Or use it 「 Delete 」 Well ?
「 to update 」 Call Redis Of set Method , Replace old value with new value ;「 Delete 」 Directly delete the original cache , Re read the database the next time you query , Then update Redis.
Conclusion : Direct use is recommended 「 Delete 」 operation .
Because use 「 to update 」 operation , You will face two choices
Update cache first , Update the database- Update the database first , Update the cache again
The first 1 Don't think about it , Let's talk about it 「 Update the database first , Update the cache again 」 This program .
If the thread 1 And thread 2 Update at the same time , But the execution order of each thread is shown in the figure above , This will lead to inconsistent data , Therefore, from this point of view, we recommend deleting the cache directly .
Besides , Recommended 「 Delete cache 」 There are two other reasons .
- If there are more scenarios of writing database than reading data , Using this scheme will cause the cache to be written frequently , Waste performance ;
- If the cache needs a series of complex calculations to get , So every time you write to the database , It is also a waste of performance to calculate the written cache again .
After clarifying this problem , There are only two choices in front of us :
- Update the database first , Delete the cache
- So let's delete the cache , Update the database
Update the database first , Delete the cache
There may be two exceptions in this way
- Database update failed , At this time, you can catch exceptions through the program , Direct return , No longer delete cache , So there will be no data inconsistency
- Update database successful , Delete cache failed . Cause the database to be the latest data , What's in the cache is old data , Data inconsistency
The first 2 What should we do in this case ? We have two ways : Failure to retry and Asynchronous update .
Failure to retry
If delete cache fails , We can catch this exception , Delete the key Send to message queue . Create a consumer consumption , Try to delete this... Again key, Until the deletion is successful .
There is a drawback to this approach , First, it will invade the business code , Secondly, message queue is introduced , It increases the uncertainty of the system .
Update cache asynchronously
Because when you update the database, you go to binlog Write log in , So we can start a monitor binlog Changing services ( For example, use Ali's canal Open source components ), Then delete it on the client side key The operation of . If the deletion fails , And send it to the message queue .
summary
All in all , In case of cache deletion failure , Our approach is to constantly retry the deletion operation , Until success . Whether it's retry or asynchronous deletion , It's all about ultimate consistency .
So let's delete the cache , Update the database
There may be two exceptions in this way
- Delete cache failed , At this time, you can catch exceptions through the program , Direct return , Do not continue to update the database , So there will be no data inconsistency
- Delete cache succeeded , Database update failed . Data inconsistency may occur in multithreading
At this time ,Redis Old data stored in , The value of the database is new data , Leading to data inconsistency . Now we can use Delay double delete The strategy of , That is, after updating the database data , Delete the cache again .
In pseudocode, that's :
/** * Delay double delete * @author official account 【 Cicadas bathe in the wind 】 */
public void update(String key, Object data) {
// First delete the cache
redisCache.delKey(key);
// Update the database
db.updateData(data);
// Sleep for a while , The time depends on the time taken to read the data
Thread.sleep(500);
// Delete cache again
redisCache.delKey(key);
}
Finally, leave two questions for readers :
- Why?
Update cache first , Update the databaseIt won't work ? - Why should the method of delayed double deletion sleep for a period of time ?
Welcome to comment area .
Recommended reading
official account 「 Cicadas bathe in the wind 」, You are welcome to pay attention to exchanges
边栏推荐
- Fitness without equipment
- Global and Chinese markets for business weather forecasting 2022-2028: Research Report on technology, participants, trends, market size and share
- 智慧运维:基于 BIM 技术的可视化管理系统
- AssertionError assert I.ndim == 4 and I.shape[1] == 3
- 【暑期每日一题】洛谷 P2026 求一次函数解析式
- Pytoch (II) -- activation function, loss function and its gradient
- [daily question in summer] Luogu p7222 [rc-04] informatics competition
- Global and Chinese market for instant messaging security and compliance solutions 2022-2028: Research Report on technology, participants, trends, market size and share
- HCIP Day13
- Copier le matériel de conseils de bébé ne peut pas être vide, comment résoudre?
猜你喜欢
[data recovery in North Asia] a data recovery case of raid crash caused by hard disk drop during data synchronization of hot spare disk of RAID5 disk array
[hard ten treasures] - 1 [basic knowledge] classification of power supply
Pytoch (III) -- function optimization
Distributed architecture system splitting principles, requirements and microservice splitting steps
Neural networks - use sequential to build neural networks
C read / write application configuration file app exe. Config and display it on the interface
分布式事务-解决方案
Go learning notes (5) basic types and declarations (4)
无器械健身
Solution: drag the Xib control to the code file, and an error setvalue:forundefined key:this class is not key value coding compliant for the key is reported
随机推荐
Copy baby prompt: material cannot be empty. How to solve it?
[hardware ten treasures catalogue] - reprinted from "hardware 100000 whys" (under continuous update ~ ~)
Global and Chinese markets of Ethernet communication modules 2022-2028: Research Report on technology, participants, trends, market size and share
Distributed architecture system splitting principles, requirements and microservice splitting steps
STM32 光敏电阻传感器&两路AD采集
Global and Chinese market of paper machine systems 2022-2028: Research Report on technology, participants, trends, market size and share
LeetCode_28(实现 strStr())
Solution: thread 1:[< *> setvalue:forundefined key]: this class is not key value coding compliant for the key*
Leetcode316- remove duplicate letters - stack - greedy - string
PR 2021 quick start tutorial, learn about the and functions of the timeline panel
Solve the problem that the external chain file of Qiankun sub application cannot be obtained
复制宝贝提示材质不能为空,如何解决?
AcWing 884. Gauss elimination for solving XOR linear equations
Manually implement a simple stack
Global and Chinese market of solder wire 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese markets of gps/gnss receiver modules 2022-2028: Research Report on technology, participants, trends, market size and share
科研狗可能需要的一些工具
LeetCode_ 58 (length of last word)
Global and Chinese market of 3D CAD 2022-2028: Research Report on technology, participants, trends, market size and share
LeetCode_58(最后一个单词的长度)