使用布隆过滤器来解决缓存穿透问题
布隆过滤器其本质就是一个只包含0和1的数组,具体操作当一个元素被加入到集合里面后,该元素通过K个Hash函数运算得到K个hash后的值,然后将K个值映射到这个位数组对应的位置,把对应位置的值设置为1。查询是否存在时,我们就看对应的映射点位置如果全是1,他就很可能存在(跟hash函数的个数和hash函数的设计有关),如果有一个位置是0,那这个元素就一定不存在。
bloom filter之所以能做到在时间和空间上的效率比较高,是因为牺牲了判断的准确率、删除的便利性
Counting Bloom Filter
在redis布隆过滤器插件地址下载最新的release源码,在编译服务器进行解压编译
wget https://github.com/RedisBloom/RedisBloom/archive/v2.2.4.tar.gz
解压插件进行插件的编译
tar RedisBloom-2.2.4.tar.gz
cd RedisBloom-2.2.4
make
编译得到动态库rebloom.so
启动redis时,如下启动即可加载bloom filter插件
配置文件形式配置
#在redis配置文件(redis.conf)中加入该模块即可
vim redis.conf
#添加
loadmodule /root/bloom/redisbloom-2.2.4/rebloom.so (前面为你自己的路径)
启动命令挂载
redis-server redis.conf --loadmodule /usr/rebloom/rebloom.so INITIAL_SIZE 1000000 ERROR_RATE 0.0001
#容量100万, 容错率万分之一, 占用空间是4m
docker run -d -p 6379:6379 --name redis-redisbloom redislabs/rebloom:latest
mkdir -p /tmp/etc/redis/
mkdir -p /tmp/data/redis/node{1..6}
将配置文件写入到redis文件中
cat > /tmp/etc/redis/redis.conf<< EOF
protected-mode no
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile ""
databases 1
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
rdb-del-sync-files no
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
oom-score-adj no
oom-score-adj-values 0 200 800
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
jemalloc-bg-thread yes
loadmodule /etc/redis/redisbloom.so
EOF
这里将本地编译好的redisbloom.so挂载到/etc/redis/redisbloom.so目录
version: '2'
services:
redis01:
image: redis
hostname: redis01
container_name: redis01
networks:
docker-network:
ipv4_address: 172.18.0.2
ports:
- "7001:6379"
volumes:
- "/tmp/etc/redis/redis.conf:/etc/redis/redis.conf"
- "/tmp/data/redis/node1:/data"
- "/tmp/etc/redis/redisbloom.so:/etc/redis/redisbloom.so"
command:
redis-server /etc/redis/redis.conf
redis02:
image: redis
hostname: redis02
container_name: redis02
networks:
docker-network:
ipv4_address: 172.18.0.3
ports:
- "7002:6379"
volumes:
- "/tmp/etc/redis/redis.conf:/etc/redis/redis.conf"
- "/tmp/data/redis/node2:/data"
- "/tmp/etc/redis/redisbloom.so:/etc/redis/redisbloom.so"
command:
redis-server /etc/redis/redis.conf
redis03:
image: redis
hostname: redis03
container_name: redis03
networks:
docker-network:
ipv4_address: 172.18.0.4
ports:
- "7003:6379"
volumes:
- "/tmp/etc/redis/redis.conf:/etc/redis/redis.conf"
- "/tmp/data/redis/node3:/data"
- "/tmp/etc/redis/redisbloom.so:/etc/redis/redisbloom.so"
command:
redis-server /etc/redis/redis.conf
redis04:
image: redis
hostname: redis04
container_name: redis04
networks:
docker-network:
ipv4_address: 172.18.0.5
ports:
- "7004:6379"
volumes:
- "/tmp/etc/redis/redis.conf:/etc/redis/redis.conf"
- "/tmp/data/redis/node4:/data"
- "/tmp/etc/redis/redisbloom.so:/etc/redis/redisbloom.so"
command:
redis-server /etc/redis/redis.conf
redis05:
image: redis
hostname: redis05
container_name: redis05
networks:
docker-network:
ipv4_address: 172.18.0.6
ports:
- "7005:6379"
volumes:
- "/tmp/etc/redis/redis.conf:/etc/redis/redis.conf"
- "/tmp/data/redis/node5:/data"
- "/tmp/etc/redis/redisbloom.so:/etc/redis/redisbloom.so"
command:
redis-server /etc/redis/redis.conf
redis06:
image: redis
hostname: redis06
container_name: redis06
networks:
docker-network:
ipv4_address: 172.18.0.7
ports:
- "7006:6379"
volumes:
- "/tmp/etc/redis/redis.conf:/etc/redis/redis.conf"
- "/tmp/data/redis/node6:/data"
- "/tmp/etc/redis/redisbloom.so:/etc/redis/redisbloom.so"
command:
redis-server /etc/redis/redis.conf
networks:
docker-network:
ipam:
config:
- subnet: 172.18.0.0/16
gateway: 172.18.0.1
docker-compose up -d
进入一个容器
x
docker exec -ti redis01 /bin/bash
执行创建集群命令
x
redis-cli --cluster create 172.18.0.2:6379 172.18.0.3:6379 172.18.0.4:6379 172.18.0.5:6379 172.18.0.6:6379 172.18.0.7:6379 --cluster-replicas 1
命令 | 功能 | 参数 |
---|---|---|
BF.RESERVE | 创建一个大小为capacity,错误率为error_rate的空的Bloom | BF.RESERVE {key} {error_rate} {capacity} [EXPANSION expansion] [NONSCALING] |
BF.ADD | 向key指定的Bloom中添加一个元素item | BF.ADD {key} {item} |
BF.MADD | 向key指定的Bloom中添加多个元素 | BF.MADD {key} {item} [item…] |
BF.INSERT | 向key指定的Bloom中添加多个元素,添加时可以指定大小和错误率,且可以控制在Bloom不存在的时候是否自动创建 | BF.INSERT {key} [CAPACITY {cap}] [ERROR {error}] [EXPANSION expansion] [NOCREATE] [NONSCALING] ITEMS {item…} |
BF.EXISTS | 检查一个元素是否可能存在于key指定的Bloom中 | BF.EXISTS {key} {item} |
BF.MEXISTS | 同时检查多个元素是否可能存在于key指定的Bloom中 | BF.MEXISTS {key} {item} [item…] |
BF.SCANDUMP | 对Bloom进行增量持久化操作 | BF.SCANDUMP {key} {iter} |
BF.LOADCHUNK | 加载SCANDUMP持久化的Bloom数据 | BF.LOADCHUNK {key} {iter} {data} |
BF.INFO | 查询key指定的Bloom的信息 | BF.INFO {key} |
BF.DEBUG | 查看BloomFilter的内部详细信息(如每层的元素个数、错误率等) | BF.DEBUG {key} |
可以登录集群执行命令
# 进入一个redis集群的节点内部
docker exec -ti redis01 /bin/bash
# 以集群方式登录172.18.0.2:3306节点
redis-cli -h 172.18.0.2 -c
# 在redis中添加一个布隆过滤器 错误率是0.01 数量是1万个
BF.RESERVE bf_test 0.01 10000 NONSCALING
# 在bf_test 的布隆过滤器添加一个key
BF.ADD bf_test key
# 验证布隆过滤器key是否存在
BF.EXISTS bf_test key
# 验证布隆过滤器key1是否存在
BF.EXISTS bf_test key1