What is cache avalanche, cache penetration, cache breakdown, and solutions

What is cache avalanche, cache penetration, cache breakdown, and solutions

Redis is often asked in interviews, and there are many questions. Cache avalanche, cache penetration, and cache breakdown are more representative, so record them here.
Insert picture description here

Cache avalanche

Cache avalanche refers to the large amount of data in the cache until the expiration time, and the huge amount of query data causes excessive pressure on the database or even downtime.
Solution
When doing e-commerce projects, it is generally to adopt different categories of goods and cache different cycles. Add a random factor to products in the same category. In this way, the cache expiration time can be dispersed as much as possible, and the cache time of products in popular categories is longer, and the cache time of products in unpopular categories is shorter, which can also save the resources of the cache service.
Insert picture description here

Advance:

Use cluster cache to ensure high availability of cache services

This solution is to achieve high availability for the cache cluster before the avalanche occurs. If you are using Redis, you can use the master-slave + sentinel, Redis Cluster to avoid Redis crashing.

In the event:

Ehcache local cache + Hystrix current limit & downgrade to avoid MySQL being killed

The purpose of using ehcache local cache is to consider that ehcache local cache can still support a while when Redis Cluster is completely unavailable.

Use Hystrix for current limiting & downgrading. For example, if 5000 requests come in one second, we can set the assumption that only 2000 requests per second can pass through this component, then the remaining 3000 requests will go through the current limiting logic.

Then go to call the downgrade component (downgrade) we developed by ourselves, such as some default values ​​set. In order to protect the final MySQL from being killed by a large number of requests.

afterwards:

Enable Redis persistence mechanism to restore the cache cluster as soon as possible

Once restarted, the data in the memory can be automatically loaded from the disk to restore the data.

The avalanche prevention plan is shown in the figure below:
Insert picture description here

Cache penetration

Cache penetration refers to querying a data that must not exist. Because the cache is missed, it needs to be queried from the database. If no data is found, it will not be written to the cache. This will cause the non-existent data to go to the database every time a request is made. Query, and then put pressure on the database.
The solution
uses the method of caching null values. If the object queried from the database is empty, it is also put into the cache, but the set cache expiration time is shorter, for example, set to 60 seconds
Insert picture description here

Cache breakdown

Cache breakdown refers to when the hot key expires at a certain point in time, and at this point in time, there are a large number of concurrent requests for this key, and a large number of requests hit the db.
Solution:
You can set the expiration time of hotspot data to be permanently valid
Insert picture description here

Small summary

Cache avalanche is a case of a large number of key failures, cache penetration, a case where a key does not exist, cache breakdown, and it is for hot data.

reference

Example-Interpretation of Redis cache penetration, cache avalanche and cache breakdown
What is cache avalanche, cache penetration, cache breakdown and solutions
Ali: About [Cache penetration, cache breakdown, cache avalanche, hot data Invalid] The solution
to the problem The three major problems in the cache world and their solutions
Redis common interview questions

Guess you like

Origin blog.csdn.net/e891377/article/details/108811639