[Reserved] cache and dual-write database consistency problems

Original: https://learnku.com/articles/25012

Foreword

Zhou is well known, to optimize the performance of the project to improve, we introduced the concept of a cache, cache data that is a technology, a component of the project in the early planning architecture will introduce the most. There are many benefits to using the cache: to speed up the response time of the request, reducing waste a lot of interaction with the IO operation of the database, etc., but the use of the cache also in some scenarios may cause an avalanche, penetration, data inconsistencies and other issues, the use of our research What will cause the cache inconsistency of data occurs and what specific scene in which solutions will be used, first of all we inevitably will still use the cache.

Cache Update Policy

To update the database and then update the cache

  • Waste of resources
    situation each time to update the database and then update the application CPU cache is required to modify the database, but if more frequent and modify the data read operation of data but less time, this strategy will lead to Cold data .

  • Dirty read data
    if the two operations simultaneously operate on the data, give chestnuts: A thread after thread B successfully update the database, updating the cache before successfully read the data, which is read into the cache of old data.
    Under less frequently than the scene more suitable strategy update, such as the blog article, basic data, personal information and other scenes.

To update the database and then delete the cache

  • Data dirty read
    a request processing process, if the database update was successful but the cache update failed, then the data requested to be read back all the old data is dirty data. Can define the time period cached by the cache expires (recommended), or use the message queue failed to delete the cache when asynchronous update the cache again, until the success of these two schemes are reading dirty data reduction scheme as much as possible, there is a kinds of programs can be completely solved dirty read data, asynchronous request is serialized, a word lock, but will increase the time business processes or even a deadlock situation may occur.
    This strategy uses the case of cache operations are used more, the scene is also more widely used.

Delete the cache and then update the database

  • Dirty read data
    and the second case is similar to, but more prone to dirty read data, such as: delete the cache failed to update the database successfully (general business may be modified in the cache after the failure no longer update the database), thread B reads a thread has been successfully removed after a cache reads the dirty data before updating the database and can also lead to the cached data is old data. Solution or the use of cache expiration or message queue, when the cache expires sure to pay attention to the problem of avalanches, high-volume data such as time expired almost focused on the same point in time, likely to cause an avalanche.

Guess you like

Origin www.cnblogs.com/tandi19960505/p/11756586.html