mirror of
https://github.com/CyC2018/Interview-Notebook.git
synced 2026-10-29 07:03:43 +00:00
Update 缓存.md
重新绘制 CDN 和一致性哈希的配图,补充多级缓存。
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 137 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 211 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 169 KiB |
+33
-7
@@ -60,6 +60,33 @@
|
||||
|
||||
相对于本地缓存来说,分布式缓存单独部署,可以根据需求分配硬件资源。不仅如此,服务器集群都可以访问分布式缓存,而本地缓存需要在服务器集群之间进行同步,实现难度和性能开销上都非常大。
|
||||
|
||||
### 多级缓存
|
||||
|
||||
多级缓存把本地缓存和分布式缓存组合起来:先查本地缓存,未命中再查 Redis,再未命中才查数据库,并逐级回填。
|
||||
|
||||
<div align="center"> <img src="pics/multilevel-cache.png" width="760px"/> </div><br>
|
||||
|
||||
在本机测试,Caffeine 读取一次约 21 纳秒;redis-benchmark 用单个连接逐条发送 GET,每次约 71 微秒,相差三千多倍,这还只是本机回环,跨机器访问还要加上网络往返。热点数据放在本地缓存中,既省去了网络开销,也不会让请求集中到存放这个热点键的 Redis 节点上。
|
||||
|
||||
本地缓存在每个实例中各存一份,难点在于数据更新后怎样让所有实例失效:
|
||||
|
||||
- 更新数据库后删除 Redis 中的键,再通过 Redis 的发布订阅或消息队列广播一条删除消息,各实例收到后删除自己的本地缓存;
|
||||
- Redis 的发布订阅不保证送达,实例断线期间的消息会丢失,所以本地缓存还要设置较短的过期时间(例如 30 秒)兜底;
|
||||
- 因此本地缓存只适合读多写少、能接受短暂不一致的数据,例如商品详情、配置;库存、余额这类数据不要放在本地缓存中。
|
||||
|
||||
Caffeine 是目前常用的本地缓存库,使用方式如下:
|
||||
|
||||
```java
|
||||
Cache<String, Product> cache = Caffeine.newBuilder()
|
||||
.maximumSize(10_000) // 最多 1 万个,超出时淘汰
|
||||
.expireAfterWrite(Duration.ofSeconds(30)) // 写入 30 秒后过期
|
||||
.build();
|
||||
|
||||
Product p = cache.get("product:1024", key -> loadFromRedisOrDb(key));
|
||||
```
|
||||
|
||||
它的淘汰算法 W-TinyLFU 同时考虑访问频率和最近访问时间,命中率通常高于 LRU;get 传入的加载函数对同一个键只会执行一次,其它并发的请求等待它的结果,避免缓存未命中时大量请求同时查询下游。
|
||||
|
||||
### 数据库缓存
|
||||
|
||||
MySQL 等数据库管理系统具有自己的缓存机制来提高查询效率,例如 InnoDB 的 Buffer Pool 将数据页和索引页缓存在内存中。MySQL 的查询缓存因为命中率低、维护开销大,已经在 8.0 版本中被移除。
|
||||
@@ -84,7 +111,7 @@ CDN 主要有以下优点:
|
||||
- 通过部署多台服务器,从而提高系统整体的带宽性能;
|
||||
- 多台服务器可以看成是一种冗余机制,从而具有高可用性。
|
||||
|
||||
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/15313ed8-a520-4799-a300-2b6b36be314f.jpg"/> </div><br>
|
||||
<div align="center"> <img src="pics/cdn.png" width="760px"/> </div><br>
|
||||
|
||||
## 四、缓存问题
|
||||
|
||||
@@ -99,7 +126,7 @@ CDN 主要有以下优点:
|
||||
|
||||
布隆过滤器用多个哈希函数把 Key 映射到位数组的多个位置并置为 1。查询时只要有一个位置为 0,Key 就一定不存在;全部为 1 时 Key 可能存在,存在一定的误判率。
|
||||
|
||||
<div align="center"> <img src="pics/bloom-filter.png" width="560px"/> </div><br>
|
||||
<div align="center"> <img src="pics/bloom-filter.png" width="620px"/> </div><br>
|
||||
|
||||
### 缓存击穿
|
||||
|
||||
@@ -132,7 +159,7 @@ CDN 主要有以下优点:
|
||||
|
||||
删除缓存而不是更新缓存,是因为两个并发的写操作到达缓存的顺序可能和到达数据库的顺序不同,导致缓存中是旧值。先更新数据库再删除缓存,是因为反过来的话,删除缓存之后、更新数据库之前的读请求会把旧值重新写入缓存。
|
||||
|
||||
<div align="center"> <img src="pics/cache-aside-race.png" width="500px"/> </div><br>
|
||||
<div align="center"> <img src="pics/cache-aside-race.png" width="520px"/> </div><br>
|
||||
|
||||
这种模式仍然存在小概率的不一致,例如删除缓存失败。常用的补救措施:
|
||||
|
||||
@@ -205,11 +232,9 @@ CDN 主要有以下优点:
|
||||
|
||||
将哈希空间 [0, 2<sup>n</sup>-1] 看成一个哈希环,每个服务器节点都通过哈希配置到哈希环上。每个数据对象通过哈希函数得到哈希值之后,存放到哈希环中顺时针方向第一个大于等于该哈希值的节点上。
|
||||
|
||||
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/68b110b9-76c6-4ee2-b541-4145e65adb3e.jpg"/> </div><br>
|
||||
|
||||
一致性哈希在增加或者删除节点时只会影响到哈希环中相邻的节点,例如下图中在 B 和 C 之间新增节点 X,只需要将节点 C 上哈希值位于 B 和 X 之间的数据迁移到 X 上即可,对于节点 A、B、D 都没有影响。
|
||||
|
||||
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/66402828-fb2b-418f-83f6-82153491bcfe.jpg"/> </div><br>
|
||||
<div align="center"> <img src="pics/consistent-hash.png" width="760px"/> </div><br>
|
||||
|
||||
### 虚拟节点
|
||||
|
||||
@@ -228,7 +253,7 @@ Redis Cluster 使用的不是一致性哈希,而是把 Key 映射到固定的
|
||||
- 访问某个节点时,将其从原来的位置删除,并重新插入到链表头部。这样就能保证链表尾部存储的就是最近最久未使用的节点,当节点数量大于缓存最大空间时就淘汰链表尾部的节点。
|
||||
- 为了使删除操作时间复杂度为 O(1),就不能采用遍历的方式找到某个节点。HashMap 存储着 Key 到节点的映射,通过 Key 就能以 O(1) 的时间得到节点,然后再以 O(1) 的时间将其从双向队列中删除。
|
||||
|
||||
<div align="center"> <img src="pics/lru.png" width="560px"/> </div><br>
|
||||
<div align="center"> <img src="pics/lru.png" width="620px"/> </div><br>
|
||||
|
||||
Java 的 LinkedHashMap 内部就是这种结构,把 accessOrder 设为 true 并重写 removeEldestEntry 方法,就可以直接得到一个 LRU 缓存。
|
||||
|
||||
@@ -368,3 +393,4 @@ public class LRU<K, V> implements Iterable<K> {
|
||||
- [How Aspiration CDN helps to improve your website loading speed?](https://www.aspirationhosting.com/aspiration-cdn/)
|
||||
- [Redis: UNLINK](https://redis.io/docs/latest/commands/unlink/)
|
||||
- [Redis CLI](https://redis.io/docs/latest/develop/tools/cli/)
|
||||
- [Caffeine Wiki](https://github.com/ben-manes/caffeine/wiki)
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="760" height="320" viewBox="0 0 760 320">
|
||||
<style>
|
||||
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
|
||||
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
|
||||
.b { font-weight: bold; }
|
||||
.c { text-anchor: middle; }
|
||||
.e { text-anchor: end; }
|
||||
.sub { font-size: 11px; fill: #6E6E6E; }
|
||||
.note { fill: #C0605A; }
|
||||
.ok { fill: #6A9F68; }
|
||||
.mono { font-family: Menlo, Consolas, monospace; }
|
||||
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
|
||||
.red { stroke: #C0605A; marker-end: url(#r); }
|
||||
.green { stroke: #6A9F68; marker-end: url(#g); }
|
||||
.dash { stroke-dasharray: 5 4; }
|
||||
.line { stroke: #555; stroke-width: 1.1; fill: none; }
|
||||
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
|
||||
.shadow { fill: #DADADA; }
|
||||
</style>
|
||||
<defs>
|
||||
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
|
||||
</marker>
|
||||
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
|
||||
</marker>
|
||||
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
|
||||
</marker>
|
||||
</defs>
|
||||
<rect width="100%" height="100%" fill="#FFFFFF"/>
|
||||
|
||||
<text x="380" y="28" class="title">CDN:用户访问离自己最近的节点</text>
|
||||
<circle cx="60" cy="129" r="9" fill="#E6E3FA" stroke="#B8B0DE" stroke-width="1.2"/>
|
||||
<path d="M43,160 q0,-19 17,-19 q17,0 17,19 z" fill="#E6E3FA" stroke="#B8B0DE" stroke-width="1.2"/>
|
||||
<text x="60" y="180" class="c sub b">杭州的用户</text>
|
||||
<rect x="280" y="60" width="40" height="13" rx="3" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1.2"/>
|
||||
<circle cx="287" cy="66.5" r="2" fill="#BFBEBA"/>
|
||||
<line x1="294" y1="66.5" x2="313" y2="66.5" stroke="#BFBEBA" stroke-width="1"/>
|
||||
<rect x="280" y="76" width="40" height="13" rx="3" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1.2"/>
|
||||
<circle cx="287" cy="82.5" r="2" fill="#BFBEBA"/>
|
||||
<line x1="294" y1="82.5" x2="313" y2="82.5" stroke="#BFBEBA" stroke-width="1"/>
|
||||
<text x="300" y="112" class="c sub b">DNS 调度</text>
|
||||
<text x="300" y="128" class="c sub">返回杭州节点的 IP</text>
|
||||
<rect x="277" y="190" width="46" height="13" rx="3" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1.2"/>
|
||||
<circle cx="284" cy="196.5" r="2" fill="#7DBCB7"/>
|
||||
<line x1="291" y1="196.5" x2="316" y2="196.5" stroke="#7DBCB7" stroke-width="1"/>
|
||||
<rect x="277" y="206" width="46" height="13" rx="3" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1.2"/>
|
||||
<circle cx="284" cy="212.5" r="2" fill="#7DBCB7"/>
|
||||
<line x1="291" y1="212.5" x2="316" y2="212.5" stroke="#7DBCB7" stroke-width="1"/>
|
||||
<rect x="277" y="222" width="46" height="13" rx="3" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1.2"/>
|
||||
<circle cx="284" cy="228.5" r="2" fill="#7DBCB7"/>
|
||||
<line x1="291" y1="228.5" x2="316" y2="228.5" stroke="#7DBCB7" stroke-width="1"/>
|
||||
<text x="300" y="256" class="c sub b">CDN 杭州节点</text>
|
||||
<text x="300" y="272" class="c sub">缓存了 logo.png</text>
|
||||
<rect x="597" y="190" width="46" height="13" rx="3" fill="#C6E4FB" stroke="#7FB2D6" stroke-width="1.2"/>
|
||||
<circle cx="604" cy="196.5" r="2" fill="#7FB2D6"/>
|
||||
<line x1="611" y1="196.5" x2="636" y2="196.5" stroke="#7FB2D6" stroke-width="1"/>
|
||||
<rect x="597" y="206" width="46" height="13" rx="3" fill="#C6E4FB" stroke="#7FB2D6" stroke-width="1.2"/>
|
||||
<circle cx="604" cy="212.5" r="2" fill="#7FB2D6"/>
|
||||
<line x1="611" y1="212.5" x2="636" y2="212.5" stroke="#7FB2D6" stroke-width="1"/>
|
||||
<rect x="597" y="222" width="46" height="13" rx="3" fill="#C6E4FB" stroke="#7FB2D6" stroke-width="1.2"/>
|
||||
<circle cx="604" cy="228.5" r="2" fill="#7FB2D6"/>
|
||||
<line x1="611" y1="228.5" x2="636" y2="228.5" stroke="#7FB2D6" stroke-width="1"/>
|
||||
<text x="620" y="256" class="c sub b">源站(北京)</text>
|
||||
<line x1="86" y1="136" x2="274" y2="88" class="arrow "/>
|
||||
<text x="170" y="98" class="c sub">① 解析 img.example.com</text>
|
||||
<line x1="86" y1="150" x2="270" y2="210" class="arrow "/>
|
||||
<text x="180" y="196" class="c sub">② 请求 logo.png</text>
|
||||
<line x1="330" y1="214" x2="592" y2="214" class="arrow dash"/>
|
||||
<text x="462" y="206" class="c sub">③ 未命中时回源,取回后缓存</text>
|
||||
<path d="M270,232 C200,250 130,200 88,162" class="arrow green"/>
|
||||
<text x="170" y="256" class="c sub ok">④ 返回,命中时不经过源站</text>
|
||||
<text x="30" y="306" class="sub">静态资源由离用户最近的节点返回,延迟低,源站的带宽压力也小;资源更新后要刷新 CDN 缓存,或者在文件名中带上版本号。</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 4.9 KiB |
@@ -0,0 +1,83 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="760" height="390" viewBox="0 0 760 390">
|
||||
<style>
|
||||
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
|
||||
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
|
||||
.b { font-weight: bold; }
|
||||
.c { text-anchor: middle; }
|
||||
.e { text-anchor: end; }
|
||||
.sub { font-size: 11px; fill: #6E6E6E; }
|
||||
.note { fill: #C0605A; }
|
||||
.ok { fill: #6A9F68; }
|
||||
.mono { font-family: Menlo, Consolas, monospace; }
|
||||
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
|
||||
.red { stroke: #C0605A; marker-end: url(#r); }
|
||||
.green { stroke: #6A9F68; marker-end: url(#g); }
|
||||
.dash { stroke-dasharray: 5 4; }
|
||||
.line { stroke: #555; stroke-width: 1.1; fill: none; }
|
||||
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
|
||||
.shadow { fill: #DADADA; }
|
||||
</style>
|
||||
<defs>
|
||||
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
|
||||
</marker>
|
||||
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
|
||||
</marker>
|
||||
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
|
||||
</marker>
|
||||
</defs>
|
||||
<rect width="100%" height="100%" fill="#FFFFFF"/>
|
||||
|
||||
<text x="380" y="28" class="title">一致性哈希:数据放在顺时针方向的第一个节点上</text>
|
||||
<circle cx="200" cy="200" r="130" fill="none" stroke="#DDD" stroke-width="2"/>
|
||||
<circle cx="244.5" cy="77.8" r="15" fill="#C6E4FB" stroke="#7FB2D6"/>
|
||||
<text x="244.463" y="81.84" class="c sub b" style="fill:#444">A</text>
|
||||
<circle cx="322.2" cy="244.5" r="15" fill="#C6E4FB" stroke="#7FB2D6"/>
|
||||
<text x="322.16" y="248.463" class="c sub b" style="fill:#444">B</text>
|
||||
<circle cx="135.0" cy="312.6" r="15" fill="#C6E4FB" stroke="#7FB2D6"/>
|
||||
<text x="135" y="316.583" class="c sub b" style="fill:#444">C</text>
|
||||
<circle cx="77.8" cy="155.5" r="15" fill="#C6E4FB" stroke="#7FB2D6"/>
|
||||
<text x="77.84" y="159.537" class="c sub b" style="fill:#444">D</text>
|
||||
<circle cx="322.2" cy="155.5" r="5" fill="#E6E3FA" stroke="#B8B0DE"/>
|
||||
<text x="297.728" y="168.43" class="c sub mono">k1</text>
|
||||
<circle cx="265.0" cy="312.6" r="5" fill="#E6E3FA" stroke="#B8B0DE"/>
|
||||
<text x="252" y="294.067" class="c sub mono">k2</text>
|
||||
<circle cx="200.0" cy="330.0" r="5" fill="#E6E3FA" stroke="#B8B0DE"/>
|
||||
<text x="200" y="308" class="c sub mono">k3</text>
|
||||
<circle cx="77.8" cy="244.5" r="5" fill="#E6E3FA" stroke="#B8B0DE"/>
|
||||
<text x="102.272" y="239.57" class="c sub mono">k4</text>
|
||||
<circle cx="244.5" cy="322.2" r="13" fill="#FFFFFF" stroke="#C0605A" stroke-dasharray="3 2"/>
|
||||
<text x="244.463" y="326.16" class="c sub b note">X</text>
|
||||
<text x="275.987" y="346.833" class="sub note">新增 X</text>
|
||||
<text x="200" y="196" class="c sub b">哈希环</text>
|
||||
<text x="200" y="214" class="c sub mono">0 ~ 2³² − 1</text>
|
||||
<text x="390" y="60" class="sub" style="fill:#444">顺时针找第一个节点:k1 → B,k2、k3 → C,k4 → D。</text>
|
||||
<text x="390" y="80" class="sub" style="fill:#444">在 B、C 之间新增 X 后,只有 k2 改由 X 负责,</text>
|
||||
<text x="390" y="98" class="sub" style="fill:#444">k3 和其它节点上的数据都不动。</text>
|
||||
<text x="390" y="134" class="b">10 万个键(MD5 取前 32 位)的实测结果</text>
|
||||
<rect x="390" y="146" width="130" height="26" rx="3" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1"/>
|
||||
<text x="455" y="163" class="c sub" style="fill:#444"></text>
|
||||
<rect x="520" y="146" width="110" height="26" rx="3" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1"/>
|
||||
<text x="575" y="163" class="c sub" style="fill:#444">每节点 1 个点</text>
|
||||
<rect x="630" y="146" width="110" height="26" rx="3" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1"/>
|
||||
<text x="685" y="163" class="c sub" style="fill:#444">每节点 150 个</text>
|
||||
<rect x="390" y="172" width="130" height="28" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
|
||||
<text x="455" y="190" class="c sub" style="fill:#444">最多 / 最少</text>
|
||||
<rect x="520" y="172" width="110" height="28" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
|
||||
<text x="575" y="190" class="c sub mono" style="fill:#444">41.6 倍</text>
|
||||
<rect x="630" y="172" width="110" height="28" rx="3" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1"/>
|
||||
<text x="685" y="190" class="c sub mono" style="fill:#444">1.17 倍</text>
|
||||
<rect x="390" y="200" width="130" height="28" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
|
||||
<text x="455" y="218" class="c sub" style="fill:#444">新增 1 个节点迁移</text>
|
||||
<rect x="520" y="200" width="110" height="28" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
|
||||
<text x="575" y="218" class="c sub mono" style="fill:#444">33.8%</text>
|
||||
<rect x="630" y="200" width="110" height="28" rx="3" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1"/>
|
||||
<text x="685" y="218" class="c sub mono" style="fill:#444">20.7%</text>
|
||||
<text x="390" y="256" class="sub">4 个节点各放 1 个点时,位置是随机的,最多的节点分到</text>
|
||||
<text x="390" y="274" class="sub">55.4% 的键,最少的只有 1.3%;新增节点后应迁移约 1/5,</text>
|
||||
<text x="390" y="292" class="sub">实际迁移了 33.8%。每个节点放 150 个虚拟节点后就接近均匀。</text>
|
||||
<text x="390" y="318" class="sub" style="fill:#444">对比:hash % 4 改为 hash % 5,79.7% 的键要迁移。</text>
|
||||
<text x="30" y="372" class="sub">左图是示意,节点位置不按比例;右表是实际计算的结果。</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 5.8 KiB |
@@ -0,0 +1,81 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="760" height="330" viewBox="0 0 760 330">
|
||||
<style>
|
||||
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
|
||||
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
|
||||
.b { font-weight: bold; }
|
||||
.c { text-anchor: middle; }
|
||||
.e { text-anchor: end; }
|
||||
.sub { font-size: 11px; fill: #6E6E6E; }
|
||||
.note { fill: #C0605A; }
|
||||
.ok { fill: #6A9F68; }
|
||||
.mono { font-family: Menlo, Consolas, monospace; }
|
||||
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
|
||||
.red { stroke: #C0605A; marker-end: url(#r); }
|
||||
.green { stroke: #6A9F68; marker-end: url(#g); }
|
||||
.dash { stroke-dasharray: 5 4; }
|
||||
.line { stroke: #555; stroke-width: 1.1; fill: none; }
|
||||
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
|
||||
.shadow { fill: #DADADA; }
|
||||
</style>
|
||||
<defs>
|
||||
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
|
||||
</marker>
|
||||
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
|
||||
</marker>
|
||||
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
|
||||
</marker>
|
||||
</defs>
|
||||
<rect width="100%" height="100%" fill="#FFFFFF"/>
|
||||
|
||||
<text x="380" y="28" class="title">多级缓存:本地缓存 → Redis → 数据库</text>
|
||||
<rect x="30" y="60" width="250" height="96" rx="8" class="frame"/>
|
||||
<text x="40" y="78" class="b">应用实例 1</text>
|
||||
<rect x="50" y="90" width="40" height="13" rx="3" fill="#C6E4FB" stroke="#7FB2D6" stroke-width="1.2"/>
|
||||
<circle cx="57" cy="96.5" r="2" fill="#7FB2D6"/>
|
||||
<line x1="64" y1="96.5" x2="83" y2="96.5" stroke="#7FB2D6" stroke-width="1"/>
|
||||
<rect x="50" y="106" width="40" height="13" rx="3" fill="#C6E4FB" stroke="#7FB2D6" stroke-width="1.2"/>
|
||||
<circle cx="57" cy="112.5" r="2" fill="#7FB2D6"/>
|
||||
<line x1="64" y1="112.5" x2="83" y2="112.5" stroke="#7FB2D6" stroke-width="1"/>
|
||||
<rect x="110" y="88" width="150" height="50" rx="6" fill="#C6ECE9" stroke="#7DBCB7"/>
|
||||
<text x="185" y="108" class="c sub b" style="fill:#444">本地缓存 Caffeine</text>
|
||||
<text x="185" y="126" class="c sub">约 21 ns</text>
|
||||
<rect x="30" y="170" width="250" height="96" rx="8" class="frame"/>
|
||||
<text x="40" y="188" class="b">应用实例 2</text>
|
||||
<rect x="50" y="200" width="40" height="13" rx="3" fill="#C6E4FB" stroke="#7FB2D6" stroke-width="1.2"/>
|
||||
<circle cx="57" cy="206.5" r="2" fill="#7FB2D6"/>
|
||||
<line x1="64" y1="206.5" x2="83" y2="206.5" stroke="#7FB2D6" stroke-width="1"/>
|
||||
<rect x="50" y="216" width="40" height="13" rx="3" fill="#C6E4FB" stroke="#7FB2D6" stroke-width="1.2"/>
|
||||
<circle cx="57" cy="222.5" r="2" fill="#7FB2D6"/>
|
||||
<line x1="64" y1="222.5" x2="83" y2="222.5" stroke="#7FB2D6" stroke-width="1"/>
|
||||
<rect x="110" y="198" width="150" height="50" rx="6" fill="#C6ECE9" stroke="#7DBCB7"/>
|
||||
<text x="185" y="218" class="c sub b" style="fill:#444">本地缓存 Caffeine</text>
|
||||
<text x="185" y="236" class="c sub">约 21 ns</text>
|
||||
<path d="M390,86 v44 a30,6 0 0 0 60,0 v-44" fill="#C6E4FB" stroke="#7FB2D6" stroke-width="1.2"/>
|
||||
<ellipse cx="420" cy="86" rx="30" ry="6" fill="#C6E4FB" stroke="#7FB2D6" stroke-width="1.2"/>
|
||||
<path d="M390,108 a30,6 0 0 0 60,0" fill="none" stroke="#7FB2D6" stroke-width="1"/>
|
||||
<text x="420" y="154" class="c b">Redis</text>
|
||||
<text x="420" y="170" class="c sub">约 71 μs(本机回环)</text>
|
||||
<path d="M590,86 v44 a30,6 0 0 0 60,0 v-44" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1.2"/>
|
||||
<ellipse cx="620" cy="86" rx="30" ry="6" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1.2"/>
|
||||
<path d="M590,108 a30,6 0 0 0 60,0" fill="none" stroke="#BFBEBA" stroke-width="1"/>
|
||||
<text x="620" y="154" class="c b">MySQL</text>
|
||||
<text x="620" y="170" class="c sub">毫秒级</text>
|
||||
<line x1="282" y1="100" x2="386" y2="104" class="arrow "/>
|
||||
<text x="334" y="92" class="c sub">① 未命中</text>
|
||||
<line x1="452" y1="104" x2="586" y2="104" class="arrow "/>
|
||||
<text x="519" y="96" class="c sub">② 未命中</text>
|
||||
<path d="M590,120 C520,140 480,140 454,124" class="arrow dash"/>
|
||||
<text x="519" y="146" class="c sub">回填</text>
|
||||
<path d="M386,120 C350,140 310,140 284,120" class="arrow dash"/>
|
||||
<text x="334" y="146" class="c sub">回填</text>
|
||||
<rect x="330" y="214" width="400" height="40" rx="6" fill="#E6E3FA" stroke="#B8B0DE"/>
|
||||
<text x="530" y="232" class="c sub" style="fill:#444">数据更新:写数据库,删除 Redis,再广播删除本地缓存</text>
|
||||
<text x="530" y="248" class="c sub">(Redis Pub/Sub 或消息队列)</text>
|
||||
<path d="M330,234 C300,234 290,150 282,128" class="arrow dash red"/>
|
||||
<path d="M330,240 C300,240 292,230 282,224" class="arrow dash red"/>
|
||||
<text x="30" y="292" class="sub">热点数据在本地内存中直接命中,不经过网络,也不会集中打到 Redis 的某一个节点;</text>
|
||||
<text x="30" y="310" class="sub">代价是每个实例各存一份,更新时要通知所有实例,并给本地缓存设置较短的过期时间兜底。</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 5.3 KiB |
Reference in New Issue
Block a user