Definition and handling of cache penetration, cache breakdown, and cache avalanche
The cache to penetrate
- Definition: Request data that does not exist in the database, so that each request is placed on the database
- Solution: The way to handle cache penetration is to set up a special cache even if the database does not have the corresponding data, so that requests do not hit the database.
Cache breakdown
- Definition: After the cache of hot data expires, a large number of requests are sent to the database, which puts pressure on the database.
- Treatment scheme:
- Lock the cache: Use a mutex when building the cache to avoid multiple requests to the database at the same time.
- Never expire: The expiration time of hotspot data is not directly set. The expiration time is carried when the data is stored. After determining the expiration time, the program asynchronously updates the cache.
Cache avalanche
- Definition: cache fails simultaneously at a certain moment, all requests are sent to the database, and the instantaneous pressure of the database is too big an avalanche.
- Treatment scheme:
- Stagger expiration: When setting the expiration time for the cache, add a random value to the original expiration time to stagger the expiration time for the cache.