Reference article: Cache penetration, Cache breakdown, Cache avalanche concepts and solutions
First. Cache Breakdown
1. Concept
Cache breakdown refers to a high concurrency situation in the cache when the resource does not exist , causing the cache to miss, all requests penetrate into the backend database system to query, so that the database pressure is too large, Even the database service was crushed to death.
2. Solution
- Direct Lock: When the cache misses, the data is fetched from the database and updated into the cache;
- Timed task: Refresh the cache periodically;
- Multilevel cache: The first-level cache failure time is short, the two-level cache failure time is long, primary cache misses when the key lock, from the database to update the data to the cache and release the lock, the subsequent thread from the two-level cache to obtain data;
Second. The cache penetrates
1. Concept
Access to a nonexistent key, the cache does not work, the request will penetrate into the DB, when the traffic is large, the db will be hung off.
2. Solution
- Using the bitmap filter, a large enough to store the key that may be accessed, the nonexistent key is filtered directly;
Third. Cache Avalanche
1. Concept
A large number of keys set the same expiration time, resulting in the cache at the same time all failed, resulting in a large amount of instantaneous db requests, pressure surges, causing avalanches.
2. Solution
You can set the expiration time for the cache by adding a random value time, so that each key's expiration time is distributed, and will not fail at the same time.
Cache breakdown, Cache penetration, cache avalanche