缓存穿透与布隆过滤器(Bloom Filter)

news/2025/3/15 11:16:39/

缓存穿透

在高并发场景中,为了避免请求直接打到数据库上(会导致数据库出现性能问题,从而影响整个系统),常使用缓存来处理大部分请求,如memcached、ehcache、redis等。
但对于一些恶意请求,传统缓存机制却难以应对,如存在以下场景:

接口参数为String id,业务处理为获取此参数,执行sql: select * from user where id = ? limit 1;

正常用户请求参数为25,业务处理则为

select * from user where id = 25 limit 1;

正常返回所查询的user表中id为25的信息,然后将次数据放到缓存中,如redis中,设置key=25,value=user.toString(),再有查询id 为25的请求,可以直接返回redis中的数据,而不用反复查询数据库。

但是如果存在请求参数为-1,业务处理则为

select * from user where id = -1 limit 1;

一般情况下,数据库中不会存在id为-1的数据,返回结果为空,所以缓存中也不会存在数据。如果存在大量的此类请求,则全部会打到数据库上,导致性能问题,从而影响整个系统,缓存变成了摆设,没有起到处理重复请求的作用,此种情况叫做缓存穿透

解决方案

  • 应对缓存穿透有多种办法,如建立无效请求的黑名单,查询参数在黑名单中的,可以直接被拦截;
  • 前级放一个有效数据的集合,记录所有的key,根据集合来判断;
  • 等等。

但上述方案都存在很大的弊端

  • 需要一个无限大的空间来存储不存在的信息,不合理;
  • 集合大小和数据库大小正相关,消耗严重;
  • 需要跟着数据库来实时更新。

布隆过滤器(Bloom Filter)

  1. 简介
  • 是一个很长的二进制向量和一系列的随机映射函数

  • 用于检测一个元素是否存在于某个集合中

  • 空间效率和查询时间远优于一般算法

  • 存在一定的误识别率并且无法删除元素

  1. 算法介绍

①使用一系列的随机映射函数将元素映射到二进制向量的上(每一个函数可设置一个1),以16位向量举例,因此可以得到

1010101000101000
原理
②在对元素进行检测判别时

判断原理
③使用同样的映射函数完成映射,得到

0100101010000000

与上一步得到的向量按位进行或运算

        10101010001010000100101010000000----------------------------1110101010101000

得到的向量与原向量不相等,因此可以得出结论,此元素不存在于集合中。

  1. 布隆过滤器的误判别

①按照上述过程生成二进制向量

②检测某个不存在的元素
在这里插入图片描述
得到二进制向量

0010100010100000

与上一步生成的二进制向量按位进行或运算

        10101010001010000010100010100000----------------------------1010101000101000

得到的二进制向量与原向量相等,因此会被误识别为“存在”。

注:布隆过滤器的误识别率与二进制向量的长度、存储元素个数有关系。

JAVA使用

guava.jar已对布隆过滤器进行封装,在pom文件中引入依赖包

		<dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>23.0</version></dependency>

创建布隆过滤器

private static BloomFilter<Integer> bloomFilter = BloomFilter.create(Funnels.integerFunnel(), total, fpp);

解析

BloomFilter.create有三个参数,分别为元素类型,最大存储个数,误判别率。

guava中的封装会根据这三个参数生成二进制向量数组LockFreeBitArray,

存储数越大,误判别率越低,这个数组就越大,理论上运算也就越慢,后面我们会对性能进行测试。
源码如下:

