Introduction to redis cache penetration, breakdown, and avalanche

64f2d72019bd4d10a621914b2867b89c.jpgcache penetration

 

concept

Cache penetration refers to a batch request coming in at a specific time and accessing a key that is neither in the cache nor in the database. At this time, it will directly penetrate the cache and reach the database, thereby causing the risk of instantaneous pressure doubling on the database, resulting in a decrease in response speed or even a crash;

solution

1. Solved by Bloom filter

Principle: Put all the keys that need to be cached into the Bloom filter through the hash algorithm and set the value corresponding to the corresponding subscript to 1. In this way, when the request comes in, first go to the Bloom filter and find that the key corresponding to the index is 1. Then go to the cache to get the data and return it directly if it is 0, thus avoiding the need to query the database.

 

Advantages and Disadvantages: The bottom layer of the Bloom filter is implemented through bitMap of redis, which is based on bit operations, so it is highly efficient; however, the key needs to be stored in the filter in advance, which greatly increases the upfront cost; and the mapping from key to index is done through the hash algorithm , then the problem of hash collision will inevitably occur;

 

2. Pass key-null

Principle: When a request comes in and the value returned from the database query is empty, the corresponding null value is also stored in redis, so that when the next request is made, there is already data in redis and it will not be sent to the database again.

Advantages and Disadvantages: The idea is clear and the operation is simple, but in extreme cases there will be N keys that are not in the database or cache. Then this implementation will cause too much junk data in redis and waste memory space.

3. Enhance pre-judgment

Principle: Perform permission and logic verification at the interface layer before querying, and determine the legality of the key as much as possible

Advantages and Disadvantages: The cost of use is low but it is not enough to avoid the risk of cache penetration, so it is recommended to only use it as an additional solution.

Cache breakdown

concept

Cache breakdown refers to a batch request coming in at a specific time to query a specific value and accessing a key that is not in the cache but exists in the database. At this time, it will directly penetrate the cache and reach the database, causing the database to double the instantaneous pressure and cause the response. Risk of slowdown or even crash; it should be noted that the cache mentioned here does not include two meanings. One is that it is not in the cache itself, and 20 it is in the cache but has expired (a small number of hotspot keys have expired).

solution

1. Set the expiration time of the key to be random.

Principle: When setting keys in batches like redis, try to make the expiration time random, so as to avoid cache breakdown caused by batch key expiration at a certain time.

Advantages and Disadvantages: Although it solves the breakdown problem caused by the expiration of batch keys at the same time in the cache, it does not solve the breakdown caused by the key not existing in the cache at all.

2. Solved by distributed locks

Principle: Add a distributed lock when querying the database, so that only one request can access the database within a certain period of time. After the access is successful, the queried value will be cached in redis, so that no breakdown will occur during the next access. Already;

It should be noted that for businesses that do not have massive requests, it is not a big problem to add a stand-alone lock. It is nothing more than changing only one request to the data at the same time to only the number of cluster requests to the database at the same time. The problem should not be big.

 

Advantages and Disadvantages: Whether there is no key in the cache or the key in the cache has expired, using locking can solve the problem of cache breakdown, but it increases the cost of locking.

3. Set the hotspot key to never expire

Principle: To solve the penetration problem caused by the batch expiration of hotspot keys, setting the key to never expire can solve the problem.

Advantages and Disadvantages: Like the first one, it can only solve the breakdown caused by key expiration, but cannot solve the breakdown caused by no key in the cache, and the confirmation of hotspot keys is also a science, and it is not always guaranteed that the set hotspot keys are not missed.

4. Deploy the key in different instances

Principle: For cluster-deployed redis, deploying hotspot keys separately can also avoid breakdown problems caused by expiration.

Advantages and Disadvantages: It can only be used by clusters, and it can only solve the breakdown problem caused by key expiration. It does not always guarantee that the set hotspot key will not be missed.

cache avalanche

concept

Compared with breakdown, avalanche means that there are a large number of expired or non-existent keys instead of one or a small number, resulting in a large number of requests falling into the database;

solution

1. Solve it by setting the expiration time randomly

When setting the key, add a random expiration time to minimize the number of expired keys at the same time;

2. Set the lock

The key is locked when accessing the key from the cache. When the cache does not exist, it is checked in the database. After checking, it is inserted into the cache and finally the lock is released. If it is a stand-alone application, the virtual machine lock is added directly. If deployed in a cluster, a distributed lock is required.

Guess you like

Origin blog.csdn.net/weixin_57763462/article/details/132912605
Recommended