Redis缓存穿透——实战代码教学,亲身体验高并发如何解决

  • 🚀 注重版权,转载请注明原作者和原文链接

  • 🥭 作者:全栈小袁

  • 🍎 原创个人开源博客项目(目前V2.0微服务版本):https://github.com/yuanprogrammer/xiaoyuanboke

  • 🍉 开源项目觉得还行的话点点star,有什么需要完善或者点子欢迎提issue

小袁有话说

众所周知,Redis三大问题,缓存穿透缓存击穿缓存雪崩,也是最常见的缓存问题,在面试当中也是经常被问到,今天我们就先来讲讲 缓存穿透 问题的解决以及如何编写代码

之前我也是看过很多相关的知识,这篇文章是结合自己所学总结的一篇文章,如果什么地方有问题或者不足之处可以评论区留言告诉我

缓存击穿和缓存雪崩,后续出~

一、前期准备

数据表

随便创建一个表,这里以用户表作为演示

CREATE TABLE `user`  (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `username` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '用户名',
  `password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '密码',
  `name` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '昵称',
  `mobile` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '号码',
  `email` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '邮箱',
  `gmt_create` datetime(0) NULL DEFAULT NULL COMMENT '时间',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1001 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

使用在线工具随机生成一千条数据,工具地址:https://datum.codedefault.com/

由于SQL比较长,就不放在这里展示了,打开我的笔记即可复制SQL语句https://note.youdao.com/s/13OsjC3d

在这里插入图片描述

下面这些网上都搜得到的,用解压版即可,如果你们找不到可以文章留言(邮箱+工具),我看到会一起打包发给你

Redis

安装好redis,并成功连接上

Redis Desktop Manager

redis客户端工具,跟navicat这种作用相似,方便查看数据情况,当然你不用也行

在这里插入图片描述

Postman

http请求工具

在这里插入图片描述

Jmeter

高并发测试工具

在这里插入图片描述

二、构建Maven项目

创建一个Maven项目,

依赖
<dependencies>
    <!-- Web启动依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.3.12.RELEASE</version>
    </dependency>
    <!-- Redis -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
        <version>2.3.12.RELEASE</version>
    </dependency>
    <!-- MyBatis-Plus -->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.5.0</version>
    </dependency>
    <!-- 数据库驱动 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.22</version>
    </dependency>
    <!-- hutool -->
    <dependency>
        <groupId>cn.hutool</groupId>
        <artifactId>hutool-all</artifactId>
        <optional>true</optional>
        <version>5.7.7</version>
    </dependency>
</dependencies>
application.yml

防止端口冲突,修改端口号

server:
  port: 8085
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/redis?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
    username: xiaoyuan
    password: root
  redis:
    port: 6379
    host: localhost
    database: 0
实体类
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

import java.util.Date;

@Data
@TableName(value = "user")
public class User {

    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    private String username;

    private String password;

    private String name;

    private String mobile;

    private String email;

    private Date gmtCreate;
}

Mapper接口
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.redis.entity.User;
import org.springframework.stereotype.Repository;

@Repository
public interface UserMapper extends BaseMapper<User> {

}
service接口
import com.baomidou.mybatisplus.extension.service.IService;
import com.redis.entity.User;

import java.util.List;

public interface UserService extends IService<User> {

    // 用户查询, 用name字段来模拟查询不存在的用户
    List<User> queryUser(String name);
}
impl实现类

一个简单的查询

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.redis.entity.User;
import com.redis.mapper.UserMapper;
import com.redis.service.UserService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public List<User> queryUser(String name) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getName, name);
        return this.baseMapper.selectList(wrapper);
    }
}
controller

我们先写一个一般业务写法

redisMap 存储每个key成功获取缓存次数的情况
mysqlMap 存储每个key访问数据库次数的情况

clear接口 —— 情况Map结果集,getMap接口 —— 查看Map结果集,query接口 用户查询接口,以name 字段为例简单模拟场景

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.redis.entity.User;
import com.redis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user/api")
public class UserController {

    // 存储每个key走了多少次缓存
    private ConcurrentHashMap<String, Integer> redisMap = new ConcurrentHashMap<>();
    // 存储每个key走了多少次数据库
    private ConcurrentHashMap<String, Integer> mysqlMap = new ConcurrentHashMap<>();

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private UserService userService;

    @GetMapping("clear")
    public void clear() {
        this.redisMap.clear();
        this.mysqlMap.clear();
    }

    @GetMapping("getMap")
    public Object getMap() {
        JSONObject json = new JSONObject();
        json.set("redisMap", this.redisMap);
        json.set("mysqlMap", this.mysqlMap);
        return json;
    }

    @GetMapping("query")
    public Object queryUser(@RequestParam("name") String key) {
        String cache = redisTemplate.opsForValue().get(key);

        // 是否存在缓存
        if (cache != null) {
            this.redisMap.put(key, (this.redisMap.get(key) == null ? 0 : this.redisMap.get(key)) + 1);
            return JSONUtil.parse(cache);
        } else {
            // 不存在缓存, 查询数据库
            this.mysqlMap.put(key, (this.mysqlMap.get(key) == null ? 0 : this.mysqlMap.get(key)) + 1);
            List<User> users = userService.queryUser(key);

            // 有数据, 丢入缓存
            if (users != null && users.size() > 0) {
                redisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(users), 60 * 50, TimeUnit.SECONDS);
            }
            return users;
        }
    }
}

在这里插入图片描述

三、正式开始(重点)

普通测试

我们用 postman 先普通测试一下接口,测试一个存在的数据,正常显示

在这里插入图片描述

jmeter压测

接下来进入正题,用 jmeter 测试不存在的数据,并发1000个线程,循环10次,设置随机变量,控制在 全栈小袁001 ~ 全栈小袁100 之间

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

执行jemter,接着使用postmant查看一下结果,从结果可以看到全部访问了数据库

如果并发非常大,是不是会给数据库造成压力,甚至导致数据库宕奔溃?

在这里插入图片描述

四、空值缓存(方案一)

在这里插入图片描述

加入这一段代码

在这里插入图片描述

// 缓存空值或者默认值
JSONObject json = new JSONObject();
json.set("res", null);
redisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(json), 10, TimeUnit.SECONDS);
return json;

重新启动项目,进行jemeter压测,测试结果可以看到大部分走了redis缓存的默认值,访问mysql的次数大幅度降低了

在这里插入图片描述

在这里插入图片描述

五、白名单(方案二)

方案一中 对所有的非法key都做了缓存,而且都是同样的value,这样的操作造成了数据冗余,而且key的数量非常多

我们可以完善一下,利用redis中的 set 集合,设置 黑名单列表

在这里插入图片描述

@GetMapping("query")
public Object queryUser(@RequestParam("name") String key) {
    // 是否在黑名单中
    if (redisTemplate.opsForSet().isMember("NullSet", key)) {
        this.redisMap.put(key, (this.redisMap.get(key) == null ? 0 : this.redisMap.get(key)) + 1);
        JSONObject json = new JSONObject();
        json.set("res", null);
        // 返回空值
        return json;
    }

    String cache = redisTemplate.opsForValue().get(key);
    if (cache != null) {
        // 存在缓存
        return JSONUtil.parse(cache);
    }else {
        // 查询数据库
        this.mysqlMap.put(key, (this.mysqlMap.get(key) == null ? 0 : this.mysqlMap.get(key)) + 1);
        List<User> users = userService.queryUser(key);

        if (users != null && users.size() > 0) {
            // 有数据, 丢入缓存
            redisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(users), 60 * 5, TimeUnit.SECONDS);
            return users;
        }else {
            // 无数据, 加入黑名单列表
            redisTemplate.opsForSet().add("NullSet", key);
            JSONObject json = new JSONObject();
            json.set("res", null);
            // 返回空值
            return json;
        }
    }
}

启动进行jmeter压测,测试结果可以也是大部分走了redis,减少mysql访问的次数,同时set集合也方便管理,减少数据的冗余

在这里插入图片描述

在这里插入图片描述


六、布隆过滤器(方案三)

无论是 方案一 还是 方案二,数据量大起来对空间消耗还是非常大的,所以就有了第三种方案—— 布隆过滤器

布隆过滤器 我就不不详细介绍了,网上也有很多详细的解释,这里我就大概说一下就行

(1)首先,布隆过滤器的结构是由 二进制 0 1 组成的数组,0是不存在,1是存在

(2)拥有 k 个独立的 哈希函数映射 ,通过要判断的字符分别计算出 哈希值 计算出下标位置,当 k 个下标获取到的值都为 1 时,则认为当前字符存在,否则不存在

(3)优点:速度非常快,占用空间极少,操作的是机器底层二进制向量;缺点:一是存在 误判,不同的字符会出现相同的哈希值,二是 删除困难

(4)这里插一点:上面说到删除困难,于是衍生出了 布谷过滤器 ,可以删除操作,有兴趣的可以自己去看看

在这里插入图片描述

这里我采用的是 hutool 工具库已经封装好的布隆过滤器,当然你也可以使用其他的工具库或者自己封装一个,原理都是一样的

import cn.hutool.bloomfilter.BloomFilter;
import cn.hutool.bloomfilter.BloomFilterUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.redis.entity.User;
import com.redis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user/api")
public class UserController {

    // 存储每个key走了多少次布隆过滤器
    private ConcurrentHashMap<String, Integer> bloomMap = new ConcurrentHashMap<>();
    // 存储每个key走了多少次数据库
    private ConcurrentHashMap<String, Integer> mysqlMap = new ConcurrentHashMap<>();

    // 布隆过滤器, 设置大约1000个数据
    private BloomFilter bloomFilter = BloomFilterUtil.createBitMap(1000);

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private UserService userService;

    @GetMapping("clear")
    public void clear() {
        this.bloomMap.clear();
        this.mysqlMap.clear();
    }

    @GetMapping("getMap")
    public Object getMap() {
        JSONObject json = new JSONObject();
        json.set("bloomMap", this.bloomMap);
        json.set("mysqlMap", this.mysqlMap);
        return json;
    }

    @GetMapping("query")
    public Object queryUser(@RequestParam("name") String key) {
        // 布隆过滤器过滤, 判断是否出现在过滤器里
        if (bloomFilter.contains(key)) {
            this.bloomMap.put(key, (this.bloomMap.get(key) == null ? 0 : this.bloomMap.get(key)) + 1);
            JSONObject json = new JSONObject();
            json.set("res", null);
            // 返回空值
            return json;
        }

        String cache = redisTemplate.opsForValue().get(key);
        if (cache != null) {
            // 返回缓存
            return JSONUtil.parse(cache);
        }else {
            // 查询数据库
            this.mysqlMap.put(key, (this.mysqlMap.get(key) == null ? 0 : this.mysqlMap.get(key)) + 1);
            List<User> users = userService.queryUser(key);

            if (users != null && users.size() > 0) {
                // 有数据, 丢入缓存
                redisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(users), 60 * 5, TimeUnit.SECONDS);
                return users;
            }else {
                // 无数据, 添加到过滤器里
                bloomFilter.add(key);
                JSONObject json = new JSONObject();
                json.set("res", null);
                // 返回空值
                return json;
            }
        }
    }
}

重新启动,压测,测试结果可以看出大部分都被 布隆过滤器 给过滤掉了

在这里插入图片描述

总结

好了,整篇文章到这里就结束了,做个总结,也是我的个人习惯之一

问题产生

客户端发送请求获取数据的时候,在redis中未命中,接着查询数据库也未命中,如果这时候大量请求这些不存在的数据,那么就会给数据库造成一定的压力甚至宕机,这就是 缓存穿透 问题的产生

解决方案
  • 缓存空值或者默认值
    • 对不存在的数据key构建缓存,下次请求时直接返回缓存,对于空间占用问题可以设置缓存时间短一些(5s/10s/20s)
  • 黑名单
    • 利用Set集合,设置空值黑名单列表,遇到存在黑名单中的非法请求直接返回空值或默认值,同时可以自定义操作set集合数据
  • 布隆过滤器
    • 0 1 组长的二进制向量,空间占用极小,速度也非常快,但删除比较困难,且存在一定的误判(不同对象可能存在相同哈希值)
选择

我自己在项目中一般都是用第一种方案,方便刷新,有可能这次这个查询是不存在数据,下次就存在了,那二三方案就比较不好实现刷新

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

OpenYuan开袁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值