缓存穿透、缓存击穿、缓存雪崩的区别(Differences among cache penetration, cache breakdown and cache avalanche)

一、缓存穿透:(即:缓存无数据,数据库也无数据)

  如黑客恶意攻击,使用缓存和数据库均没有的key进行不断请求,导致数据库压力过大。

解决方法:

1、对用户进行鉴权、对请求参数进行校验,不合理直接过滤。

2、对查询不到的数据也放到缓存,value为空,设置一定的过期时间。(不太常用,因为如果是随机key就不起作用,且占缓存)

3、使用布隆过滤器,快速判断key是否在数据库中存在,不存在直接返回。(最有效)

第1种是最常用的策略,第2种不太常用,因为如果是随机key就不起作用,且占缓存,第三种最简单有效。实际使用中,可以1、3相结合。

————————

1、 Cache penetration: (i.e. no data in cache and no data in database)

Such as malicious attacks by hackers, using keys that are not available in the cache and the database to make continuous requests, resulting in excessive pressure on the database.

resolvent:

1. It authenticates users and verifies request parameters. It is unreasonable to filter directly.

2. The data that cannot be queried is also put into the cache. If value is empty, set a certain expiration time. (not commonly used, because random keys do not work and occupy the cache)

3. Use the bloom filter to quickly determine whether the key exists in the database. If it does not exist, it will be returned directly. (most effective)

The first is the most commonly used strategy, and the second is less commonly used, because if it is a random key, it will not work and occupy the cache. The third is the most simple and effective. In practical use, 1 and 3 can be combined.