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

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

小袁有话说

众所周知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 组长的二进制向量空间占用极小速度也非常快但删除比较困难且存在一定的误判不同对象可能存在相同哈希值

选择

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

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: redis

“Redis缓存穿透——实战代码教学,亲身体验高并发如何解决” 的相关文章