当前位置:网站首页>Redis explores cache consistency
Redis explores cache consistency
2022-07-01 12:46:00 【Bronze God】
What is consistency ?
Consistency refers to requests at the same time , Whether the data in the cache is the same as the data in the database .
Strong consistency : Database update operation and cache update operation are atomic , Cache and database data are consistent at any time , This is the hardest consistency to achieve .
Weak consistency : When the data is updated , The data in the cache may be the value before update , It may also be the updated value , Because this update is asynchronous .
Final consistency : A special weak consistency , After a certain period of time , The data will reach a consistent state . Final consistency is the ideal state of weak consistency , It is also highly recommended in the data consistency solution of distributed systems .
Update strategy
Our updated strategy corresponds to 4 Kind of :
(1) Update cache first , Update the database .
(2) Update the database first , Update the cache again .
(3) So let's delete the cache , Update the database .
(4) Update the database first , Delete the cache .
Update cache or delete cache ?
Update cache
advantage : The data in the cache will always be valid , Relative delete cache , It will improve the cache hit rate .
shortcoming : Updating the cache frequently costs more , The business scenario of writing more and reading less is not applicable .
Delete cache
advantage : The logic is simple , Just delete the data in the cache .
shortcoming : Deleting the cache will cause the next request cache miss .
Considering the time cost and code difficulty of updating the cache , A more economical way to delete the cache .
Cache first or database first ?
Let's put 4 Draw all the scenes that will be encountered in each implementation
Update the cache before updating the database - Double write scene
① Threads B Update cache
② Threads A Update cache
③ Threads A Update the database
④ Threads B Update the database
In this scenario of double writing , If the update operation of another thread is overwritten between two operations of one thread , There will inevitably be consistency problems . Because if threads A Want fields +1, Threads B Also want fields +1, After independent calculation of two threads , Two threads calculate +1 Result , Then the thread A Update the cache to +1 Result , The database is inconsistent with the cache .
Update the cache before updating the database - Reading and writing scenes
① Threads B The query cache , But cache misses
② Threads B Query the database
③ Threads A Update cache
④ Threads A Update the database
⑤ Threads B Find the data , Update cache
This situation also leads to consistency problems . This consistency problem is well understood , Threads B Query request received , Go to the cache to find the results , But cache misses , Check the database . Threads A Update the cache first , And update the database . Reverse the thread B To update the cache , At this time, there are threads in the cache A Update previous results , So the data is inconsistent . But because the operation cache is faster than the operation database , So the probability of this situation is low .
Delete the cache before updating the database - Double write scene
① Threads B Delete cache
② Threads A Delete cache
③ Threads A Update the database
④ Threads B Update the database
In the double write scenario with updating the cache first , Facing the same situation , But deleting the cache first obviously won't suffer from consistency problems , Because no matter what order cache is, it will be deleted .
Delete the cache before updating the database - Reading and writing scenes
① Threads B The query cache , Not hit
② Threads B Query the database
③ Threads A Delete cache
④ Threads A Update the database
⑤ Threads B Update cache
This situation does not guarantee consistency . Threads B Query request received , Go to the cache to find the results , But cache misses , Check the database . Threads A Delete the cache first , And update the database . Reverse the thread B To update the cache , At this time, there are threads in the cache A Update previous results , So the data is inconsistent .
First update the database and then update the cache - Double write scene
① Threads B Update the database
② Threads A Update the database
③ Threads A Update cache
④ Threads B Update cache
This situation does not guarantee consistency . Threads B First update the database to get the results , Threads A Then update the database and update the cache , Threads B Updating the cache again will inevitably lead to cache inconsistency .
First update the database and then update the cache - Reading and writing scenes
① Threads B The query cache , Not hit
② Threads B Query the database
③ Threads A Update the database
④ Threads A Update cache
⑤ Threads B Update cache
Consistency cannot be guaranteed in this case . Threads B Query request received , Go to the cache to find the results , But cache misses , Check the database . Threads A Update the cache first , And update the database . Reverse the thread B To update the cache , At this time, there are threads in the cache A Update previous results , So the data is inconsistent . But because the operation cache is faster than the operation database , So the probability of this situation is low .
First update the database and then delete the cache - Double write scene
① Threads B Update the database
② Threads A Update the database
③ Threads A Delete cache
④ Threads B Delete cache
This situation can ensure consistency . Because the double write scenario of deleting the cache does not care whether you delete it first or later , Because there is no difference , So it must be consistent .
First update the database and then delete the cache - Reading and writing scenes
① Threads B The query cache , Not hit
② Threads B Query the database
③ Threads A Update the database
④ Threads A Delete cache
⑤ Threads B Update cache
Consistency cannot be guaranteed in this case . Threads B Query request received , Go to the cache to find the results , But cache misses , Check the database . Threads A Update the database first , Then delete the cache . Reverse the thread B To update the cache , At this time, there are threads in the cache A Update previous results , So the data is inconsistent . But because the operation cache is faster than the operation database , So the probability of this situation is low .
wait ~ Autumn bean sack , Why can't the four strategies guarantee consistency . What are you doing ?
Don't worry. , Let's review these four situations :
1. Update the cache before updating the database : In the double write scenario , It is easy to have consistency problems , In the reading and writing scenario , Consistency problems occur with small probability , therefore Pass.
2. Delete the cache before updating the database : In the double write scenario , There will be no consistency problems , In the reading and writing scenario , It is easy to have consistency problems , therefore Pass.
3. First update the database and then update the cache : In the double write scenario , It is easy to have consistency problems , In the reading and writing scenario , Consistency problems occur with small probability , therefore Pass.
4. First update the database and then delete the cache : In the double write scenario , There will be no consistency problems , In the reading and writing scenario , Consistency problems occur with small probability , So keep it for the time being .
Three of the four strategies have been Pass 了 , There is only one buddy left , But it still can't completely meet the consistency , So we need to add some technology .
Delay double delete
We first know the process of delayed double deletion , Let's explain the process .
Why delete the cache first ?
First solve the problem of updating the database , The query request goes to the cache , Getting outdated data .
Why does the second deletion need to be delayed ?
To avoid threads A The second delete cache operation of , Before thread B Update cache operation , Thus, there are still inconsistencies .
How to define this delay , It is appropriate to define how long ?
This can only be evaluated according to the query performance of the system , There is no universal value .
in addition , Delayed double deletion can only guarantee the final consistency , There is no guarantee of strong consistency .
边栏推荐
- Look at the sky at dawn and the clouds at dusk, and enjoy the beautiful pictures
- logstash报错:Cannot reload pipeline, because the existing pipeline is not reloadable
- 哪个券商公司开户佣金低又安全又可靠
- Tencent Li Wei: deeply cultivate "regulatory technology" to escort the steady and long-term development of the digital economy
- 手机便签应用
- A hole in solder paste
- R语言使用yardstick包的conf_mat函数计算多分类(Multiclass)模型在每个交叉验证(或者重采样)的每一折fold上的混淆矩阵、并使用summary输出每个fold的其它详细指标
- 晓看天色暮看云,美图欣赏
- 简单斐波那契(递推)
- Stack-------
猜你喜欢
shell脚本导入存储过程到数据库
Project deployment is not difficult at all!
工具箱之 IKVM.NET 项目新进展
软件测试中功能测试流程
codeforces -- 4B. Before an Exam
《MATLAB 神经网络43个案例分析》:第40章 动态神经网络时间序列预测研究——基于MATLAB的NARX实现
基因检测,如何帮助患者对抗疾病?
ROS2 Foxy depthai_ros教程
Manage nodejs with NVM (downgrade the high version to the low version)
leetcode:241. Design priority for operation expression [DFS + Eval]
随机推荐
运行Powershell脚本提示“因为在此系统上禁止运行脚本”解决办法
AI matting tool
How can genetic testing help patients fight disease?
Chapter 14 signals (IV) - examples of multi process tasks
VM虚拟机配置动态ip和静态ip访问
Wang Xing's infinite game ushers in the "ultimate" battle
redis探索之缓存一致性
下半年还有很多事要做
ASP. Net core 6 from entry to enterprise level practical development application technology summary
Run PowerShell script prompt "because running script is prohibited on this system" solution
Efforts at the turn of the decade
Powerful, easy-to-use, professional editor / notebook software suitable for programmers / software developers, comprehensive evaluation and comprehensive recommendation
be based on. NETCORE development blog project starblog - (13) add friendship link function
请问flink mysql cdc 全量读取mysql某个表数据,对原始的mysql数据库有影响吗
Tencent security and KPMG released a regulatory technology white paper to analyze the "3+3" hot application scenarios
Ikvm of toolbox Net project new progress
Based on the open source stream batch integrated data synchronization engine Chunjun data restore DDL parsing module actual combat sharing
Relationship between accuracy factor (DOP) and covariance in GPS data (reference link)
Double linked list related operations
Quickly understand what the compressed list in redis is