Update 分布式.md

重新绘制配图,补充分布式 ID 和幂等。
This commit is contained in:
CyC2018
2026-10-26 22:04:03 +08:00
parent c942f9509a
commit 07db1f5e9e
11 changed files with 421 additions and 33 deletions
+58
View File
@@ -0,0 +1,58 @@
<svg xmlns="http://www.w3.org/2000/svg" width="720" height="250" viewBox="0 0 720 250">
<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="360" y="28" class="title">Paxos 的三类角色</text>
<rect x="40" y="70" width="130" height="36" rx="6" fill="#C6ECE9" stroke="#7DBCB7"/>
<text x="105" y="92.5" class="c sub b">Proposer A</text>
<rect x="40" y="150" width="130" height="36" rx="6" fill="#C6ECE9" stroke="#7DBCB7"/>
<text x="105" y="172.5" class="c sub b">Proposer B</text>
<rect x="300" y="50" width="130" height="36" rx="6" fill="#C6E4FB" stroke="#7FB2D6"/>
<text x="365" y="72.5" class="c sub b">Acceptor X</text>
<rect x="300" y="110" width="130" height="36" rx="6" fill="#C6E4FB" stroke="#7FB2D6"/>
<text x="365" y="132.5" class="c sub b">Acceptor Y</text>
<rect x="300" y="170" width="130" height="36" rx="6" fill="#C6E4FB" stroke="#7FB2D6"/>
<text x="365" y="192.5" class="c sub b">Acceptor Z</text>
<rect x="560" y="110" width="120" height="36" rx="6" fill="#EAE9E7" stroke="#BFBEBA"/>
<text x="620" y="132.5" class="c sub b">Learner</text>
<line x1="172" y1="88" x2="298" y2="68" class="arrow "/>
<line x1="172" y1="88" x2="298" y2="128" class="arrow "/>
<line x1="172" y1="88" x2="298" y2="188" class="arrow "/>
<line x1="172" y1="168" x2="298" y2="68" class="arrow "/>
<line x1="172" y1="168" x2="298" y2="128" class="arrow "/>
<line x1="172" y1="168" x2="298" y2="188" class="arrow "/>
<line x1="432" y1="68" x2="558" y2="128" class="arrow dash"/>
<line x1="432" y1="128" x2="558" y2="128" class="arrow dash"/>
<line x1="432" y1="188" x2="558" y2="128" class="arrow dash"/>
<text x="235" y="46" class="c sub">Prepare、Accept</text>
<text x="495" y="66" class="c sub">接受的提议</text>
<text x="30" y="236" class="sub">Proposer 向所有 Acceptor 发请求,超过一半 Acceptor 接受同一个提议,这个值就被选定;Learner 只负责得知结果。</text>
</svg>

After

Width:  |  Height:  |  Size: 3.3 KiB

