当前位置:网站首页>Redis -- cache avalanche, cache penetration, cache breakdown
Redis -- cache avalanche, cache penetration, cache breakdown
2022-07-27 01:03:00 【hayhead】
Usually, in order to ensure the consistency between the data in the cache and the data in the database , Will give Redis Set expiration time for data in , When the cache data is out of date , If the data accessed by the user is not in the cache , The business system needs to regenerate the cache , So you access the database , And update the data to Redis in , In this way, subsequent requests can hit the cache directly .
Cache avalanche
If at the same time A large number of cached data expire at the same time ( invalid ) or redis The service outage , So all requests access the database directly , This leads to a surge of pressure on the database , Serious will cause database downtime .
There are two reasons for cache avalanches
- A large number of cached data expire at the same time ( invalid )
- redis The service outage
A large number of cached data expire at the same time ( invalid ) Solutions for
1、 Randomly set expiration time (TTL)
2、 The mutex
When a business thread is processing a user request , If you find that the accessed data is not in Redis in , Just add a mutex , Ensure that there is only one request to build the cache at the same time ( Reading data from a database , Then update the data to Redis in ), When the cache is built , Release the lock again . Failed to get request for mutex , Or wait for the lock to be released and reread the cache , Either return a null value or a default value .
When implementing mutex , It's best to set the timeout , Or the first request gets the lock , And then something unexpected happened to the request and it was blocking , Never release the lock , At this time, other requests have been unable to get the lock , There will be no response in the whole system .
3、 double key Strategy
We can use two key, One is the Lord key, The expiration time will be set , One is preparation key, No expiration will be set , They're just key Dissimilarity , however value The value is the same , It's equivalent to making a copy of the cached data .
When the business thread cannot access 「 Lord key 」 When caching data for , Go straight back 「 To prepare key 」 Cache data , And then when you update the cache , Simultaneous updating 「 Lord key 」 and 「 To prepare key 」 The data of .
4、 Multi level cache
jvm Local cache ,nginx Cache, etc
Redis The solution of cache avalanche caused by failure and downtime
Service breakdown or request current limiting mechanism (sentinel);
structure Redis Cache highly available clusters ( Master-slave cluster );
Cache breakdown
Cache breakdown is also known as ( hotspot key problem ), One being High concurrent access And Cache business reconstruction is difficult Of key Suddenly expired ( invalid ) 了 , So all requests access the database directly , As a result, the pressure on the database increases sharply and even goes down .
Such as :tb Seckill activity ,wb Hot list, etc

Solution
1、 The mutex

Mutexes ensure that only one business thread updates the cache at the same time , Failed to get request for mutex , Or wait for the lock to be released and reread the cache , Either return a null value or a default value . Finally, add expiration time to the lock
redis command
SETNX KEY_NAME VALUE
Use setIfAbsent,boot2.1 Version above is supported
stringRedisTemplate.opsForValue().setIfAbsent(key, "1",10, TimeUnit.SECONDS);
Use lua Script
private Boolean tryGetLock1(String key){
/** redisUtil.setIfAbsent Newly added with timeout setIfAbsent Script */
//KEYS[1] It is used to indicate that in redis Parameter placeholders used as key values in ,
// Mainly used to pass on redis Used as keys Parameter of value
// ARGV[1] It is used to indicate that in redis Placeholders used as parameters in ,
// Mainly used to pass on redis Medium use as value Parameter of value .
String newSetIfAbsentScriptStr = " if 1 == redis.call('setnx',KEYS[1],ARGV[1]) then" +
" return 1;" +
" else" +
" return 0;" +
" end;";
// establish redis Script object
RedisScript<Boolean> newSetIfAbsentScript = new DefaultRedisScript<>(newSetIfAbsentScriptStr,Boolean.class);
List<String> keys = new ArrayList<>();
keys.add(key); // key
Object[] values = {
"1"}; // value
// Execute the script
Boolean res = stringRedisTemplate.execute(newSetIfAbsentScript, keys, values);
System.out.println("result:"+res);
return res;
}
2、 Logical expiration

Logical expiration refers to not setting expiration time for hot data , The cache is updated asynchronously by the background , Or before hot data is ready to expire , Notify the background thread to update the cache and reset the expiration time in advance
Mutex and logical expiration 
Cache penetration
Cache penetration means that the data requested by the client does not exist in the cache or database , So all requests access the database directly , As a result, the pressure on the database increases sharply and even goes down .
There is reason : A malicious attack , Intentionally access a large number of services that read nonexistent data
Solution
1、 Request to check
2、 Caching empty objects
3、 The bloon filter
The characteristics of the verification results of the bloom filter are :
- If the filter determines that an element exists , Then this element does not necessarily exist
- If the filter determines that an element does not exist , Then this element must not exist
Bloom filter :https://blog.csdn.net/cssweb_sh/article/details/124284785
边栏推荐
- MYSQL 使用及实现排名函数RANK、DENSE_RANK和ROW_NUMBER
- [HITCON 2017]SSRFme
- Channel shutdown: channel error; protocol method: #method<channel.close>(reply-code=406, reply-text=
- 基于Flink实时项目:用户行为分析(一:实时热门商品统计)
- Checked status in El checkbox 2021-08-02
- Canal installation
- [HFCTF2020]EasyLogin
- Flink based real-time project: user behavior analysis (I: real-time popular product statistics)
- 分区的使用及案例
- One of the Flink requirements - sideoutput (Application of side output flow: output the temperature higher than 30 ℃ to the mainstream, and output the temperature lower than 30 ℃ to the side flow)
猜你喜欢
随机推荐
Flink based real-time project: user behavior analysis (I: real-time popular product statistics)
Spark source code learning - Data Serialization
golang实现AES有五种加密模式函数,Encrypt加解密字符串输出
flink需求之—SideOutPut(侧输出流的应用:将温度大于30℃的输出到主流,低于30℃的输出到侧流)
[ciscn2019 North China division Day1 web2]ikun
Flink中的状态管理
Spark数据倾斜解决办法
[NPUCTF2020]ezinclude
Write the changed data in MySQL to Kafka through flinkcdc (datastream mode)
10 - CentOS 7 上部署MySql
Flink Interval Join源码理解
Canal 安装
Status management in Flink
Scala pattern matching
MySQL Part 2
MySQL索引优化:索引失效以及不适合建立索引的场景
Ah ah ah ah ah a
2022.7.14DAY604
Spark On YARN的作业提交流程
Doris或StarRocks Jmeter压测






![[SQL注入] 报错注入](/img/89/4809d427e307574cf73af668be3698.png)

![[CISCN2019 总决赛 Day2 Web1]Easyweb](/img/36/1ca4b6cae4e0dda0916b511d4bcd9f.png)