8000 docs: update caching penetration content (#232) · lhzctf/advanced-java@80cb1f2 · GitHub 10000
[go: up one dir, main page]

Skip to content

Commit 80cb1f2

Browse files
liukun2634yanglbme
andauthored
docs: update caching penetration content (doocs#232)
* docs: update caching penetration content * Update redis-caching-avalanche-and-caching-penetration.md Co-authored-by: Yang Libin <contact@yanglibin.info>
1 parent b552288 commit 80cb1f2

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed
Loading

docs/high-concurrency/redis-caching-avalanche-and-caching-penetration.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@
4848

4949
解决方式很简单,每次系统 A 从数据库中只要没查到,就写一个空值到缓存里去,比如 `set -999 UNKNOWN` 。然后设置一个过期时间,这样的话,下次有相同的 key 来访问的时候,在缓存失效之前,都可以直接从缓存中取数据。
5050

51+
当然,如果黑客如果每次使用不同的负数 id 来攻击,写空值的方法可能就不奏效了。更为经常的做法是在缓存之前增加布隆过滤器,将数据库中所有可能的数据哈希映射到布隆过滤器中。然后对每个请求进行如下判断:
52+
53+
- 请求数据的 key 不存在于布隆过滤器中,可以确定数据就一定不会存在于数据库中,系统可以立即返回不存在。
54+
- 请求数据的 key 存在于布隆过滤器中,则继续再向缓存中查询。
55+
56+
使用布隆过滤器能够对访问的请求起到了一定的初筛作用,避免了因数据不存在引起的查询压力。
57+
58+
![redis-caching-avoid-penetration](./images/redis-caching-avoid-penetration.png)
59+
5160
### 缓存击穿
5261

5362
缓存击穿,就是说某个 key 非常热点,访问非常频繁,处于集中式高并发访问的情况,当这个 key 在失效的瞬间,大量的请求就击穿了缓存,直接请求数据库,就像是在一道屏障上凿开了一个洞。

0 commit comments

Comments
 (0)
0