+74
View File
@@ -0,0 +1,74 @@
<svg xmlns="http://www.w3.org/2000/svg" width="760" height="350" viewBox="0 0 760 350">
<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">Raft 选举:A 的竞选计时器最先到期</text>
<text x="40" y="84" class="sub b" style="fill:#444">节点 A</text>
<text x="40" y="100" class="sub">超时 180 ms</text>
<rect x="150" y="72" width="247.5" height="16" rx="3" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1"/>
<text x="273.75" y="84" class="c sub">Follower,等待心跳</text>
<rect x="397.5" y="72" width="22" height="16" rx="3" fill="#E6E3FA" stroke="#B8B0DE" stroke-width="1"/>
<rect x="419.5" y="72" width="280.5" height="16" rx="3" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1"/>
<text x="562.5" y="84" class="c sub b" style="fill:#444">Leader,任期 1</text>
<text x="40" y="154" class="sub b" style="fill:#444">节点 B</text>
<text x="40" y="170" class="sub">超时 260 ms</text>
<rect x="150" y="142" width="261.25" height="16" rx="3" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1"/>
<text x="273.75" y="154" class="c sub">Follower</text>
<rect x="419.5" y="142" width="4" height="16" rx="1" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1"/>
<rect x="488.25" y="142" width="4" height="16" rx="1" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1"/>
<rect x="557" y="142" width="4" height="16" rx="1" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1"/>
<rect x="625.75" y="142" width="4" height="16" rx="1" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1"/>
<rect x="694.5" y="142" width="4" height="16" rx="1" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1"/>
<line x1="507.5" y1="138" x2="507.5" y2="162" stroke="#C0605A" stroke-dasharray="2 2"/>
<text x="507.5" y="176" class="c sub">原定 260 ms 超时</text>
<text x="40" y="224" class="sub b" style="fill:#444">节点 C</text>
<text x="40" y="240" class="sub">超时 230 ms</text>
<rect x="150" y="212" width="261.25" height="16" rx="3" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1"/>
<text x="273.75" y="224" class="c sub">Follower</text>
<rect x="419.5" y="212" width="4" height="16" rx="1" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1"/>
<rect x="488.25" y="212" width="4" height="16" rx="1" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1"/>
<rect x="557" y="212" width="4" height="16" rx="1" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1"/>
<rect x="625.75" y="212" width="4" height="16" rx="1" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1"/>
<rect x="694.5" y="212" width="4" height="16" rx="1" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1"/>
<line x1="466.25" y1="208" x2="466.25" y2="232" stroke="#C0605A" stroke-dasharray="2 2"/>
<text x="466.25" y="246" class="c sub">原定 230 ms 超时</text>
<text x="397.5" y="64" class="sub" style="fill:#766AA8">180 ms 超时:成为候选人,任期加 1,投自己一票,向 B、C 请求投票</text>
<line x1="408.5" y1="90" x2="408.5" y2="210" stroke="#B8B0DE" stroke-dasharray="3 3"/>
<text x="402.5" y="119" class="sub e">B、C 投票给 A,3 票中得到 3 票</text>
<text x="562.5" y="268" class="c sub">浅青色短条是 Leader 当选后立即发出、之后每 50 ms 发一次的心跳,Follower 收到后重新计时</text>
<line x1="150" y1="292" x2="700" y2="292" class="line"/>
<text x="150" y="310" class="c sub mono">0 ms</text>
<text x="287.5" y="310" class="c sub mono">100 ms</text>
<text x="425" y="310" class="c sub mono">200 ms</text>
<text x="562.5" y="310" class="c sub mono">300 ms</text>
<text x="700" y="310" class="c sub mono">400 ms</text>
<text x="30" y="338" class="sub">每个节点的竞选超时在 150~300 ms 中随机选取,通常只有一个节点先超时,它在别人超时前就能拿到多数票。</text>
</svg>

After

Width:  |  Height:  |  Size: 5.2 KiB

