preface

In a distributed system, when both the cache and the database exist, do writes come first to the database or to the cache? This article will be divided into five schemes for comparison, thank you for reading ~

Github address, heartfelt thanks to every star

Github.com/whx123/Java…

Cache maintenance Solution 1

If it is A read (thread B) write (thread A) operation, “operation cache first, operation database second”. The flow chart is as follows:

  • 1. Thread A initiates A write operation, and the first step is del cache

  • 2. Thread A writes new data to DB in the second step

  • 3. Thread B initiates a read operation and the cache Miss is invalid.

  • 4. Thread B obtains the latest data from the DB

  • 5. Thread B performs set cache to update the data read from the DB to the cache.

“Look at it this way, there’s no problem.” Let’s look at the second flow chart, as follows:

  • 1. Thread A initiates A write operation, and the first step is del cache

  • 2. Thread B initiates a read operation, cache Miss

  • 3. Thread B continues to read DB data and reads old data

  • 4. The old data is cached

  • 5. Thread A writes the latest data to the DB

The old data is in the cache, “every time I read the old data, the cache and data are inconsistent with the database data”.

Cache maintenance Solution 2

The last solution was read/write. If it is a dual write operation, “cache first, then database”, what happens?

  • 1. Thread A initiates A write operation and sets the cache in the first step

  • 2. Thread A writes new data to DB in the second step

  • 3. Thread B initiates a write operation and sets the cache

  • 4. Thread B writes new data to DB in the second step

“Look at it that way, there’s no problem.” , but sometimes things don’t work out as expected. Let’s look at the second flow chart, as follows:

  • 1. Thread A initiates A write operation and sets the cache in the first step

  • 2. Thread B initiates a write operation and sets the cache in the first step

  • 3. Thread B writes data to the database

  • 4. Thread A writes data to the database

After the operation is complete, the cache stores the data after operation B and the database stores the data after operation A. “The cache data is inconsistent with the database data.”

Cache maintenance Solution 3

Write (thread A) read (thread B) operation, “database operation first, cache operation later”.

  • 1. Thread A initiates A write operation, and the first step is write DB

  • 2. The second step of thread A is del cache

  • Thread B initiates a read operation, cache miss

  • 4. Thread B obtains the latest data from the DB

  • 5. Thread B simultaneously sets the cache

Some of you might think, what if thread B reads over before deleting the cache in step 2? In this case, it is normal business logic to read old cache data, and the next time it will read the correct data.

This scheme “has no obvious concurrency problem”, but may “step 2 delete cache failure”, although the probability is relatively small, “better than scheme 1 and scheme 2”, scheme 3 is also used in daily work.

To sum up, we generally adopt plan 3, but is there a perfect way to solve all the disadvantages of Plan 3?

Cache maintenance Solution 4

This is the improvement of plan 3, which is to operate the database first and then the cache. Let’s look at the flow chart:

Through the database “binlog” to “asynchronously eliminate the key”, for example, mysql can “use Ali’s Canal to send binlog log collection to MQ queue”, and then “through the ACK mechanism to confirm processing” this update message, delete the cache, to ensure the consistency of data cache.

But then there’s the question, “What if it’s a master-slave database?”

Cache Maintenance Solution 5

Master/slave DB problem: There is a delay for the master/slave DB synchronization. After the cache is deleted, dirty data will be read from the standby database if a request is received before data is synchronized to the standby database. How do I resolve this problem? The solution flow chart is as follows:

Summary of Cache Maintenance

To sum up, in a distributed system, when cache and database exist together, if there is a write operation, “database operation first, then cache operation”. As follows:

  • 1. Check whether relevant data exists in the cache
  • 2. If value exists in the cache, the value is returned
  • 3. If there is no relevant data in the cache, read the relevant data from the database, put it in the cache, and return it
  • 4. If data is updated, update the database first and then delete the cache
  • 5. To ensure that the cache in step 4 is deleted successfully, use binlog to delete the cache asynchronously
  • 6. In the case of a master slave database, binglog is taken from the slave database
  • 7. If there is one master and multiple slaves, each slave library will collect the binlog data, and the consumer will delete the cache after receiving the last binlog data

Personal public account

A boy picking up field snails

  • Welcome to pay attention to, we study together, discuss together ha.
  • Reply to PDF for ebook learning