当前位置:网站首页>Consistency between database and cache data

Consistency between database and cache data

2022-06-21 06:50:00 y_ bccl27

For this cache and database data consistency problem , There is a classic solution , Namely Cache Aside Pattern.

Cache Aside Pattern The three solutions are as follows :

1. hit : The program first reads data from the cache , If hit , Then return directly

2. invalid : The program first reads data from the cache , If you don't hit , Read from the database , After success, put the data into the cache

3. to update : The program first updates the database , Delete the cache

Update involves the consistency between cache and database , There is 4 It's possible :

1. Update cache first , Update the database

If the cache update succeeds , And update the database . But when updating the database, the update failed , At this time, the database data will be rolled back , The cache also needs to be rolled back , Only manual programming can delete the cache , This is a troublesome thing , Therefore, this strategy is unreasonable in the sequence of execution .

2. Update the database first , Update the cache again

Threads A The updated database will key Set to 1, And then this time CPU Resources are B The thread grabbed ,B The thread updates the database and will key Set to 2 And put it in the cache key Also updated to 2, Then the thread B performed . next , Threads A Got this CPU resources , take key be equal to 1 Updated to cache , This is very likely to happen . Cache the latest data , Should be key be equal to 2, however

原网站

版权声明
本文为[y_ bccl27]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206210639515524.html