我遇到了一个关于Redis Penatration的问题。来自客户端的太多无意义的查询进入数据库,因为Redis中没有匹配的键。现在我希望使用BloomFilter来过滤一些毫无意义的查询,但是我不知道如何在Redis或DB中使用它。下面列出了一系列问题。
什么时候在Bloom过滤器中添加密钥?就在密钥被添加到Redis缓存之前?如果Redis中的密钥过期,是否删除Bloom过滤器中的密钥?
或者读取数据库中的所有密钥,并将它们放入Blomm过滤器中?但是,如果将密钥从DB中删除,我们可以从BloomFilter中删除密钥吗?
真的很贴切。
发布于 2022-11-21 06:37:27
如何避免高速缓存穿透
当然,最好的策略是在检查缓存之前实现一些逻辑(例如IP范围过滤)。
此外,如果重复查询相同不可接受的地址,则可以考虑使用空字符串值将这些地址存储在Redis中。
如果您需要存储数百万无效密钥,实际上,您可以考虑使用Bloom过滤器。
RedisBloom是由Redis.开发的一个Redis模块,它向Redis添加了概率数据结构(包括一个Bloom过滤器)。您可以在现有Redis的基础上安装RedisBloom,也可以切换到红宝石堆,这包括RedisBloom。
RedisBloom命令被记录为这里。
只需使用BF.RESERVE创建一个Bloom过滤器,并使用BF.ADD添加无效地址。要确定以前是否见过无效地址,请使用BF.EXISTS (答案"1“意味着,在很高的概率下,该值已经被看到,而"0”表示它肯定是以前没有看到的)。
如何处理传入请求
由于使用Bloom过滤器(BF)可以进行假阳性匹配,因此有以下几个选项:
- Add all valid keys to the BF
- When a request is received, search in the Bloom filter.
- If found in the BF - it is, with high probability, a valid key. Try to fetch it from the DB. If not found in the DB (low probability) it was a false positive.
- If not found in the BF: it is necessarily an invalid key.- When a request is received, search in the Bloom filter.
- If found in the BF - it is, with high probability, a valid key that was already seen. Try to fetch it from the DB. If not found in the DB (low probability) it was a false positive.
- If not found in the BF - it is either a first-time valid key or an invalid key. Check, and if valid - add to the BF.- When a request is received, search in the Bloom filter.
- If found in the BF - it is, with high probability, an invalid key. Note that it may be a valid key (low probability) and you will ignore it, but that's a price you should be ready to pay if you go this way.
- If not found in the BF - it is either a valid key or a first-time invalid key. Check, and if invalid - add to the BF.一些注意事项:
https://stackoverflow.com/questions/74514478
复制相似问题