BloomFilter:布隆过滤器防止Redis内存穿透

前面提到了Redis缓存穿透,这里介绍一种可以防止缓存穿透的方法,布隆过滤器(BloomFilter),范例代码用的谷歌的Guava,依赖如下:


            com.google.guava
            guava
            28.1-jre

bloomfilter不能确定存在改数据,但是可以判断一条数据一定不存在,刚好契合内存穿透的痛点。

package com.MyExample.Reading.test;

import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

public class test {
    //位向量的长度
    private static int _size = 1000;
    //初始化一个过滤器,ffp就是给出的误报率,应该会按照按照入参的ffp给出数量不同的hash函数
    private static BloomFilter filter = BloomFilter.create(Funnels.stringFunnel(Charset.defaultCharset()), _size, 0.02);

    public static void main(String[] args) {
        //先添加一千条数据
        for (int i = 0; i < _size; ++i) {
            String url = "_Code" + i;
            filter.put(url);
        }
        List list = new ArrayList(_size);

        //验证是否添加成功
        for (int i = 0; i < _size; ++i) {
            String url = "_Code" + i;
            if (filter.mightContain(url)) {
                System.out.println("缓存中已存在");
            }
        }
        //现在用mightContain方法去找1000以后的数据,很明显我们上述代码没有添加
        for (int i = _size; i < _size + 1000; ++i) {
            String url = "_Code" + i;
            if (filter.mightContain(url)) {
                list.add(url);
            }
        }
        System.out.println(list.size());
    }
}
fpp=0.02

按照范例代码得到的误报数量,1000条,误报1000*fpp条,理论上size越大,会越精确。

fpp=0.04

fpp修改为0.04。

下面简单的介绍一下过滤器的思想:
设计一个位向量(bit数组)来作为标志位判断该条数据是否存在。
对一条数据而言,有几个hash函数就会有多少个标志位。


过滤器示意图

内存穿透就是利用一个绝对不会存在的key来请求DB里的value,我们在响应请求之前加一层BloomFilter,快速的利用bit数组查询,N个hash值是否都为1,来判断是否存在该数据,防止了恶意请求。
如何确定fpp

从示意图中可以看出当位向量的长度不足的时候,很容易就会将所有的bit位置1,后续的所有查询都会误报,在初始化是给的预估数据量还是需要慎重填写的,如果真实数据过大,则fpp会偏差严重。

让我们来看下谷歌大佬们怎么实现

static  BloomFilter create(Funnel funnel, long expectedInsertions, double fpp, BloomFilter.Strategy strategy) {
        Preconditions.checkNotNull(funnel);
        Preconditions.checkArgument(expectedInsertions >= 0L, "Expected insertions (%s) must be >= 0", expectedInsertions);
        Preconditions.checkArgument(fpp > 0.0D, "False positive probability (%s) must be > 0.0", fpp);
        Preconditions.checkArgument(fpp < 1.0D, "False positive probability (%s) must be < 1.0", fpp);
        Preconditions.checkNotNull(strategy);
        if (expectedInsertions == 0L) {
            expectedInsertions = 1L;
        }

        long numBits = optimalNumOfBits(expectedInsertions, fpp);
        int numHashFunctions = optimalNumOfHashFunctions(expectedInsertions, numBits);

        try {
            return new BloomFilter(new LockFreeBitArray(numBits), numHashFunctions, funnel, strategy);
        } catch (IllegalArgumentException var10) {
            throw new IllegalArgumentException("Could not create BloomFilter of " + numBits + " bits", var10);
        }
    }

构造函数利用预期插入数量和fpp,来确定bit数组的大小和hash函数的个数,从而保证误报率。
具体公式可以参考:
Bloom Filters by Example
此外,布隆过滤器还可以应用在海量数据筛选中:如何判断一个元素在亿级数据中是否存在?

你可能感兴趣的:(BloomFilter:布隆过滤器防止Redis内存穿透)