Cache penetration, breakdown, avalanche
theme: juejin
highlight: redis
I. Cache penetration
Possible cause: Users are using non-existent values to access the database maliciously. If the value does not exist in the cache, the request will continue to db, resulting in excessive pressure on database access.
Solution:
1. For user access, perform authentication and set parameters that are greater than or less than or conform to the specifications defined by the user.
2. Use bloom filter to exclude requests that cannot be cached:
Implementation principle: All potentially problematic data is hash into a large enough bitmap. If a certain non-existent data is directly intercepted by the BigMap, thus avoiding the query pressure of the underlying storage system.
Advantages:
1. Space saving: bits are very space-saving, and very long things can be changed into several bits.
2. Time saving: Judgment and operations are fast, and the hash algorithm is also fast to transform into vectors.
Cons: With Bloom, there is a loss of accuracy. Bloom’s default loss of accuracy is 3%, but this loss of accuracy is manageable.
<! --guava--> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>22.0</version>
</dependency>
Copy the code
/ * * *@ClassName : BloomFilterTest
* @Description: Use method of Bloom filter *@Author : LinZhiQ
* @Date: 2020/12/3 15:00 * /
public class BloomFilterTest {
public static void main(String[] args) {
//1%, there is a probability problem, the bigger the bloom, the more space it takes up, but the error probability is reduced
BloomFilter bloomFilter= BloomFilter.create(Funnels.stringFunnel(Charset.defaultCharset()),1000000.0.001);
bloomFilter.put("name");
// True means in bloom filter
System.out.println(bloomFilter.mightContain("name")); }}Copy the code
Two. Cache break
Cause: Cache breakdown refers to that there is no data in the cache but there is data in the database (generally, the cache time expires). At this time, because there are too many concurrent users who read the cache but fail to read the data and go to the database to fetch the data at the same time, the database pressure increases suddenly, causing excessive pressure.
Solution:
1. Set the hotspot cache to never expire.
2. Add mutex to solve cache penetration.
/ * * *@ClassName : RedisLockTest
* @Description: mutex *@Author : LinZhiQ
* @Date : 2020/12/3 15:30
*/
@Service
public class RedisLockTest implements RedisLockTestInt{
private Lock lock = new ReentrantLock();
@Autowired
private RedisTemplateUtils redisTemplateUtils;
public String redisLockTest(String key) {
// Read data from the cache
Object o = redisTemplateUtils.get(key);
String result = "";
// There is no data in cache
if (o == null) {
//String str = String.valueOf(o);
// To obtain the lock, when the lock is successful, go to the database query data
if (lock.tryLock()) {
try {
System.out.println("Accessing the database... Check the" + "result");
result = "result";
redisTemplateUtils.set(key, result);
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock(); / / releases the lock}}else {
try {
/ / pause 200 ms
Thread.sleep(200);
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock(); / / releases the lock
}
redisLockTest(key); // Get the data again}}return result; // Return the final result}}Copy the code
Cache avalanche
Cause: Cache avalanche refers to that a large amount of data in the cache reaches the expiration time, but a large amount of query data causes the database to be overburdened or even down.