Distributed cache database consistency problems

Cache and database consistency problems, there are many solutions, no perfect solutions, only the best possible solutions for their business.

And cache concurrency due to its high performance characteristics, has been widely used in the project.

  Usually the first query query cache, if the cache hit , then return the data directly.

  If there is no cache data (such as failure , or did not set up data), then start the application database query data, if not in the cache is empty, the data.

So when the update, how to deal with cache and database? After updating the cache to update the database? After updating the cache to update the database? After the first out of the cache, or update the database?

Why do not you update the cache to update the database?

  1): If you update the database fails, then the resulting data inconsistencies

 

First update problem update the cache of the database :

  1): two threads concurrently update the database and then update the cache update the cache order problems that may arise

  2): If you update frequently, less reading of the situation, then the cache is also updated frequently, resulting in unnecessary overhead

  3): If the value of the cache is to go through a series of complex calculations, so every time to update the cache is undoubtedly the performance of waste

 

Update the database after the problem first delete the cache:

  1): A thread is finished before deleting cache update database, thread B does not hit the cache, the query from the database to the value stored in the cache before update

  Solution: Delay double deletion strategy (recommended)

  That is, first delete the cache, and then update the database, sleep for some time, and then delete the cache

  Pseudo-code as follows:

public void write(String key,Object data){
    redis.delKey(key); 
    db.updateData(data); 
    Thread.sleep(1000); redis.delKey(key);
 }

Why you want to sleep for one second? To these dirty data deleted due within one second, there may be a thread to read the old data before the update has not yet had time to write caching

How to determine how much time to sleep? Time-consuming project to assess its own business logic to read data, on this basis can be a plus 100ms. It ensures that dirty data has been written to the cache

Separate read and write how to do?

  Delay also uses double deletion strategy, sleeping time to ensure the completion of master-slave synchronization

In order to avoid dormancy resulting in reduced throughput, the second can be deleted as asynchronous operation

The second deletion failed how to do?

  Write cache delete the old value during the update failed

  Solution: The need to remove the key messages sent to the queue, and then consume their own messages, access key to be deleted, continue to retry the delete operation until it succeeds.

After the first update the database to delete the cache

  I.e. Cache Aside Pattern, i.e. cache bypass mode

  Failure: Applications to take existing cache data, not to take, fetch data from the database, after the success of its cache

  Hit: application channel data from the cache, and then return

  Update: put data into the database, after the success, and then delete the cache

Guess you like

Origin www.cnblogs.com/yangyongjie/p/11094437.html