It is often seen that people ask how to solve the problem of database and cache consistency , I don't think we should solve this problem .

If you don't believe me, let's look at my list

 

hypothesis

Both the database and the cache are 1 element .

Double write cache when users update database .

 

1. Double writing without deletion
Write library recharge 10 element >>> Return success
-----
time interval
<<< Read cache Back to old 1 element   This is wrong
time interval
-----
Write cache recharge 10 element >>>
<<< Read cache Return to new 10 element   That's right

2. Double write first delete cache succeeded  【 Ideal situation 】
<<< Read cache old 1 element   That's right Because I haven't written yet
Write library recharge 10 element >>>
-----
time interval
<<< Read cache no data Read database 10 element   Cached
time interval
-----
Write cache >>> write 10 element Actually repeat  

3. Double write first delete cache failed  【 Abnormal non compensation 】 
Does it need to be compensated for the first time ?
Write library recharge 10 element >>>
-----
time interval
<<< Read cache old 1 element   This is wrong
time interval
-----
Write cache >>> write 10 element Failed again     Do you need compensation here ?
<<< Read cache old 1 element   This is wrong

4. Double write first delete cache failed  【 Abnormal compensation 】 
The first compensation is not completed
Write library recharge 10 element >>>
-----
time interval
<<< Read cache old 1 element   This is wrong
time interval
The first compensation is completed
<<< Read cache no data Read database 10 element   Cached
-----

In fact, compensation can not completely solve the problem , Because the data read by the compensation gap is wrong !

If you introduce distributed locks to solve this consistency problem , Distributed transactions wait for you to fall into CAP The mire of .
Will your system perform well ? Would not introduce new problems ?

So caching is caching , To set expiration time , The real-time requirements are relatively high, such as charging to read the database directly .
High concurrency of database requires sub database and sub table .

Technology
©2019-2020 Toolsou All rights reserved,
Bitcoin in ten years ,VDS Opportunity or fraud Don't annoy the panda with any cat !「 Kung Fu Panda 」20 It's the year of man 4 blood sort ( one ) bubble sort Random forest R Language implementation java Realize the function of grabbing red packets Python Basic knowledge and notes python To solve the problem of dictionary writing list in CSS architecture design Vue Common features ( one )2021 year 2 Monthly programmer salary statistics , average 15144 element