/*** Creates a {@link BloomFilter} with the expected number of insertions and* expected false positive probability.** <p>Note that overflowing a {@code BloomFilter} with significantly more elements than specified,* will result in its saturation, and a sharp deterioration of its false positive probability.** <p>The constructed {@code BloomFilter} will be serializable if the provided* {@code Funnel<T>} is.** <p>It is recommended that the funnel be implemented as a Java enum. This has the benefit of* ensuring proper serialization and deserialization, which is important since {@link #equals}* also relies on object identity of funnels.** @param funnel the funnel of T's that the constructed {@code BloomFilter} will use* @param expectedInsertions the number of expected insertions to the constructed*     {@code BloomFilter}; must be positive* @param fpp the desired false positive probability (must be positive and less than 1.0)* @return a {@code BloomFilter}* @since 19.0*/public static <T> BloomFilter<T> create(Funnel<? super T> funnel, long expectedInsertions, double fpp) {return create(funnel, expectedInsertions, fpp, BloomFilterStrategies.MURMUR128_MITZ_64);}@VisibleForTestingstatic <T> BloomFilter<T> create(Funnel<? super T> funnel, long expectedInsertions, double fpp, Strategy strategy) {checkNotNull(funnel);checkArgument(expectedInsertions >= 0, "Expected insertions (%s) must be >= 0", expectedInsertions);checkArgument(fpp > 0.0, "False positive probability (%s) must be > 0.0", fpp);checkArgument(fpp < 1.0, "False positive probability (%s) must be < 1.0", fpp);checkNotNull(strategy);if (expectedInsertions == 0) {expectedInsertions = 1;}/** TODO(user): Put a warning in the javadoc about tiny fpp values, since the resulting size* is proportional to -log(p), but there is not much of a point after all, e.g.* optimalM(1000, 0.0000000000000001) = 76680 which is less than 10kb. Who cares!*/long numBits = optimalNumOfBits(expectedInsertions, fpp);int numHashFunctions = optimalNumOfHashFunctions(expectedInsertions, numBits);try {return new BloomFilter<T>(new LockFreeBitArray(numBits), numHashFunctions, funnel, strategy);} catch (IllegalArgumentException e) {throw new IllegalArgumentException("Could not create BloomFilter of " + numBits + " bits", e);}

LockFreeBitArray是BloomFilter的静态内部类,通过他的构造器可以发现,实际存储的数据类型为AtomicLongArray

在这里插入图片描述
看到这里的包路径,我们就更可以安心了,线程安全。

元素检测

调用封装好的api,BloomFilter.mightContain

在这里插入图片描述
读注释发现,返回true表示可能含有此数据,返回false表示一定不包含此数据!
上述为使用布隆过滤器的思想。

布隆过滤器的使用与性能表现

package com.example.demo.test;import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;import java.util.ArrayList;
import java.util.List;public class BloomTest {private static int total = 10000000;private static double fpp = 0.0000001;private static BloomFilter<Integer> bloomFilter = BloomFilter.create(Funnels.integerFunnel(), total, fpp);public static void main(String[] args) {System.out.println("=======init========");Long initTime = System.currentTimeMillis();for (int i = 0; i < total; i++) {bloomFilter.put(i);}Long startTime = System.currentTimeMillis();System.out.println("init time:"+(startTime - initTime)+"ms");System.out.println("=======start========");// 存放误判的数据List<Integer> list = new ArrayList<>(1000);for (int i = total+1000; i < total + total ; i++) {if (bloomFilter.mightContain(i)) {list.add(i);}}System.out.println("误判数量:" + list.size());System.out.println("=======done========");Long endTime = System.currentTimeMillis();System.out.println("use time:"+(endTime - startTime)+"ms");}
}

声明一千万的数据量,同时进行一千万的数据检测,误判率为0.0000001

执行结果为

=init==
init time:16554ms
=start==
误判数量:2
=done==
use time:2378ms

可以看出在初始化时,较为耗时,但做一千万数据量与另一个一千万数据量的比对时,仅耗时2.3s,且只有2个误判数量。
对应到实际业务中就是,数据库存在一千万的数据量,恶意请求为一千万个不同的错误id,仅有2个id会打到数据库上,基本可以完成对恶意请求的拦截。
若想要更好的结果,可以继续优化误判别率,或是针对这不多的漏网之鱼定向捕杀。


http://www.ppmy.cn/news/813745.html

相关文章

我的世界服务器皮肤怎么用文件夹,我的世界皮肤正文件,皮肤制作器怎么打开文件夹...

打开versions&#xff0c;我的世界皮肤站怎么找皮肤文件夹里百面有个小茶壶形状的文件&#xff0c;用压度缩工具打开它&#xff0c;依次打知开assets&#xff0c;minecraft&#xff0c;&#xff0c;textures&#xff0c;entity&#xff0c;先将里面自带道的原皮肤删掉&#xff…

微信小程序实现循环列表中加样式

最近开始接触小程序&#xff0c;所以记录一下做项目过程中遇到的问题&#xff0c;由于小程序不想做网站开发&#xff0c;可以直接点击添加样式&#xff0c;得通过this.setdata来设置 这是效果图&#xff1a; 第一步&#xff1a;核心是判断当前点击的项的id&#xff0c;通过三目…

微信公众号全局返回码

公众号每次调用接口时&#xff0c;可能获得正确或错误的返回码&#xff0c;开发者可以根据返回码信息调试接口&#xff0c;排查错误。 全局返回码说明如下&#xff1a; 返回码 说明 -1 系统繁忙&#xff0c;此时请开发者稍候再试 0 请求成功 40001 获取 access_token 时 A…

LeetCode 146. LRU缓存机制Golang版

LeetCode 146. LRU缓存机制Golang版 1. 问题描述 运用你所掌握的数据结构&#xff0c;设计和实现一个 LRU (最近最少使用) 缓存机制 。 实现 LRUCache 类&#xff1a; LRUCache(int capacity) 以正整数作为容量 capacity 初始化 LRU 缓存 int get(int key) 如果关键字 key 存…

使用微信公众号回复带有外链的文字

后台发送 使用客服消息回复文字&#xff0c;按照以下格式&#xff1a; 您好&#xff0c;< a href’https://www.baidu.com‘>点我进行页面跳转

我的世界服务器皮肤怎么用文件夹,我的世界怎么用皮肤文件,怎么通过文件夹更改皮肤...

打开versions&#xff0c;我的世界怎么用文件换皮肤教程里百面有个小茶壶形状的文件&#xff0c;用压度缩工具打开它&#xff0c;依次打知开assets&#xff0c;minecraft&#xff0c;&#xff0c;textures&#xff0c;entity&#xff0c;先将里面自带道的原皮肤删掉&#xff0c…

ios在微信中遇见的bug

ios在微信中目前遇见的bug 1.ios在微信浏览器中不能获取文本域的焦点 将有这几个属性删除即可 * {-webkit-touch-callout:none;-webkit-user-select:none;-khtml-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none; } input {-webkit-user-sel…

基础|01|CPU缓存知识(待完善0.3)

&#xff08;写在前面&#xff0c;本文还没写完&#xff0c;争取在2022.2.1前写完&#xff0c;觉得可以的话&#xff0c;可以先关注噢&#xff09; 概览 由于各存储结构的速度不同&#xff0c;容量和价格上也不同&#xff0c;因此 1、对于单个CPU产生了缓存架构 既然有了缓…