+88
View File
@@ -0,0 +1,88 @@
<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">Raft 日志复制:SET x = 5</text>
<text x="162" y="56" class="c sub b" style="fill:#444">① 写入 Leader 的日志</text>
<text x="80" y="91" class="sub b e" style="fill:#444">Leader</text>
<rect x="90" y="72" width="72" height="28" rx="3" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1"/>
<text x="126" y="90" class="c sub mono" style="fill:#444">1 y=1</text>
<rect x="162" y="72" width="72" height="28" rx="3" fill="#E6E3FA" stroke="#B8B0DE" stroke-width="1"/>
<text x="198" y="90" class="c sub mono" style="fill:#444">1 x=5</text>
<text x="80" y="133" class="sub b e" style="fill:#444">B</text>
<rect x="90" y="114" width="72" height="28" rx="3" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1"/>
<text x="126" y="132" class="c sub mono" style="fill:#444">1 y=1</text>
<rect x="162" y="114" width="72" height="28" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="80" y="175" class="sub b e" style="fill:#444">C</text>
<rect x="90" y="156" width="72" height="28" rx="3" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1"/>
<text x="126" y="174" class="c sub mono" style="fill:#444">1 y=1</text>
<rect x="162" y="156" width="72" height="28" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="332" y="56" class="c sub b" style="fill:#444">② 复制给 Follower</text>
<rect x="260" y="72" width="72" height="28" rx="3" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1"/>
<text x="296" y="90" class="c sub mono" style="fill:#444">1 y=1</text>
<rect x="332" y="72" width="72" height="28" rx="3" fill="#E6E3FA" stroke="#B8B0DE" stroke-width="1"/>
<text x="368" y="90" class="c sub mono" style="fill:#444">1 x=5</text>
<rect x="260" y="114" width="72" height="28" rx="3" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1"/>
<text x="296" y="132" class="c sub mono" style="fill:#444">1 y=1</text>
<rect x="332" y="114" width="72" height="28" rx="3" fill="#E6E3FA" stroke="#B8B0DE" stroke-width="1"/>
<text x="368" y="132" class="c sub mono" style="fill:#444">1 x=5</text>
<rect x="260" y="156" width="72" height="28" rx="3" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1"/>
<text x="296" y="174" class="c sub mono" style="fill:#444">1 y=1</text>
<rect x="332" y="156" width="72" height="28" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="502" y="56" class="c sub b" style="fill:#444">③ 多数写入后提交</text>
<rect x="430" y="72" width="72" height="28" rx="3" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1"/>
<text x="466" y="90" class="c sub mono" style="fill:#444">1 y=1</text>
<rect x="502" y="72" width="72" height="28" rx="3" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1"/>
<text x="538" y="90" class="c sub mono" style="fill:#444">1 x=5</text>
<rect x="430" y="114" width="72" height="28" rx="3" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1"/>
<text x="466" y="132" class="c sub mono" style="fill:#444">1 y=1</text>
<rect x="502" y="114" width="72" height="28" rx="3" fill="#E6E3FA" stroke="#B8B0DE" stroke-width="1"/>
<text x="538" y="132" class="c sub mono" style="fill:#444">1 x=5</text>
<rect x="430" y="156" width="72" height="28" rx="3" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1"/>
<text x="466" y="174" class="c sub mono" style="fill:#444">1 y=1</text>
<rect x="502" y="156" width="72" height="28" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="672" y="56" class="c sub b" style="fill:#444">④ 心跳通知 Follower 提交</text>
<rect x="600" y="72" width="72" height="28" rx="3" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1"/>
<text x="636" y="90" class="c sub mono" style="fill:#444">1 y=1</text>
<rect x="672" y="72" width="72" height="28" rx="3" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1"/>
<text x="708" y="90" class="c sub mono" style="fill:#444">1 x=5</text>
<rect x="600" y="114" width="72" height="28" rx="3" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1"/>
<text x="636" y="132" class="c sub mono" style="fill:#444">1 y=1</text>
<rect x="672" y="114" width="72" height="28" rx="3" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1"/>
<text x="708" y="132" class="c sub mono" style="fill:#444">1 x=5</text>
<rect x="600" y="156" width="72" height="28" rx="3" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1"/>
<text x="636" y="174" class="c sub mono" style="fill:#444">1 y=1</text>
<rect x="672" y="156" width="72" height="28" rx="3" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1"/>
<text x="708" y="174" class="c sub mono" style="fill:#444">1 x=5</text>
<text x="30" y="236" class="sub">格子中是任期号和命令。浅青色已提交,淡紫色已写入日志但未提交。</text>
<text x="30" y="254" class="sub">第 ③ 步时 C 还没有写入,但 Leader 和 B 已经构成 3 个节点中的多数,Leader 就可以提交、执行并回复客户端;</text>
<text x="30" y="272" class="sub">已提交的日志一定在多数节点上,新 Leader 也一定包含它,所以不会丢失。C 落后或宕机恢复后,Leader 再把缺少的日志补给它。</text>
</svg>

After

Width:  |  Height:  |  Size: 6.6 KiB

