Update 缓存.md

重新绘制 CDN 和一致性哈希的配图,补充多级缓存。
This commit is contained in:
CyC2018
2026-10-26 22:04:10 +08:00
parent cf23f4bd99
commit c398e7b1ed
7 changed files with 272 additions and 7 deletions
+75
View File
@@ -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

+83
View File
@@ -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

+81
View File
@@ -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