How to deal with cache and cache penetrate the avalanche problem

Analysis: These two questions, tell the truth, most small and medium traditional software business, hard to encounter this problem. If you have a large concurrent projects, there is flow around a few million. These two issues must deeply consider.
 

Cache penetration, i.e. hacking intentionally requested data does not exist in the cache, resulting in hate all requests to the database, so that database connection abnormality.
 

Solution:
 

(a) use mutex, when the cache miss, go get the lock, get a lock, go to the request database. Not be locked, then retry the sleep period

 

(ii) an asynchronous update policy, regardless of whether or not to take the key value directly returns. value value maintains a cache miss time, if the cache expires, asynchronous thread to read from a database, update the cache. Cache warming needs to be done (before the project started, to load the cache) operation.

 

(C) providing a rapidly determines whether the request is valid interception mechanism, for example, by using Bloom filters to maintain the interior of a series of valid key. Quickly determine, whether the request is carried by Key valid. If not legal, the direct return.

 

Cache avalanche that cache invalidation large area at the same time, this time again a wave of requests, requests hate the results to the database, resulting in abnormal database connection.
 

Solution:
 

(a) to cache expiration time, plus a random value, to avoid collective failure.

 

(Ii) use the mutex, but significantly decreased the certain program.

 

(Iii) double buffer. We have two cache, cache and cache A B. A cache failure time is 20 minutes, the buffer B no dead time. Do their own cache warm-up operation. Then the following segments dots

 

I A is read from the cache database with direct return

II A no data, read data directly from the B, return directly and asynchronously update start a thread.

III update threads simultaneously update the cache A cache and B.

Guess you like

Origin www.cnblogs.com/zhuyeshen/p/12635889.html