Graphical Redis cache penetration, breakdown, avalanche (must know)

Table of contents

1. What is cache?

Cache is the buffer area for data exchange, where data is stored, and generally has high read and write performance.
insert image description here

2. The role and cost of caching

1. The role of cache:

  1. Reduce backend load
  2. Improve read and write efficiency and reduce response time

2. The cost of caching:

  1. data consistency cost
  2. code maintenance cost
  3. Operation and maintenance cost

3. Cache effect model

insert image description here

1. Query data cache process according to id

insert image description here

4. Cache update strategy

1. Memory elimination

Redis's memory elimination mechanism automatically eliminates some data when the memory is insufficient, and updates the cache in the next query.

2. Overtime elimination

When the TTL time is set for the cached data, the cache will be automatically deleted after the expiration, and the cache will be updated in the next query.

3. Active update

Write business logic to update the cache while modifying the database.

Five, cache penetration

Cache penetration means that the data requested by the client does not exist in Redis or the database, so caching cannot be performed, and these requests will hit the database.

Solution:

1. Cache empty objects

A cache is also created in Redis for non-existent data, the value is empty, and a shorter TTL time is set.

  • Advantages: simple implementation and convenient maintenance;
  • Disadvantages: additional memory consumption may cause short-term data inconsistency;

2. Bloom filter

Using the Bloom filtering algorithm, before the request enters Redis, it is first judged whether it exists, and if it does not exist, the access is directly denied.

  • Pros: Small memory footprint
  • Disadvantages: ① Complicated implementation; ② Possibility of misjudgment;

6. Cache Avalanche

Cache avalanche refers to the fact that a large number of cache keys fail at the same time or the Redis service is down, causing a large number of requests to hit the database, bringing enormous pressure.

Solution:

  1. Add random values ​​to the TTL of different keys;
  2. Use Redis cluster to improve service availability;
  3. Add a downgrade current limiting strategy to the cache;
  4. Add multi-level cache to the business;

Seven, cache breakdown

Cache breakdown is also called the hotspot key problem. It means that a key that is highly concurrently accessed and the cache rebuilding business is more complicated fails, and countless access requests will hit the database in an instant, bringing huge pressure.

1. Solve cache breakdown through mutex

Lock the cache reconstruction process to ensure that only one thread executes the reconstruction process while other threads wait.

The biggest problem with mutexes is that the thread waits and the performance is poor.
insert image description here

2. Query product information according to id, and solve the problem of cache breakdown based on mutex

insert image description here

3. Solve cache breakdown through logical expiration

The advantage of logical expiration is good performance , but the disadvantage is that it does not guarantee consistency , has additional memory consumption, and is complicated to implement.
insert image description here

insert image description here

8. Redis Tools

// 解决缓存穿透
Goods goods = cacheClient.queryWithPassThrough(CACHE_GOODS_KEY, id, Goods.class, this::getById, CACHE_GOODS_TTL, TimeUnit.MINUTES);

// 互斥锁解决缓存击穿
Goods goods = cacheClient.queryWithMutex(CACHE_GOODS_KEY, id, Goods.class, this::getById, CACHE_GOODS_TTL, TimeUnit.MINUTES);

// 逻辑过期解决缓存击穿
Goods goods = cacheClient.queryWithLogicalExpire(CACHE_GOODS_KEY, id, Goods.class, this::getById, 20L, TimeUnit.SECONDS);
package com.guor.utils;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

@Slf4j
@Component
public class CacheClient {

    private final StringRedisTemplate stringRedisTemplate;

    private static final ExecutorService CACHE_REBUILD_EXECUTOR = Executors.newFixedThreadPool(10);

    public CacheClient(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }

