What
Consistency means that the data in the database and the cache are the same. If the two are different, then there is a consistency problem.
Consistency optimization refers to how to eliminate consistency problems, or how to find and solve consistency problems.
Why
If the database and the cache are inconsistent, it will cause a bug in the system, especially some key data, such as balance. Therefore, in some scenes where inconsistencies cannot be tolerated, the problem of inconsistencies must be eliminated.
Why are there data inconsistencies?
-
Database cache inconsistency caused by inconsistent database master and slave.
-
The database cache inconsistency caused by the failure to eliminate the cache.
Refer to the chapter Drawback in " Cache Design-Internet Best Practice Cache Aside Pattern "
How
There are three solutions for database cache inconsistency caused by inconsistent master and slave:
Option 1: Selective read master: Refer to the chapter Selective read master in " Database Design-Master-Slave Consistency "
Option 2: Two-step elimination method (asynchronous, service)
- Asynchronous methods such as making a small tool (such as DTS, Canal) can subscribe and analyze binlog, and asynchronously eliminate the cache again. The worst case is that the duration of the dirty data in the cache is equal to the time of the master-slave synchronization delay
- The service can also be eliminated for the second time. In the first step, colleagues of del caching can start a timer to trigger the elimination of the cache again after 1 second.
Solution 3: To allow cache miss scenarios, set a timeout period and have a chance to correct it (finally consistent)
Refer
- https://www.jianshu.com/p/3613d55fb843