concept

The cache to penetrate

It refers to the data that is not in the cache or the database, and the user who continuously sends requests is likely to be an attacker. Attacks may cause excessive pressure on the database.

Cache breakdown

When the cache expires at a certain point in time, a large number of concurrent requests for the Key come in, and these requests find that the cache is out of date and usually load data from the back-end DB back to the cache, the large number of concurrent requests can overwhelm the back-end DB instantly.

Cache avalanche

When the cache server restarts or a large number of caches fail in a certain period of time, the failure will also put a lot of pressure on the back-end system (such as DB).

The solution

Cache penetration solution

The most common is to use a Bloom filter, hash all possible data into a large enough bitmap, a non-existent data will be intercepted by the bitmap, thus avoiding the query pressure on the underlying storage system.

Cache breakdown solution

1. Use mutex keys

A common practice in the industry is to use Mutex. In simple terms, when the cache fails, instead of loading the db immediately, the cache tool will first set a mutex key with the return value of the successful operation (such as Redis SETNX or Memcache ADD). When the return value of the successful operation is returned, Load DB and set the cache; Otherwise, retry the entire GET cache method.

  1. Use a mutex key ahead of time:

Set a timeout value (timeout1) inside the value, which is smaller than the actual memcache timeout(timeout2). When timeout1 is read from the cache and found to be out of date, extend timeout1 and reset it to the cache. The data is then loaded from the database and set to the cache.

  1. Never expire

Cache Avalanche solution

A simple solution is to spread out the cache expiration time. For example, we can add a random value on the basis of the original expiration time, such as 1-5 minutes random, so that the repetition rate of each cache expiration time will be reduced, and it is difficult to trigger the collective failure event.

2. The cache flag records whether the cache data has expired. If it has expired, another thread will be notified to update the cache of the actual key in the background. Example: Mark cache time 30 minutes, data cache set to 60 minutes. This way, when the cache key expires, the actual cache can still return the old data to the caller until another thread completes the update in the background.

conclusion

For business systems, it is always a case by case analysis, there is no best, only the most appropriate.