Cache and database double write consistency | database

Time:2021-10-21

preface

As we all know, when optimizing and improving the performance of the project, we introduced a concept of caching, that is, a technology for caching data, a component that will be introduced in the initial architecture planning of the project. Using cache has many advantages: speeding up the response speed of requests, reducing database interaction and wasting a lot of timeIOOperation, etc. However, using cache in some scenarios may also cause avalanches, penetration, data inconsistency and other problems. We will study what data inconsistency will occur when using cache and what specific solutions will be used in which scenarios. First, we will certainly use cache.

Cache update policy

  • Update the database before updating the cache

    • Waste resources
      Every time you update the database and then update the cache, you need to applyCPUWhen the database is modified, if the data is modified more frequently and the data reading operation is less, this strategy will lead to problemsCold dataThe situation.
    • Dirty data reading
      If two operations operate on data at the same time, take a chestnut: threadAIn threadBAfter the database is successfully updated and before the cache is successfully updated, the data is read, that is, the cached old data is read.
      This strategy is more suitable for scenarios with less frequent updates, such as blog articles, basic data, personal information and so on.
  • Update the database before deleting the cache

    • Dirty data reading
      During the processing of a request, if the database update succeeds but the cache update fails, the data read by the subsequent requests are old data and dirty data. You can define the validity of the cache by the cache expiration time(recommend), or use message queuing to update the cache asynchronously again when deleting the cache fails until it succeeds. These two schemes are to minimize the reading of dirty data. Another scheme can completely solve the dirty reading of data, that is, asynchronous request serialization, one wordlockHowever, it will increase the business processing time and even cause deadlock.
      This cache operation strategy is widely used and used in a wide range of scenarios.
  • Delete the cache before updating the database

    • Dirty data reading
      It is similar to the second case, but it is more prone to dirty data reading. For example, deleting the cache fails and updating the database succeeds (general businesses may not update the database after cache modification fails), thread B reads the dirty data before thread a has successfully deleted the cache and updated the database, and the cached data is also old data. The solution is to use the cache expiration time or message queue. When the cache expires, you must pay attention to the problem of avalanche. For example, the expiration time of large quantities of data is almost concentrated at the same time point, which is easy to cause avalanche.

This work adoptsCC agreement, reprint must indicate the author and the link to this article


Value comes from technology, contribution comes from sharing notes, sharing and archivingNo matter where I am, I will reply you immediately when I see the email.