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.
Solution:
1. Distributed lock: if only one thread can obtain the lock, first determine whether the cache exists. If not, obtain data from DB and update the cache; if there is cache, block other processes; after updating all caches, obtain the cache and release it.
2. Data preheating: Cache all hotspot data well in advance without waiting for user request. If there is a large amount of data, it will be handed over to the background for operation and maintenance; if the data is not large, it will be automatically loaded.
3. Cache degradation: When cache A fails, backup B is requested. The expiration period of CACHE A is short, and the expiration period of cache B is long.
4. Off-peak cache update: The update cache is staggered with user request peak, and the update cache expiration time policy is specified.
5. Set different expiration times: Make the cache time even, so that the cache data of many users will be saved.
6. Set the hotspot data to never expire.
7. If the cache is distributed, evenly distribute hotspot data among different cache databases.