当前位置:网站首页>Redis learning 5 - high concurrency distributed lock practice
Redis learning 5 - high concurrency distributed lock practice
2022-04-23 11:28:00 【virtuousOne】
I'm learning Redis When using distributed locks , We are bound to encounter some problems in actual development . Some records were made
One : Cache penetration
Cache penetration refers to querying a data that doesn't exist at all , Neither the cache tier nor the storage tier will hit , Usually for the sake of fault tolerance , If the data cannot be found from the storage layer, it will not be written to the cache layer .
Cache penetration will cause non-existent data to be queried in the storage layer every time it is requested , Lost the significance of cache protection back-end storage .
The solution is as follows : Cache an empty object
public String getCacheThrough(String key){
String cacheValue = (String) redisTemplate.opsForValue().get(key);
if(null == cacheValue){
// Get... From the database
String dbValue = getDbValue(key);
redisTemplate.opsForValue().set(key,dbValue);
// If there's no database , Just set up a key The expiration date is 300 Of a second
if(dbValue == null){
redisTemplate.expire(key,60 * 5, TimeUnit.SECONDS);
}
return dbValue;
}
return cacheValue;
}
Two : Cache invalidation
Because a large number of caches fail at the same time, a large number of requests may penetrate the cache and reach the database at the same time , It may cause the database to be under too much pressure and even hang up , In this case, we'd better set the cache time of this batch of data to different times in a time period when adding in batch .
public String getCacheBreakDown(String key){
// Get data from cache summary
String cacheValue = (String) redisTemplate.opsForValue().get(key);
if(null == cacheValue){
// Get... From storage
String dbValue = getDbValue(key);
int expireTime = new Random().nextInt(300)+300;
// Set a random expiration time
redisTemplate.opsForValue().set(key,dbValue,expireTime,TimeUnit.SECONDS);
return dbValue;
}
return cacheValue;
}
3、 ... and : hotspot Redis key The reconstruction
- At present key It's a hot spot key( For example, a popular entertainment news ), The amount of concurrency is very large .
- Rebuilding the cache cannot be done in a short time , It could be a complex calculation , For example, complicated SQL、 many times IO、 Multiple dependencies, etc .
In the moment of cache failure , There are a lot of threads to rebuild the cache , Cause the back-end load to increase , It might even crash the app .
To solve this problem is to avoid a large number of threads rebuilding cache at the same time .
public String getCacheHot(String key){
String cacheVale =(String) redisTemplate.opsForValue().get(key);
// If value by null , Start refactoring the cache
if(null == cacheVale){
// Add distributed lock to solve the problem of concurrent reconstruction of hotspot cache
String muteNxKey = "mutex:key:"+key;
RLock hotCreateCacheLock = redisson.getLock(muteNxKey);
hotCreateCacheLock.lock();
try{
String dbValue = getDbValue(key);
redisTemplate.opsForValue().set(key,dbValue,5*60,TimeUnit.SECONDS);
return dbValue;
}finally {
hotCreateCacheLock.unlock();
}
}
return cacheVale;
}
Four : Cache and database write inconsistency
This is a common situation , When we insert data into a process, it has not been processed yet , Another thread reads the data into the cache .
- Write about : Two threads write
- Reading and writing : A thread reads , A thread writes
1. Use distributed locks to solve
public String updateCacheDoubleWrite1(String key){
RLock updateValeLock = redisson.getLock(key);
updateValeLock.lock();
String dbVale = null;
try{
dbVale = updateValue(key);
redisTemplate.opsForValue().set(key,dbVale,getTimeout(),TimeUnit.SECONDS);
}finally {
updateValeLock.unlock();
}
return dbVale;
}
2. Use Redis The read-write lock is solved , The bottom layer is Lua Script
public String updateCacheDoubleWrite2(String key){
RReadWriteLock updateWriteLock = redisson.getReadWriteLock(key);
RLock write =updateWriteLock.writeLock();
write.lock();
String dbVale = null;
try {
dbVale = updateValue(key);
redisTemplate.opsForValue().set(key,dbVale,getTimeout(),TimeUnit.SECONDS);
}finally {
write.unlock();
}
return dbVale;
}
5、 ... and : Cache avalanche
Cache avalanche refers to cache failure or downtime , All traffic flows to the back-end database , Cause the whole system to hang up .
Prevent and solve buffer avalanche problem , Consider from the following aspects ;
- Ensure high availability of cache layer services , For example, use Redis Sentinel or Redis Cluster.
- The dependent isolation component fuses and demotes the back end current limiter . For example, use Sentinel or Hystrix Current limiting degraded components . Carry out relevant degradation of services .
- Before the project goes online , After the walkthrough cache layer goes down , Application and backend load and possible problems , On this basis On this basis, make some Plan Settings .
版权声明
本文为[virtuousOne]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231123242315.html
边栏推荐
- oh-my-lotto
- On lambda powertools typescript
- MIT: label every pixel in the world with unsupervised! Humans: no more 800 hours for an hour of video
- Tensorflow使用keras创建神经网络的方法
- 少儿编程结构的改变之路
- docker MySQL主从备份
- MQ的了解
- redis优化系列(二)Redis主从原理、主从常用配置
- Interpreting the art created by robots
- Nacos Foundation (6): Nacos configuration management model
猜你喜欢
MQ的了解
Overall plan management mode in maker Education
得物技术网络优化-CDN资源请求优化实践
少儿编程结构的改变之路
nacos基础(5):nacos配置入门
Summary of the relationship among GPU, CUDA and cudnn
分享两个实用的shell脚本
qt5. 8. You want to use SQLite in the 64 bit static library, but the static library has no method to compile the supporting library
Understanding of MQ
升级cpolar内网穿透能获得的功能
随机推荐
MQ is easy to use in laravel
How to count fixed assets and how to generate an asset count report with one click
Understanding of MQ
When the activity is in progress! Click the link to join the live studio to participate in "can AI really save energy?" Let's have a discussion!
Canvas详解
MySQL数据库10秒内插入百万条数据的实现
nacos基础(5):nacos配置入门
On the integration of steam education in early childhood education
qt5.8 64 位静态库中想使用sqlite但静态库没有编译支持库的方法
Tensorflow使用keras创建神经网络的方法
@Valid, @ validated learning notes
实践数据湖iceberg 第三十课 mysql->iceberg,不同客户端有时区问题
R-Drop:更强大的Dropout正则方法
On lambda powertools typescript
ImportError: libX11.so.6: cannot open shared object file: No such file or directory
Laravel绑定钉钉群警报(php)
Redis optimization series (II) redis master-slave principle and master-slave common configuration
PDMS软光刻加工过程
Summary of the relationship among GPU, CUDA and cudnn
用curl库压缩成发送字符串为utf8并用curl库发送