MIT 6.5840:Lecture 12 Spanner
本篇逐行译注 MIT 6.5840 Spring 2026 Lecture 12,覆盖 Spanner 的 Paxos replication、distributed transactions、Snapshot Isolation、safe time、TrueTime 与 external consistency。
MIT 6.5840:Lecture 12 Spanner
TL;DR
- Spanner 的组织:data 按 key 分 shard,每个 shard 由一个 Paxos group replication;read/write transaction 使用 2PC,TC 和 participants 都可以由 Paxos 复制。
- Read/write transactions:通过 two-phase locking 保证 serializability;locks、跨 data center communication 和 synchronous disk writes 带来较高 latency。
- Read-only transactions:使用 Snapshot Isolation,不需要 locks、2PC 或 transaction manager,并且尽量从 local replica 读取。
- Safe time:replica 只有在确认已经看到目标 timestamp 之前的 writes 后,才能为该 timestamp 提供 read,避免 local replica stale。
- TrueTime 与 commit wait:用
[earliest, latest]的时间区间选择 timestamps;commit wait 保证 committed timestamp 已经在过去,从而实现 external consistency。
名词解释
| 名词 | 含义 | 本讲对应物 |
|---|---|---|
| Spanner | Google 的 globally distributed database | Paxos、2PC、Snapshot Isolation、TrueTime |
| Paxos group | 一个 shard 的 replicated group | 每个 shard 一个 group |
| Snapshot Isolation | 按 timestamp 读取 record 的版本 | read-only transaction 的主要机制 |
| Safe time | replica 已经安全追赶到的时间点 | 允许 local replica 提供新鲜 read |
| TrueTime | 返回 [earliest, latest] 的时间服务 | 给 timestamp 和 commit wait 提供边界 |
| External consistency | real-time 顺序与 serial order 一致 | Spanner 对外提供的 strict serializability |
| Commit wait | 等待 timestamp 确定已经过去 | 保证后启动 transaction 看到先完成 transaction |
目录
| 章节 | 内容 |
|---|---|
| 版本与来源 | 固定 Lecture 12 源文件 |
| 原文逐行翻译 | 按 l-spanner.txt 顺序译注 |
| 机制小结 | Spanner 的复制、事务与时间机制 |
| 本篇验收 | 源行、图示与术语边界记录 |
版本与来源
- 课程版本:MIT 6.5840 Spring 2026。
- Lecture 源文件:l-spanner.txt,330 行,非空源行 288 行,SHA-256:
22d1fdf3c5ea538607beac1d6a3b5c44947495d972811711508711f5d3795ffc。 - 课程日程:MIT 6.5840 Spring 2026 Schedule。
- 相关资料:Spanner: Becoming a SQL System、Spanner replication。
- 翻译边界:英文源行按原顺序保留,随后给出中文译文;Spanner、Paxos、2PC、Snapshot Isolation、TrueTime、timestamp、safe time、external consistency 和表/图编号原样保留。
- 图片结论:本 Lecture 源文件没有独立图片文件;
[diagram: ...]、Paxos/2PC 流程图、TrueTime ASCII 时间线和 Figure 引用均按原位置保留。
原文逐行翻译
Spanner 的目标与使用场景
6.5840 2026 Lecture 12: Spanner
6.5840 2026 年 Lecture 12:Spanner。
Why this paper (Google Spanner, OSDI 2012)?
为什么学习这篇论文(Google Spanner,OSDI 2012)?
Unusually ambitious for its time:
它在当时异常 ambitious:
Wide-area distributed transactions.
Wide-area distributed transactions。
Consistent wide-area replication.
Consistent wide-area replication。
Neat ideas:
其中有一些 neat ideas:
Two-phase commit over Paxos.
在 Paxos 之上运行 Two-phase commit。
Read/only transactions w/o locking, via Snapshot Isolation.
通过 Snapshot Isolation 实现不需要 locking 的 read/only transactions。
Read/only reads from local replica, but fresh.
Read/only 可以从 local replica 读取,但数据仍然是 fresh 的。
Synchronized time for external consistency.
用 synchronized time 实现 external consistency。
Spanner is used a lot inside Google.
Spanner 在 Google 内部被大量使用。
And influential, e.g. CockroachDB.
它也很有影响力,例如 CockroachDB。
What was the motivating use case?
最初的 motivating use case 是什么?
Google F1 advertising database (Section 5.4).
Google F1 advertising database(Section 5.4)。
Previously sharded over many MySQL and BigTable DBs; awkward.
此前 data 被 sharded 到许多 MySQL 和 BigTable DBs 上;使用起来很 awkward。
Needed:
它需要:
Synchronous replication over data centers (failure zones).
跨 data centers(failure zones)的 synchronous replication。
Cross-shard transactions.
Cross-shard transactions。
External consistency / linearizability / serializability.
External consistency / linearizability / serializability。
Automatic re-sharding.
Automatic re-sharding。
Workload is dominated by read-only transactions (Table 6).
Workload 主要由 read-only transactions 构成(Table 6)。
基本组织结构
The basic organization:
基本组织结构:
Datacenter A:
Datacenter A:
"clients" are web servers e.g. for gmail
“clients”是 web servers,例如 Gmail 的 web servers。
data is sharded over multiple servers:
data 被 sharded 到多个 servers 上:
a-m
a-m。
n-z
n-z。
Datacenter B:
Datacenter B:
has its own local clients
拥有自己的 local clients。
and its own copy of the data shards
并拥有自己的 data shards copy。
a-m
a-m。
n-z
n-z。
Datacenter C:
Datacenter C:
same setup
相同的 setup。
Replication managed by Paxos; one Paxos group per shard.
Replication 由 Paxos 管理;每个 shard 对应一个 Paxos group。
Paxos similar to Raft -- replicas, leader, quorum, log.
Paxos 与 Raft 类似——都有 replicas、leader、quorum、log。
Replicas in different data centers, hopefully fail independently.
Replicas 位于不同 data centers,希望它们能够 independent 地 failure。
Why this arrangement?
为什么采用这种 arrangement?
Sharding allows huge total throughput via parallelism.
Sharding 通过 parallelism 提供巨大的 total throughput。
Replicas at different data centers cope with whole-site failures.
位于不同 data centers 的 replicas 可以应对 whole-site failures。
Paxos requires only a majority -- tolerate slow/distant replicas.
Paxos 只需要 majority——可以容忍 slow/distant replicas。
Clients can read local replica -- fast!
Clients 可以读取 local replica——速度快!
What are the challenges?
挑战是什么?
Entirely local read-only transactions pose some problems:
完全 local 的 read-only transactions 会带来一些问题:
Can we have multi-record read-only transactions without locking?
能否在不 locking 的情况下进行 multi-record read-only transactions?
Data on local replica may not be fresh if not in Paxos majority.
如果 local replica 不在 Paxos majority 中,其中的 data 可能不 fresh。
A transaction may involve multiple shards -> multiple Paxos groups.
一个 transaction 可能涉及多个 shards → 多个 Paxos groups。
Read/write transactions
Spanner treats read/write and read/only transactions differently.
Spanner 对 read/write 和 read/only transactions 采用不同处理方式。
First, read/write transactions.
首先看 read/write transactions。
Example read/write transaction (bank transfer):
read/write transaction 示例(bank transfer):
BEGIN
BEGIN。
x = x + 1
x = x + 1。
y = y - 1
y = y - 1。
END
END。
We don't want any read or write of x or y sneaking between our two ops.
我们不希望对 x 或 y 的任何 read 或 write sneaking between our two ops。
After commit finishes, all reads should see our updates.
commit 完成后,所有 reads 都应该看到我们的 updates。
Summary: two-phase commit (2pc) with Paxos-replicated participants.
总结:使用 Paxos-replicated participants 的 two-phase commit(2pc)。
(Omitting timestamps for now.)
(暂时省略 timestamps。)
(This is for r/w transactions, not r/o.)
(这里针对的是 r/w transactions,不是 r/o。)
Diagram:
Diagram:
1. C sends Rx, Ry to A, B.
- C 向 A、B 发送
Rx、Ry。
2. A, B update lock tables.
- A、B 更新 lock tables。
3. C commits, sends Wx Wy to A and B.
- C commit,向 A 和 B 发送
Wx、Wy。
4. A, B log prepare.
- A、B log prepare。
5. A, B send "yes" to TC.
- A、B 向 TC 发送“yes”。
6. TC logs commit.
- TC log commit。
7. TC sends commit to A, B, C.
- TC 向 A、B、C 发送 commit。
8. A, B update tables, release locks.
- A、B 更新 tables,release locks。
Client sends each read to Paxos leader of relevant shard (2.1).
Client 将每个 read 发送给相关 shard 的 Paxos leader(2.1)。
Each shard first acquires a lock on the relevant record.
每个 shard 首先获取 relevant record 上的 lock。
May have to wait.
可能需要等待。
Separate lock table per shard, in shard leader.
每个 shard 有单独的 lock table,位于 shard leader 中。
Read locks are not replicated via Paxos, so leader failure -> abort.
Read locks 不通过 Paxos replication,因此 leader failure → abort。
Client keeps writes private until commit.
Client 在 commit 之前保持 writes private。
When client commits (4.2.1):
当 client commit 时(4.2.1):
Chooses a Paxos group to act as 2pc Transaction Coordinator (TC).
选择一个 Paxos group 充当 2pc Transaction Coordinator(TC)。
Sends writes to relevant shard leaders.
向相关 shard leaders 发送 writes。
Each written shard leader:
每个被写入的 shard leader:
Acquires/upgrades lock(s) on the written record(s).
获取/升级 written record(s) 上的 lock(s)。
Log a "prepare" record via Paxos, to replicate lock and new value.
通过 Paxos log 一条“prepare”record,以 replication lock 和 new value。
Tell TC it is prepared.
告诉 TC 自己已经 prepared。
Or tell TC "no" if crashed and lost lock table.
或者,如果 crash 并丢失 lock table,就告诉 TC “no”。
Transaction Coordinator:
Transaction Coordinator:
Decides commit or abort.
决定 commit 或 abort。
Logs the decision to its group via Paxos.
通过 Paxos 把 decision log 到自己的 group。
Tell participant leaders and client the result.
告诉 participant leaders 和 client result。
Each participant leader:
每个 participant leader:
Log the TC's decision via Paxos.
通过 Paxos log TC 的 decision。
Perform writes (values already logged during commit).
执行 writes(values 在 commit 期间已经 log)。
Release the transaction's locks.
释放 transaction 的 locks。
Read/write 设计的取舍
Some points about the design so far (just read/write transactions).
到目前为止关于该设计的一些要点(这里只讨论 read/write transactions)。
Locking (two-phase locking) ensures serializability.
Locking(two-phase locking)保证 serializability。
2pc widely hated b/c it blocks with locks held if TC fails.
2pc 广受诟病,因为如果 TC failure,它会在持有 locks 的情况下 blocking。
Replicating the TC with Paxos solves this problem!
使用 Paxos replication TC 可以解决这个问题!
r/w transactions take a long time.
r/w transactions 需要很长时间。
Inter-data-center messages, sync disk writes.
原因包括 inter-data-center messages、sync disk writes。
Tables 4 and 6 suggest 10ms to 100ms.
Tables 4 和 6 表明 latency 大约为 10ms 到 100ms。
But lots of parallelism: many clients, many shards.
但 parallelism 很强:many clients、many shards。
So total throughput can be high if busy.
因此在负载较高时 total throughput 可以很高。
Read-only transactions 与 Snapshot Isolation
Now for read-only (r/o) transactions.
现在看 read-only(r/o)transactions。
These can involve multiple reads, from multiple shards.
它们可能涉及来自多个 shards 的多个 reads。
Table 6 shows 99.9% of transactions are read-only!
Table 6 显示 99.9% 的 transactions 是 read-only!
So we want to make them fast, even at expense of r/w transactions.
因此即使牺牲 r/w transactions 的性能,也要让它们变快。
But still strictly serializable.
但仍然要 strictly serializable。
Spanner eliminates three big costs for r/o transactions:
Spanner 消除了 r/o transactions 的三个大成本:
No two-phase commit, no transaction manager, no locks (!).
不需要 two-phase commit、不需要 transaction manager,也不需要 locks(!)。
To avoid slow inter-data-center msg to Paxos leader.
这是为了避免发送到 Paxos leader 的 slow inter-data-center msg。
And to avoid forcing r/w transactions to wait via locks.
也为了避免通过 locks 强制 r/w transactions 等待。
Read from local replicas, again to avoid Paxos and cross-datacenter msgs.
从 local replicas 读取,同样是为了避免 Paxos 和 cross-datacenter msgs。
Note local replica may not be up to date!
注意 local replica 可能不是 up to date 的!
Tables 3 and 6 show r/o cleverness yields 10x lower latency than r/w!
Tables 3 和 6 显示,r/o 的 cleverness 让 latency 比 r/w 低 10 倍!
This is a big deal.
这是一个很大的优势。
How to be correct despite cutting these corners?
在做出这些取舍后,如何仍然保证 correctness?
Correctness constraints on r/o transactions:
r/o transactions 的 correctness constraints:
Serializable:
Serializable:
Same results as if transactions executed one-by-one.
结果要与 transactions one-by-one 执行时相同。
Even though they may actually execute concurrently.
即使它们实际上可能并发执行。
I.e. an r/o xaction must essentially fit between r/w xactions.
也就是说,一个 r/o xaction 必须实质上 fit between r/w xactions。
See all writes from prior transactions, nothing from subsequent.
它要看到 prior transactions 的所有 writes,看不到 subsequent transactions 的任何 writes。
Even though *concurrent* with r/w xactions! And not locking!
即使它与 r/w xactions concurrent!并且不使用 locking!
Externally consistent (== linearizable == "strict" serializability):
Externally consistent(== linearizable == “strict” serializability):
If T1 completes before T2 starts, T2 must see T1's writes.
如果 T1 在 T2 开始之前完成,T2 必须看到 T1 的 writes。
"Before" refers to real (wall-clock) time.
“Before”指 real(wall-clock)time。
Even though T2 doesn't know of T1, diff data ctr, no locks.
即使 T2 不知道 T1 的存在、位于不同 data ctr,且没有 locks。
Why not have r/o transactions just read the latest committed values?
为什么不让 r/o transactions 直接读取 latest committed values?
Suppose two bank transfers, and a transaction that reads both balances.
假设有两个 bank transfers,以及一个读取两个 balances 的 transaction。
T1: Wx Wy C
T1: Wx Wy C。
T2: Wx Wy C
T2: Wx Wy C。
T3: Rx Ry
T3: Rx Ry。
We want T3 to see both of T2's writes, or none.
我们希望 T3 要么看到 T2 的两个 writes,要么都看不到。
We want T3's reads to all occur at the same point relative to T1/T2.
我们希望 T3 的所有 reads 相对于 T1/T2 都在 同一个 point 发生。
(Locking would fix this; T3's Rx would force T2 to wait in Wx...)
(Locking 可以解决这个问题;T3 的 Rx 会迫使 T2 在 Wx 处等待……)
Idea: Snapshot Isolation (SI):
思路:Snapshot Isolation(SI):
DB stores multiple versions for each record.
DB 为每个 record 存储多个 versions。
Each labeled with a time -- called "time-stamp".
每个 version 都带有一个 time 标记——称为“time-stamp”。
A write adds a new version (doesn't overwrite).
write 添加一个 new version(不 overwrite)。
Assign every transaction a time-stamp.
为每个 transaction 分配一个 time-stamp。
All of a r/w transactions's writes get the xaction's time-stamp.
一个 r/w transaction 的所有 writes 都使用该 xaction 的 time-stamp。
An r/o transaction's reads see versions as of its time-stamp.
r/o transaction 的 reads 看到截至其 time-stamp 的 versions。
The record version with the highest time-stamp less than the xaction's.
也就是 time-stamp 小于该 xaction、且 time-stamp 最高的 record version。
Called Snapshot Isolation.
这称为 Snapshot Isolation。
Our example with Snapshot Isolation:
使用 Snapshot Isolation 后的例子:
x@10=9 x@20=8
x@10=9 x@20=8。
y@10=11 y@20=12
y@10=11 y@20=12。
T1 @ 10: Wx Wy C
T1 @ 10: Wx Wy C。
T2 @ 20: Wx Wy C
T2 @ 20: Wx Wy C。
T3 @ 15: Rx Ry
T3 @ 15: Rx Ry。
"@ 10" indicates the time-stamp.
“@ 10”表示 time-stamp。
Now T3's reads will both be served from the @10 versions.
现在 T3 的两个 reads 都会从 @10 versions 中得到服务。
T3 won't see T2's write even though T3's read of y occurs after T2.
即使 T3 对 y 的 read 发生在 T2 之后,T3 也看不到 T2 的 write。
Now the results are serializable: T1 T3 T2
现在 results 是 serializable 的:T1 T3 T2。
The same as if executed serially in time-stamp order.
这与按照 time-stamp order serially 执行相同。
Why OK for T3 to read the old value of y even though there's a newer value?
为什么 T3 读取 y 的 old value 是 OK 的,即使存在 newer value?
T2 and T3 are concurrent, so external consistency allows either order.
T2 和 T3 是 concurrent 的,因此 external consistency 允许任意一种 order。
Remember: r/o transactions need to read values
记住:r/o transactions 需要读取
as of their timestamp, and *not* see later writes.
截至其 timestamp 的 values,不能看到 later writes。
Nice:
好处:
T3 can get transactional (serializable) reads without locking!
T3 可以在不 locking 的情况下获得 transactional(serializable)reads!
Reduces communication and blocking of r/w transactions.
这减少了 communication,也减少了对 r/w transactions 的 blocking。
The cost (so far) is storing multiple versions for Snapshot Isolation.
目前的代价是:为了 Snapshot Isolation 存储 multiple versions。
Safe time
Problem: what if T3 reads x from a replica that hasn't seen T1's write?
问题:如果 T3 从一个尚未看到 T1 write 的 replica 读取 x 呢?
Because the replica wasn't in the Paxos majority?
因为该 replica 不在 Paxos majority 中?
Solution: replica "safe time".
解决方案:replica 的“safe time”。
Paxos leaders send writes in timestamp order.
Paxos leaders 按 timestamp order 发送 writes。
Before serving a read at time 20, replica must see Paxos write for time > 20.
在为 time 20 的 read 提供服务之前,replica 必须看到 time > 20 的 Paxos write。
So it knows it has seen all writes < 20.
这样它就知道自己已经看到所有 < 20 的 writes。
So the replica may have to delay its response to a read.
因此 replica 可能必须 delay 对 read 的 response。
Must also delay if prepared but uncommitted r/w transactions (Section 4.1.3).
如果存在 prepared 但未 committed 的 r/w transactions,也必须 delay(Section 4.1.3)。
Thus: r/o transactions can read from local replica -- usually fast.
因此:r/o transactions 可以从 local replica 读取——通常很快。
Clocks 与 external consistency
Problem: what if clocks are not correct?
问题:如果 clocks 不正确怎么办?
How to ensure external consistency for r/o transactions?
如何保证 r/o transactions 的 external consistency?
What goes wrong if clocks aren't correct?
如果 clocks 不正确,会出什么问题?
R/O TS too large:
R/O TS too large:
Its TS will be higher than replica safe times, and reads will block.
它的 TS 会高于 replica safe times,reads 会 block。
Correct but slow -- delay increased by amount of clock error.
这是 correct 但 slow——delay 会因 clock error 而增加。
R/O TS too small:
R/O TS too small:
It will miss writes that committed before the r/o xaction started.
它会漏掉在 r/o xaction 开始前已经 committed 的 writes。
Since its low TS will cause it to use old versions of records.
因为低 TS 会让它使用 records 的 old versions。
This violates external consistency!
这违反 external consistency!
R/W TS too large:
R/W TS too large:
Old writes may seem to r/o like latest data.
Old writes 对 r/o 来说可能看起来像 latest data。
R/W TS too small:
R/W TS too small:
New writes may seem old to r/o, and ignored.
New writes 对 r/o 来说可能看起来很 old,并被忽略。
Example of problem if r/o xaction's TS is too small:
r/o xaction 的 TS 太小时的问题示例:
r/w T1 @ 10: Wx2 C
r/w T1 @ 10: Wx2 C。
r/o T2 @ 5: Rx?
r/o T2 @ 5: Rx?。
(C for commit)
(C 表示 commit。)
This would cause T2 to read the version of x before T1.
这会导致 T2 读取 T1 之前的 x version。
But T2 started after T1 committed (in real time),
但 T2 在 T1 committed 之后才开始(按 real time),
so external consistency requires that T2 see x=2.
因此 external consistency 要求 T2 看到 x=2。
So we need a way to deal with incorrect clocks!
所以我们需要一种处理 incorrect clocks 的方法!
What goes wrong with computer clocks?
computer clocks 会有什么问题?
The absolute time is never quite right -- offset.
absolute time 从来不会完全正确——存在 offset。
Tick rate varies, never exactly right -- drift.
tick rate 会变化,从来不会完全正确——存在 drift。
You can ask a better clock what time it it.
可以向一个 better clock 询问当前时间。
Correct your offset, measure and compensate for drift.
修正 offset,测量并补偿 drift。
But comparisons have error due to variable communication delays.
但由于 communication delays 可变,comparisons 仍然存在 error。
TrueTime
Google's time reference system (Section 5.3)
Google 的 time reference system(Section 5.3)。
[UTC(USNO), GPS satellites, masters, servers]
[UTC(USNO)、GPS satellites、masters、servers]。
A tree, periodic network queries.
一个 tree,进行 periodic network queries。
Uncertainty due to network delays, drift between queries.
不确定性来自 network delays,以及两次 queries 之间的 drift。
TrueTime
TrueTime。
Time service yields a TTinterval = [ earliest, latest ].
Time service 返回一个 TTinterval = [ earliest, latest ]。
The correct time is somewhere in the interval, with high probability.
正确时间以很高概率位于该 interval 中的某处。
Interval width computed from measured network delays,
Interval width 根据测得的 network delays、
measured clock drift, time since last sync.
测得的 clock drift,以及距上次 sync 的时间计算。
Figure 6: intervals are usually < 1 millisecond, but sometimes 10+ ms.
Figure 6:intervals 通常 < 1 millisecond,但有时会达到 10+ ms。
So: server clocks aren't exactly synchronized.
因此:server clocks 并没有 exact synchronized。
But some useful guarantees:
但它提供了一些有用的 guarantees:
TS > latest is guaranteed to be in the future.
TS > latest 时,可以保证 TS 在 future。
TS < earliest is guaranteed to be in the past.
TS < earliest 时,可以保证 TS 在 past。
Spanner 的 timestamp 与 commit wait
How Spanner ensures that if r/w T1 finishes before r/o T2 starts, TS2 > TS1.
Spanner 如何保证 r/w T1 在 r/o T2 开始前完成时,TS2 > TS1?
So that T2's snapshot reads see T1's writes.
这样 T2 的 snapshot reads 就能看到 T1 的 writes。
And are thus externally consistent.
因此它们是 externally consistent 的。
Two rules for r/w transactions (4.1.2):
r/w transactions 的两条规则(4.1.2):
Start rule:
Start rule:
TS = TT.now().latest, when commit begins
commit 开始时,TS = TT.now().latest。
this is the time-stamp it will write in the DB records
这就是它写入 DB records 的 time-stamp。
Commit wait:
Commit wait:
Before releasing locks or replying to client,
在 releasing locks 或 reply 给 client 之前,
delay until TS < TT.now().earliest
delay,直到 TS < TT.now().earliest。
Guarantees that TS *is* in the past.
这保证 TS 已经位于 past。
Rule for r/o transctions (4.1.4, 4.2.2):
r/o transctions 的规则(4.1.4、4.2.2):
TS = TT.now().latest
TS = TT.now().latest。
and read that version (really, version with highest timestamp < TS)
读取该 version(严格来说,是 timestamp < TS 且 timestamp 最高的 version)。
TS guaranteed not to be in the past
可以保证 TS 不在 past。
Example updated with intervals and commit wait:
加入 intervals 和 commit wait 后的例子:
The scenario is T1 finishes, then T2 starts, T2 must see T1's writes.
场景是 T1 finish,然后 T2 start,T2 必须看到 T1 的 writes。
I.e. we need TS1 < TS2.
也就是说,我们需要 TS1 < TS2。
TS1=10
TS1=10。
[1,10] [11,20]
[1,10] [11,20]。
r/w T1: C..........W
r/w T1: C..........W。
TS2=15
TS2=15。
[5,15]
[5,15]。
r/o T2: Rx
r/o T2: Rx。
"C" for start of commit, "W" for end of commit wait
“C”表示 start of commit,“W”表示 end of commit wait。
Remember, the assumption is that T2 starts after T1 finishes,
记住,假设是 T2 在 T1 finish 后才 start,
so that T2 needs to see T1's writes.
所以 T2 需要看到 T1 的 writes。
Why commit wait provides external consistency for r/o transactions:
为什么 commit wait 能为 r/o transactions 提供 external consistency:
Given that T1 finishes before T2 starts.
给定 T1 在 T2 start 之前 finish。
Commit wait means TS1 is guaranteed to be in the past.
Commit wait 意味着可以保证 TS1 在 past。
After commit wait:
commit wait 之后:
TS1 guaranteed to be in the past.
TS1 可以保证在 past。
TS2 = TT.now().latest guaranteed to be in the future.
TS2 = TT.now().latest 可以保证在 future。
So TS2 > TS1.
所以 TS2 > TS1。
So T2 will see T1's writes.
所以 T2 会看到 T1 的 writes。
Why it matters:
为什么这很重要:
I change my password, wait for finish, then log in via another data ctr.
我修改 password,等待完成,然后通过另一个 data ctr 登录。
External consistency guarantees I'll encounter my new password.
External consistency 保证我会遇到 new password。
Why does an r/w transaction use TS = TT.now().latest?
为什么 r/w transaction 使用 TS = TT.now().latest?
(above reasoning allows any TS that commit-wait forces into the past)
(上面的 reasoning 允许任何能被 commit-wait 强制置于 past 的 TS。)
Suppose sequential r/w transactions: T3, and afterwards T4.
假设有 sequential r/w transactions:T3,之后是 T4。
We must have TS3 < TS4 (so that an r/o that sees T4 also sees T3).
我们必须有 TS3 < TS4(这样看到 T4 的 r/o 也能看到 T3)。
The same reasoning as for r/o will cause TS4 to be greater than TS3.
与 r/o 相同的 reasoning 会使 TS4 大于 TS3。
So all r/w transactions use TS = TT.now().latest.
因此所有 r/w transactions 都使用 TS = TT.now().latest。
总结 Spanner 的机制
More generally:
更一般地说:
Snapshot Isolation gives you serializable r/o transactions.
Snapshot Isolation 为你提供 serializable r/o transactions。
Timestamps set an order.
Timestamps 建立一个 order。
Snapshot versions implement consistent reads at a timestamp.
Snapshot versions 实现在某个 timestamp 上的 consistent reads。
Xaction sees all writes from lower-TS xactions, none from higher.
Xaction 看到 lower-TS xactions 的所有 writes,看不到 higher-TS xactions 的 writes。
Synchronized time + commit-wait yields external consistency.
Synchronized time + commit-wait 产生 external consistency。
Even among transactions at different data centers.
即使 transactions 位于不同 data centers 也是如此。
Safe time allows reading from local replicas that might lag.
Safe time 允许从可能 lag 的 local replicas 读取。
Why is all this useful?
这一切为什么有用?
Fast r/o transactions:
快速的 r/o transactions:
Read from local replicas!
从 local replicas 读取!
No locks!
不需要 locks!
Thus the 10x latency r/o vs r/w improvement in Table 3 (and Table 6).
因此 Table 3(以及 Table 6)中 r/o 相对 r/w 获得了 10 倍 latency improvement。
Although:
不过:
r/o transaction reads may pause due to safe time, to catch up.
r/o transaction reads 可能因为 safe time 而 pause,以等待追赶进度。
r/w transaction commits pause in Commit Wait.
r/w transaction commits 会在 Commit Wait 中 pause。
Accurate (small interval) TrueTime minimizes these delays.
准确的(small interval)TrueTime 会最小化这些 delays。
(This is The Question)
(这就是 The Question。)
Summary:
总结:
Rare to see deployed systems offer distributed transactions
很少有 deployed systems 提供 distributed transactions,
over geographically distributed data.
并且这些 data geographically distributed。
Spanner was a surprising demonstration that it can be practical.
Spanner 出人意料地证明了这可以 practical。
Timestamping scheme is the most interesting aspect.
Timestamping scheme 是最有趣的 aspect。
Widely used within Google; a commercial Google service; influential.
它在 Google 内部广泛使用;是 Google 的 commercial service;也很 influential。
Next:
接下来:
Chain replication: simpler than Raft/Paxos.
Chain replication:比 Raft/Paxos 更简单。
Spanner: Becoming a SQL System, 2017, https://research.google/pubs/pub46103.pdf
Spanner: Becoming a SQL System,2017,https://research.google/pubs/pub46103.pdf
https://www.cockroachlabs.com/blog/living-without-atomic-clocks/
https://www.cockroachlabs.com/blog/living-without-atomic-clocks/
https://engineering.fb.com/2020/03/18/production-engineering/ntp-service/
https://engineering.fb.com/2020/03/18/production-engineering/ntp-service/
https://sookocheff.com/post/time/truetime/
https://sookocheff.com/post/time/truetime/
https://communities.actian.com/s/article/Using-MVCC-Multi-Version-Concurrency-Control
https://communities.actian.com/s/article/Using-MVCC-Multi-Version-Concurrency-Control
https://cloud.google.com/spanner/docs/replication
https://cloud.google.com/spanner/docs/replication
机制小结
1. Paxos + 2PC 的 read/write path
Spanner 为每个 shard 建立 Paxos group。client 的 reads/writes 先到相关 shard leader;写入 transaction 在 commit 时选择一个 Paxos group 作为 TC,各 participant 通过 Paxos 持久化 prepare 和新值,TC 再持久化 commit decision,最后 participants 执行 writes 并释放 locks。
2. Snapshot Isolation 的 read-only path
每个 record 保留带 timestamp 的多个 versions。r/o transaction 选择一个 timestamp,所有 reads 都读取小于该 timestamp 的最高版本,因此多个 shards 上的 reads 能形成一个一致 snapshot,不需要 locks、2PC 或 transaction manager。
3. Safe time 解决 replica lag
local replica 不能仅凭“本地已有某个版本”就立即服务任意 timestamp 的 read。它必须确认自己已经通过 Paxos 看到足够靠后的 writes,才能知道目标 timestamp 之前的 writes 都已到达;如果未达到 safe time,就延迟 read。
4. TrueTime + commit wait
TrueTime 不返回一个看似精确的时间点,而返回包含真实时间的 [earliest, latest] interval。r/w transaction 在 commit 开始时取 latest 作为 TS,并在释放 locks 或回复 client 前等待到 TS 确定已经过去;之后启动的 r/o transaction 取新的 latest,即可得到更大的 timestamp,满足 external consistency。
本篇验收
- [x] 官方
l-spanner.txt已固定:330 行、288 个非空源行,SHA-256 与来源记录一致。 - [x] 源文本的非空行按原顺序逐行保留,中文译文紧随对应英文源行。
- [x] Spanner 的 shard/Paxos 组织、read/write 2PC、read-only Snapshot Isolation、safe time、clock error、TrueTime 和 commit wait 全部覆盖。
- [x] 所有
[diagram: ...]、Paxos/2PC 流程图、时间线、Figure 引用和参考 URL 均按原位置保留;源文件没有独立图片。 - [x] Spanner、Paxos、2PC、SI、timestamp、safe time、TrueTime、external consistency 和表/图编号保留原样。
- [ ] Lecture 12 的中文译文仍建议进行一次人工术语审校;自动审计只验证源文覆盖与顺序,不证明翻译语义正确。
评论 (0)