After the interview
For a long time, I thought that cache penetration and cache penetration are the same thing, until recently I went to Tencent for an interview, the interviewer asked me the difference between cache penetration and cache penetration; When I replied that they were the same, the interviewer immediately looked up at me with his long, narrow, single-lidded eyes and said, “Are you sure?” In the end, the interview reminded me that since they have different names, they must be different, which means cache penetration and cache penetration are not the same thing.
So today we’re going to look at the difference between these two things, and the consequences of them;
Add a cache to your project
In general, we put hot data in the cache, such as commonly used dictionaries, user information, order details, and so on. That is to say, when the project is started, the hot data will be loaded into Redis first, after the need for data will not have to go to the database every time, so as to reduce the pressure of the database, but also improve the speed of access, it can be said that it is multiple birds with one stone!
The cache to penetrate
Cache penetration is data that is not in the cache or the database, and the user keeps making requests, such as for data with id “-1” or data with an ID that is particularly large and does not exist. In this case, the user is likely to be the attacker, and the attack will cause the database to be overburdened.
Solution:
- Add verification at the interface layer, such as user authentication verification, id basic verification, ID <=0 direct interception;
- If the data cannot be cached or retrieved from the database, you can write the key-value pair to key-null and set the cache validity period to shorter, for example, 30 seconds. (If the value is too long, the cache cannot be used in normal cases.) This prevents the attacking user from using the same ID repeatedly for violent attacks
Cache breakdown
Cache breakdown refers to a large number of keys expire at the same time, but a large number of requests need to use these expired keys, so the program cannot find data in Redis, it will go to the database to query, the database processing a large number of requests at the same time, resulting in a sudden increase in pressure, resulting in excessive pressure, and even crash. The solution
- Set the key value to never expire
- Set the expiration time of the key to random
- By adding a mutex lock, when multiple keys expire, only one query request is sent to the database at the same time, and other keys are checked one by one in turn, which can avoid the problem of excessive database pressure. The code is as follows:
static Lock lock = new ReentrantLock();
public String getData(String key ) throws InterruptedException {
try {
// Get the value from redis
String data = getRedisData(key);
// If the key does not exist, query it from the database
if(null == data){
// Try to get the lock
if(! lock.tryLock()){// Failed to obtain the lock,100ms later retry
TimeUnit.MILLISECONDS.sleep(100);
data = getData(key);
}
// The lock was successfully acquired
// Get the lock from myqsl
data = getMysqlData(key);
// Update the data to redis
setDataToRedis(key,value);
}
return data;
} catch (Exception e){
e.printStackTrace();
throw e;
} finally {
/ / unlocklock.unlock(); }}Copy the code
The difference between penetration and penetration
About the difference between penetration and penetration has been introduced very clearly above, here is a summary
- Penetration: Requests a large amount of data that is not available in the cache or database, and queries the database every time, resulting in excessive database pressure
- Breakdown: A large number of keys expire at the same time, causing all requests to reach the database and causing the database to become overloaded
Avalanche effect
Avalanche effect refers to the excessive pressure caused by penetration and breakdown of the database, and finally the whole database down. Once the database crashes, the chain reaction is terrible. Your server can not be used even if the database is not available. This is the avalanche effect;