    public void set(String key, Object value, Long time, TimeUnit unit) {
        stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(value), time, unit);
    }

    public void setWithLogicalExpire(String key, Object value, Long time, TimeUnit unit) {
        // 设置逻辑过期
        RedisData redisData = new RedisData();
        redisData.setData(value);
        redisData.setExpireTime(LocalDateTime.now().plusSeconds(unit.toSeconds(time)));
        // 写入Redis
        stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(redisData));
    }

    public <R,ID> R queryWithPassThrough(String keyPrefix, ID id, Class<R> type, Function<ID, R> dbFallback, Long time, TimeUnit unit){
        String key = keyPrefix + id;
        // 1.从redis查询商铺缓存
        String json = stringRedisTemplate.opsForValue().get(key);
        // 2.判断是否存在
        if (StrUtil.isNotBlank(json)) {
            // 3.存在,直接返回
            return JSONUtil.toBean(json, type);
        }
        // 判断命中的是否是空值
        if (json != null) {
            // 返回一个错误信息
            return null;
        }

        // 4.不存在,根据id查询数据库
        R r = dbFallback.apply(id);
        // 5.不存在,返回错误
        if (r == null) {
            // 将空值写入redis
            stringRedisTemplate.opsForValue().set(key, "", RedisConfig.CACHE_NULL_TTL, TimeUnit.MINUTES);
            // 返回错误信息
            return null;
        }
        // 6.存在,写入redis
        this.set(key, r, time, unit);
        return r;
    }

    public <R, ID> R queryWithLogicalExpire(String keyPrefix, ID id, Class<R> type, Function<ID, R> dbFallback, Long time, TimeUnit unit) {
        String key = keyPrefix + id;
        // 1.从redis查询商铺缓存
        String json = stringRedisTemplate.opsForValue().get(key);
        // 2.判断是否存在
        if (StrUtil.isBlank(json)) {
            // 3.存在,直接返回
            return null;
        }
        // 4.命中,需要先把json反序列化为对象
        RedisData redisData = JSONUtil.toBean(json, RedisData.class);
        R r = JSONUtil.toBean((JSONObject) redisData.getData(), type);
        LocalDateTime expireTime = redisData.getExpireTime();
        // 5.判断是否过期
        if(expireTime.isAfter(LocalDateTime.now())) {
            // 5.1.未过期,直接返回店铺信息
            return r;
        }
        // 5.2.已过期,需要缓存重建
        // 6.缓存重建
        // 6.1.获取互斥锁
        String lockKey = RedisConfig.LOCK_GOODS_KEY + id;
        boolean isLock = tryLock(lockKey);
        // 6.2.判断是否获取锁成功
        if (isLock){
            // 6.3.成功,开启独立线程,实现缓存重建
            CACHE_REBUILD_EXECUTOR.submit(() -> {
                try {
                    // 查询数据库
                    R newR = dbFallback.apply(id);
                    // 重建缓存
                    this.setWithLogicalExpire(key, newR, time, unit);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }finally {
                    // 释放锁
                    unlock(lockKey);
                }
            });
        }
        // 6.4.返回过期的商铺信息
        return r;
    }

    public <R, ID> R queryWithMutex(String keyPrefix, ID id, Class<R> type, Function<ID, R> dbFallback, Long time, TimeUnit unit) {
        String key = keyPrefix + id;
        // 1.从redis查询商铺缓存
        String json = stringRedisTemplate.opsForValue().get(key);
        // 2.判断是否存在
        if (StrUtil.isNotBlank(json)) {
            // 3.存在,直接返回
            return JSONUtil.toBean(json, type);
        }
        // 判断命中的是否是空值
        if (json != null) {
            // 返回一个错误信息
            return null;
        }

        // 4.实现缓存重建
        // 4.1.获取互斥锁
        String lockKey = RedisConfig.LOCK_GOODS_KEY + id;
        R r = null;
        try {
            boolean isLock = tryLock(lockKey);
            // 4.2.判断是否获取成功
            if (!isLock) {
                // 4.3.获取锁失败,休眠并重试
                Thread.sleep(50);
                return queryWithMutex(keyPrefix, id, type, dbFallback, time, unit);
            }
            // 4.4.获取锁成功,根据id查询数据库
            r = dbFallback.apply(id);
            // 5.不存在,返回错误
            if (r == null) {
                // 将空值写入redis
                stringRedisTemplate.opsForValue().set(key, "", RedisConfig.CACHE_NULL_TTL, TimeUnit.MINUTES);
                // 返回错误信息
                return null;
            }
            // 6.存在,写入redis
            this.set(key, r, time, unit);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }finally {
            // 7.释放锁
            unlock(lockKey);
        }
        // 8.返回
        return r;
    }

    private boolean tryLock(String key) {
        Boolean flag = stringRedisTemplate.opsForValue().setIfAbsent(key, "1", 10, TimeUnit.SECONDS);
        return BooleanUtil.isTrue(flag);
    }

    private void unlock(String key) {
        stringRedisTemplate.delete(key);
    }
}

Guess you like

Origin blog.csdn.net/qq_31432773/article/details/128516209