Redis cache avalanche, cache penetration and cache breakdown concepts and solutions

1. Cache Avalanche

Concept : Cache avalanche means that during a certain period of time, the cache fails centrally, causing all requests to go to the database, which may destroy the database and paralyze the entire service.
Reasons for centrally invalidating the cache:
1. The redis server is down.
2. The same expiration time is set for the cached data, resulting in centralized cache failure within a certain period of time.
Solution :
1. For reason 1, high availability of redis can be achieved, such as Redis Cluster or Redis Sentinel (sentinel).
2. For reason 2, add a random value when setting the cache expiration time to prevent the cache from expiring at the same time.
3. Use the double cache strategy to set up two caches, the original cache and the backup cache. When the original cache fails, the backup cache is accessed, and the backup cache invalidation time is set longer.

2. Cache penetration

Concept : Cache penetration means to query a data that must not exist. Since the cache is not obtained, it is not written into the cache. As a result, the non-existent data needs to be queried in the database every time, which loses the meaning of caching. A large amount of requested data is not cached, leading to the database, which may destroy the database and paralyze the entire service.
For example, in the article table, generally our primary key IDs are unsigned self-incrementing types. Some people want to destroy your database and use negative IDs for each request, and there are no records with negative IDs in the database.
Solution: 1. To directly filter
out illegal requests with negative IDs , a Bloom filter is used . 2. For those records that cannot be found in the database, we still store the empty data in the cache, set the value corresponding to this key to a default value, such as "NULL", and set a cache expiration time, of course Generally, a short expiration time is set. At this time, all accesses through this key are blocked by the cache before the cache expires. Later, if the data corresponding to this key exists in the DB, after the cache expires, you can access the data through this key to get a new value.

3. Cache breakdown

Concept : Cache breakdown means that the cache of a certain key is very popular and has been accessed with high concurrency. If the cache fails, the database will be taken away at the same time and the database will be overwhelmed.
The difference between cache breakdown and cache avalanche is that this is for a popular key cache, while avalanche is for the centralized failure of a large number of caches.
Solution:
1. Make the cache of the popular key never expire.
2. Use a mutex and implement a mutex through setnx of redis.

Guess you like

Origin blog.csdn.net/t707584896/article/details/128812799