+63
View File
@@ -0,0 +1,63 @@
<svg xmlns="http://www.w3.org/2000/svg" width="760" height="300" viewBox="0 0 760 300">
<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">Raft 选举:选票被瓜分时进入下一个任期</text>
<text x="30" y="92" class="b">任期 4</text>
<rect x="110" y="70" width="130" height="40" rx="6" fill="#C6E4FB" stroke="#7FB2D6"/>
<text x="175" y="88" class="c sub b" style="fill:#444">节点 A</text>
<text x="175" y="103" class="c sub">投 B</text>
<rect x="260" y="70" width="130" height="40" rx="6" fill="#E6E3FA" stroke="#B8B0DE"/>
<text x="325" y="88" class="c sub b" style="fill:#444">节点 B</text>
<text x="325" y="103" class="c sub">候选人,2 票</text>
<rect x="410" y="70" width="130" height="40" rx="6" fill="#C6E4FB" stroke="#7FB2D6"/>
<text x="475" y="88" class="c sub b" style="fill:#444">节点 C</text>
<text x="475" y="103" class="c sub">投 D</text>
<rect x="560" y="70" width="130" height="40" rx="6" fill="#E6E3FA" stroke="#B8B0DE"/>
<text x="625" y="88" class="c sub b" style="fill:#444">节点 D</text>
<text x="625" y="103" class="c sub">候选人,2 票</text>
<text x="110" y="132" class="sub" style="">B、D 同时超时,各得 2 票,都不足 3 票(4 个节点的多数),没有 Leader</text>
<text x="30" y="202" class="b">任期 5</text>
<rect x="110" y="180" width="130" height="40" rx="6" fill="#C6E4FB" stroke="#7FB2D6"/>
<text x="175" y="198" class="c sub b" style="fill:#444">节点 A</text>
<text x="175" y="213" class="c sub">投 D</text>
<rect x="260" y="180" width="130" height="40" rx="6" fill="#C6E4FB" stroke="#7FB2D6"/>
<text x="325" y="198" class="c sub b" style="fill:#444">节点 B</text>
<text x="325" y="213" class="c sub">投 D</text>
<rect x="410" y="180" width="130" height="40" rx="6" fill="#C6E4FB" stroke="#7FB2D6"/>
<text x="475" y="198" class="c sub b" style="fill:#444">节点 C</text>
<text x="475" y="213" class="c sub">投 D</text>
<rect x="560" y="180" width="130" height="40" rx="6" fill="#C6ECE9" stroke="#7DBCB7"/>
<text x="625" y="198" class="c sub b" style="fill:#444">节点 D</text>
<text x="625" y="213" class="c sub">候选人,4 票</text>
<text x="110" y="242" class="sub" style="fill:#444">重新随机超时,D 先超时,得到全部选票,成为 Leader</text>
<text x="30" y="284" class="sub">淡紫色是候选人,浅青色是当选的 Leader。随机超时让两个节点再次同时超时的概率很低。</text>
</svg>

After

Width:  |  Height:  |  Size: 3.9 KiB

+56
View File
@@ -0,0 +1,56 @@
<svg xmlns="http://www.w3.org/2000/svg" width="760" height="300" viewBox="0 0 760 300">
<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">雪花算法:64 位 ID 的组成</text>
<rect x="30" y="70" width="34" height="36" rx="3" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1"/>
<text x="47" y="92" class="c sub mono" style="fill:#444">0</text>
<text x="47" y="62" class="c sub b" style="fill:#444">符号位</text>
<text x="47" y="124" class="c sub">1 位,固定为 0</text>
<rect x="66" y="70" width="434.6" height="36" rx="3" fill="#C6E4FB" stroke="#7FB2D6" stroke-width="1"/>
<text x="283.3" y="92" class="c sub mono" style="fill:#444">00001010010110100100001001111001110111101</text>
<text x="283.3" y="62" class="c sub b" style="fill:#444">时间戳</text>
<text x="283.3" y="124" class="c sub">41 位:距 2024-01-01 的毫秒数</text>
<rect x="502.6" y="70" width="106" height="36" rx="3" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1"/>
<text x="555.6" y="92" class="c sub mono" style="fill:#444">0000000101</text>
<text x="555.6" y="62" class="c sub b" style="fill:#444">机器 ID</text>
<text x="555.6" y="124" class="c sub">10 位:0~1023</text>
<rect x="610.6" y="70" width="127.2" height="36" rx="3" fill="#E6E3FA" stroke="#B8B0DE" stroke-width="1"/>
<text x="674.2" y="92" class="c sub mono" style="fill:#444">000000000000</text>
<text x="674.2" y="62" class="c sub b" style="fill:#444">序号</text>
<text x="674.2" y="124" class="c sub">12 位:同一毫秒内 0~4095</text>
<text x="30" y="164" class="sub b" style="fill:#444">本机生成的一个 ID:372990889737342976</text>
<text x="30" y="184" class="sub mono" style="fill:#444">时间戳 88927957949 ms → 2026-10-26 06:12:37.949(UTC),机器 ID 5,序号 0</text>
<text x="30" y="214" class="sub">41 位毫秒数可以用 69.7 年;每台机器每毫秒最多 4096 个,即每秒约 409 万个。</text>
<text x="30" y="232" class="sub">高位是时间,多台机器生成的 ID 整体上按时间递增,作为 InnoDB 主键时基本是顺序插入。</text>
<text x="30" y="256" class="sub note">需要注意的是,ID 依赖本机时钟:时钟回拨后可能生成重复的 ID,生成器发现当前时间小于上次的时间时,</text>
<text x="30" y="274" class="sub note">回拨很小就等待追上,较大就报错,由运维排查时钟同步。</text>
</svg>

After

Width:  |  Height:  |  Size: 3.8 KiB