Preface
Original official account :bigsai
For cache penetration 、 Cache avalanches and cache breakdown often occur in interviews , Let's see what it is today ?
redis Cache penetration
understand
- Focus on
through
Well , That is to say, through redis Go straight through mysql, It's usually a nonexistentkey
, Query in the database asnull
. Every request falls in the database 、 And high concurrency . The database will hang up if you can't carry it .
Solution
- You can find out null Set to key Cache object of .
- Of course , It can also be based on obviously wrong key On the logical level
verification
. - meanwhile , You can also analyze user behavior , Whether it's a deliberate request or a reptile 、 The attacker . Restrict user access .
- Others, etc , For example, with a bloon filter ( Super large hashmap) Filter first .
redis Cache avalanche
understand
- An avalanche , It's something
Things are pouring in
It means , Like an avalanche . ad locum , Namely redis Cache collectiveMass collective failure
, In the case of high concurrency key A large-scale visit mysql, Make the database crash . Imagine the countryAging population
. Later that day people concentrated on 70-80 year , No one's working . The state's labor force creates pressure .
Solution
- The usual solution is to key After the expiration date of, add a
random number
, Give Way key Uniform failure . - Consider using queues or locks to keep programs within pressure limits , Of course, this scheme may affect the concurrency .
- Hot data can be considered not to be invalid
redis Cache breakdown
understand
Cache breakdown , It means a key Very hot , Constantly carrying big concurrency , Large concurrent centralized access to this point , When this key At the moment of failure , Continuous large concurrency breaks through the cache , Direct request database , It's like brute force .
- Breakdown and penetration are different , Penetration means thinking
Bypass
redis To make the database crash . And breakdown can be understood asThe front is just
breakdown , This is usually a large number of concurrent pairs of key Large scale read and write operations . This key Request a large number of databases during cache failure , Too much pressure on the database makes it crash . Justsuch as
In the second kill scene 10000 The dollar mac and 100 The block mac This 100 The order for block will be rushed to the top , Constant requests ( Of course, the specific seckill has its own way of handling. Here is just an example ). So cache breakdown is for a common key A large number of requests caused the database to crash .
Solution
- Mutexes can be used to avoid a large number of requests falling at the same time db.
- The bloon filter , Determine whether a container is in the collection
- You can set the cache to never expire ( Suitable for some cases )
- Do a good job in fusing 、 Downgrade , Prevent system crash .
Original official account :bigsai