Redis avalanche, penetration, breakdown

Normal caching flowchart: Enter picture description

1. Caching avalanche  Enter picture description reason: a large number of caches fail at the same time, resulting in a large number of requests directly requesting the database, which causes the database to hang, which affects the page to not work properly

solution:

1- Set the cache invalidation time so that it does not expire at the same time, and initialize the invalidation time randomly so that it will not be invalidated at the same time

2- Put the hot key on different nodes in the cluster

3- The most violent way is not to set the expiration time (generally not taken)

4- Set up scheduled tasks to refresh the cache expiration time from time to time

2. Cache penetration Enter picture description 

Reason: Illegal users pass in illegal data in the request to the request. If they can't find it in redis, they will go to the database to check, which causes the database to hang. Generally, they are malicious users.

solution:

1- Regardless of the result of querying the database, even if it is null, it will be cached in redis. Next time there is such a request, it can be returned directly, of course, it will be changed to a different request

2- Block this IP directly, of course it is also possible to change to a different IP

3-Check the legality of the parameter, return directly if the parameter is illegal

4- Use Bloom filter, this is a new technology

3. Cache breakdown  Enter picture description reason: a large number of requests to access a key, but suddenly the key fails, which directly accesses the database, causing the database to hang

Solution: 1- The most violent solution is to never fail. This is ok, of course, this method is generally adopted

2- Distributed applications can use distributed locks, and single applications can use mutex locks, that is, only one thread can acquire the lock to access the database. After the query is found, it is cached in redis, and subsequent requests directly access redis, thus Prevent breakdown

Guess you like

Origin blog.csdn.net/qq_27920435/article/details/109044651