MIT 6.5840:Lecture 11 Distributed Transactions
本篇逐行译注 MIT 6.5840 Spring 2026 Lecture 11,覆盖 distributed transactions、serializability、ACID、concurrency control、two-phase locking(2PL)、two-phase commit(2PC)以及 Raft 与 2PC 的区别。
MIT 6.5840:Lecture 11 Distributed Transactions
TL;DR
- Distributed transactions:把 concurrency control 和 atomic commit 结合起来,让跨 shard 的多步读写表现得像一个可靠的 transaction。
- Serializable:并发 execution 的结果必须等价于某个 serial order 的结果;不要求内部真的串行执行。
- 2PL:事务在使用 record 前获得 lock,并一直持有到 commit/abort 之后;它能提供 serializability,但可能 deadlock。
- 2PC:participants 先 PREPARE,全部 YES 后由 transaction coordinator(TC)发送 COMMIT;任何 NO 都导致 ABORT。
- 2PC 的核心代价:participant 在 YES 后无法单方面决定结果,必须等待 TC;TC crash 可能导致事务长时间 blocking 且 locks 仍被持有。
- Raft 与 2PC:Raft 复制相同 computation 以获得 availability;2PC 让执行不同工作的 participants 达成 atomic commit,两者解决不同问题。
名词解释
| 名词 | 含义 | 本讲对应物 |
|---|---|---|
| Transaction | 一组应当作为整体执行的读写 operations | bank transfer、audit |
| ACID | Atomic、Consistent、Isolated、Durable | transaction 的经典语义保证 |
| Serializable | 并发结果等价于某个 serial execution | concurrency control 的目标 |
| 2PL | Two-Phase Locking | 按需加锁,commit/abort 后释放 |
| TC | Transaction Coordinator | 协调 2PC 的全局决定 |
| Participant | 参与 transaction 的 shard server | 管理本 shard 的 locks 和 tentative updates |
| 2PC | Two-Phase Commit | PREPARE/COMMIT 或 ABORT |
| Prepared | participant 已投 YES,等待 TC 决定 | 不能单方面 abort 或 commit |
目录
| 章节 | 内容 |
|---|---|
| 版本与来源 | 固定 Lecture 11 源文件 |
| 原文逐行翻译 | 按 l-2pc.txt 顺序译注 |
| 机制小结 | serializability、2PL、2PC 和 Raft 的边界 |
| 本篇验收 | 源行、图示和协议状态记录 |
版本与来源
- 课程版本:MIT 6.5840 Spring 2026。
- Lecture 源文件:l-2pc.txt,333 行,非空源行 285 行,SHA-256:
134bdfed5265fce4ec37f7688fee7315fdcfab35c9fb87400df617008058c81c。 - 课程日程:MIT 6.5840 Spring 2026 Schedule。
- 翻译边界:英文源行按原顺序保留,随后给出中文译文;transaction、ACID、serializable、2PL、2PC、TC、TID、PREPARE、COMMIT、ABORT、ACK 和 Raft 原样保留。
- 图片结论:本 Lecture 源文件没有独立图片文件;
[diagram: ...]、transaction time diagram、state diagram 和最后的[diagram]均按原位置保留。
原文逐行翻译
Distributed transactions 的问题
6.5840 2026 Lecture 11: Distributed Transactions
6.5840 2026 年 Lecture 11:Distributed Transactions。
Topics:
主题:
distributed transactions = concurrency control + atomic commit
distributed transactions = concurrency control + atomic commit。
where are we in the course?
我们目前学到课程的哪里了?
so far, mostly distribution for fault tolerance
到目前为止,主要讨论的是为了 fault tolerance 而进行 distribution。
multiple servers trying to look like one reliable server
多个 servers 尝试表现得像一个可靠的 server。
now, many servers for performance
现在转向为了 performance 而使用 many servers。
[diagram: clients, servers, data sharded by key]
[diagram:clients、servers、按 key sharded 的 data]。
split the data up (shard) over multiple servers, for parallelism
为了 parallelism,把 data 拆分(shard)到多个 servers 上。
fine as long as clients use data items one at a time
只要 clients 一次使用一个 data item,这样就没问题。
what if an application operation involves records in different shards?
但如果一个 application operation 涉及不同 shards 中的 records 呢?
failures? atomicity?
failures?atomicity?
important problem, we'll see a number of approaches
这是一个重要问题;我们会看到多种 approaches。
client application actions often involve multiple reads and writes
client application 的 actions 经常涉及多个 reads 和 writes。
bank transfer: debit and credit
bank transfer:debit 和 credit。
install bi-directional links in a social graph
在 social graph 中安装 bi-directional links。
insert new record, add to index
插入 new record,并把它加入 index。
we'd like to hide interleaving and failure from application writers
我们希望对 application writers 隐藏 interleaving 和 failure。
this is an old problem in databases
这是 databases 中的一个 old problem。
the traditional solution: transactions
传统 solution:transactions。
programmer marks beginning/end of sequences of code as transactions
programmer 把一段代码 sequence 的 beginning/end 标记为 transaction。
the system automatically provides good behavior
system 自动提供良好的 behavior。
Transaction 示例与 ACID
example transactions
transaction 示例。
x and y are bank balances -- records in database tables
x 和 y 是 bank balances——database tables 中的 records。
both start out as $10
两者开始时都是 10 美元。
T1 and T2 are transactions
T1 和 T2 是 transactions。
T1: transfer $1 from y to x
T1:把 1 美元从 y 转给 x。
T2: audit, to find the total amount of money in the bank
T2:执行 audit,找出银行中的 money 总量。
T1: T2:
T1: T2:。
BEGIN-X BEGIN-X
BEGIN-X BEGIN-X。
add(x, 1) tmp1 = get(x)
add(x, 1) tmp1 = get(x)。
add(y, -1) tmp2 = get(y)
add(y, -1) tmp2 = get(y)。
END-X print tmp1, tmp2
END-X print tmp1, tmp2。
END-X
END-X。
the "END-X" indicates that the transaction would like to commit
“END-X”表示 transaction 希望 commit。
it asks the transaction system to attempt to commit
它请求 transaction system 尝试 commit。
as we'll see, the commit may succeed, or it may fail
正如我们将看到的,commit 可能成功,也可能失败。
what is correct behavior for a transaction?
transaction 的正确 behavior 是什么?
usually called "ACID"
通常称为“ACID”。
Atomic -- all writes or none, despite failures
Atomic——即使发生 failures,也要么所有 writes 生效,要么一个都不生效。
Consistent -- obeys application-specific invariants
Consistent——遵守 application-specific invariants。
Isolated -- no interference between xactions -- serializable
Isolated——xactions 之间没有 interference——serializable。
Durable -- committed writes are permanent
Durable——committed writes 是 permanent 的。
ACID transactions are magic!
ACID transactions 就像 magic!
programmer writes straightforward serial code
programmer 编写 straightforward serial code。
system automatically adds locking!
system 自动加入 locking!
system automatically adds fault tolerance!
system 自动加入 fault tolerance!
of course we need to implement this magic
当然,我们需要实现这种 magic。
a note on trends
关于 trends 的说明。
some storage systems provide transactions, some don't
一些 storage systems 提供 transactions,另一些不提供。
some applications benefit a lot from transactions, some don't
一些 applications 从 transactions 中获益很多,另一些则不是。
SQL databases provide transactions
SQL databases 提供 transactions。
but transactions are slow, particularly for sharded data
但 transactions 很慢,尤其是对于 sharded data。
so for a while simple key/value stores gained popularity
所以有一段时间,simple key/value stores 变得流行。
just put and get on single records
它们只对 single records 执行 put 和 get。
but transactions are coming back
但 transactions 正在回归。
today: ACID for distributed transactions
今天:为 distributed transactions 提供 ACID。
with data sharded over multiple servers
其中 data 被 sharded 到多个 servers 上。
Serializable 的含义
What does serializable mean?
serializable 是什么意思?
you execute some concurrent transactions, which yield results
你执行一些 concurrent transactions,并得到 results。
"results" means both output and changes in the DB
“results”同时指 output 和 DB 中的 changes。
the results are serializable if:
如果满足以下条件,results 就是 serializable 的:
there exists a serial execution order of the transactions
存在一个 transactions 的 serial execution order,
that yields the same results as the actual execution
它产生的 results 与 actual execution 相同。
(serial means one at a time; wait for one to finish before starting the next)
(serial 表示一次执行一个;等待一个完成后再开始下一个。)
(this definition should remind you of linearizability)
(这个定义应该会让你想起 linearizability。)
You can test whether an execution's result is serializable by
可以通过以下方式测试 execution 的 result 是否 serializable:
looking for a serial order that yields the same results.
寻找一个能产生相同 results 的 serial order。
for our example, the possible serial orders are
在我们的例子中,可能的 serial orders 是:
T1; T2
T1; T2。
T2; T1
T2; T1。
so the correct (serializable) results are:
因此,正确的(serializable)results 是:
T1; T2 : x=11 y=9 "11,9"
T1; T2 : x=11 y=9 "11,9"。
T2; T1 : x=11 y=9 "10,10"
T2; T1 : x=11 y=9 "10,10"。
the results for the two differ; either is OK
两种 results 不同;任意一种都 OK。
no other result is OK for a serializable system
对 serializable system 来说,其他 result 都不 OK。
the implementation might have executed T1 and T2 in parallel
implementation 可能并行执行了 T1 和 T2,
but it must still yield results as if in a serial order
但它仍然必须产生仿佛按 serial order 执行的 results。
what if T1's operations run entirely between T2's two get()s?
如果 T1 的 operations 完全运行在 T2 的两个 get()s 之间呢?
would the result be serializable?
result 会是 serializable 的吗?
T2 would print 10,9
T2 会打印 10,9。
but 10,9 is not one of the two serializable results!
但 10,9 不属于两个 serializable results 中的任何一个!
what if T2 runs entirely between T1's two adds()s?
如果 T2 完全运行在 T1 的两个 adds() 之间呢?
T2 would print 11,10
T2 会打印 11,10。
but 11,10 is not one of the two serializable results!
但 11,10 不属于两个 serializable results 中的任何一个!
what if x's server does the increment but y's server can't?
如果 x 的 server 完成了 increment,但 y 的 server 无法完成呢?
x=11 y=10 is not one of the serializable results!
x=11 y=10 不属于 serializable results!
Abort 与 application programmer
a transaction can "abort" if something goes wrong
如果发生问题,一个 transaction 可以“abort”。
an abort un-does any modifications
abort 会撤销所有 modifications。
the transaction might voluntarily abort,
transaction 可能主动 abort,
e.g. if the account doesn't exist, or y's balance is <= 0
例如 account 不存在,或者 y 的 balance <= 0。
the system may force an abort, e.g. to break a locking deadlock
system 也可能强制 abort,例如为了打破 locking deadlock。
server failure can result in abort
server failure 可能导致 abort。
result of abort should be as if xaction never executed!!!
abort 的 result 应当仿佛 xaction 从未执行过!!!
must un-do, or not apply, all updates
必须撤销或不应用所有 updates。
the application might (or might not) try the transaction again
application 可能会(也可能不会)再次尝试该 transaction。
serializable transactions are nice for application programmers
serializable transactions 对 application programmers 很友好。
write transaction as if there is nothing else going on
可以像系统中没有其他事情发生一样编写 transaction。
ordinary serial code, programmer doesn't need to worry about locks
它就是普通的 serial code,programmer 不需要担心 locks。
Concurrency control
now: implementing distributed transactions
现在:实现 distributed transactions。
two main components:
两个主要 components:
concurrency control (to provide isolation/serializability)
concurrency control(提供 isolation/serializability)。
atomic commit (to provide atomicity despite failure)
atomic commit(尽管发生 failure 仍提供 atomicity)。
first, concurrency control
首先是 concurrency control。
the goal: isolated/serializable execution of concurrent transactions
目标:对 concurrent transactions 进行 isolated/serializable execution。
for now, on a single DB server (not distributed)
目前先在单个 DB server 上讨论(不是 distributed 的)。
two classes of concurrency control for transactions:
transactions 有两类 concurrency control:
pessimistic:
pessimistic:
lock records before use
在使用 records 之前先 lock。
conflicts cause delays (waiting for locks)
conflicts 会导致 delays(等待 locks)。
optimistic:
optimistic:
use records without locking
不加锁地使用 records。
commit checks if reads/writes were serializable
commit 时检查 reads/writes 是否 serializable。
conflict causes abort+retry
conflict 会导致 abort+retry。
called Optimistic Concurrency Control (OCC)
称为 Optimistic Concurrency Control(OCC)。
pessimistic is faster if conflicts are frequent
conflicts frequent 时 pessimistic 更快。
optimistic is faster if conflicts are rare
conflicts rare 时 optimistic 更快。
today: pessimistic concurrency control
今天:pessimistic concurrency control。
in a few weeks: optimistic concurrency control (FaRM)
几周后:optimistic concurrency control(FaRM)。
Two-Phase Locking
"Two-phase locking" is one way to implement serializability
“Two-phase locking”是实现 serializability 的一种方式。
each database record has a lock
每个 database record 都有一个 lock。
2PL rules:
2PL rules:
a transaction must acquire a record's lock before using it
transaction 必须在使用 record 之前获得它的 lock。
a transaction must hold its locks until *after* commit or abort
transaction 必须一直持有 locks,直到 commit 或 abort 之后。
2PL for our example
对我们例子的 2PL。
suppose T1 and T2 start at the same time
假设 T1 和 T2 同时开始。
the transaction system automatically acquires locks as needed
transaction system 按需自动获取 locks。
so first of T1/T2 to use x will get the lock
因此 T1/T2 中第一个使用 x 的 transaction 会获得 lock。
the other waits until the first completely finishes (reaches END-X)
另一个会等待,直到第一个完全完成(到达 END-X)。
this prohibits the non-serializable interleavings
这会禁止 non-serializable interleavings。
details:
细节:
an executing transaction acquires locks as needed, at the first use
执行中的 transaction 在第一次使用时按需获取 locks。
add() and get() implicitly acquire record's lock
add() 和 get() 会隐式获取 record 的 lock。
END-X commits or aborts, then releases all locks
END-X commit 或 abort,然后释放所有 locks。
all locks are exclusive (for this discussion, no reader/writer locks)
所有 locks 都是 exclusive 的(在本讨论中没有 reader/writer locks)。
the full name is "strong strict two-phase locking"
完整名称是“strong strict two-phase locking”。
strong strict = hold locks until after commit
strong strict = 持有 locks 直到 commit 之后。
much more structured than e.g. Go's Mutexes:
它比例如 Go 的 Mutexes 更 structured:
programmer must supply BEGIN-X/END-X
programmer 必须提供 BEGIN-X/END-X。
DB locks automatically, on first use of each record
DB 在每个 record 第一次被使用时自动加 lock。
DB unlocks automatically, at transaction end
DB 在 transaction end 自动 unlock。
DB may automatically abort to resolve deadlock
DB 可能自动 abort 以解决 deadlock。
(consider: could Go mutexes usefully be automatic like this?)
(思考:Go mutexes 是否也能有用地自动完成这些工作?)
Why hold locks until after commit/abort?
为什么要持有 locks 直到 commit/abort 之后?
why not release as soon as done with the record?
为什么不在 record 使用完后立即 release?
example of a resulting problem:
由此产生的问题示例:
suppose T2 releases x's lock after get(x)
假设 T2 在 get(x) 后 release x 的 lock。
T1 could then execute between T2's get()s
那么 T1 就可能在 T2 的两个 get() 之间执行。
T2 would print 10,9
T2 会打印 10,9。
not a serializable execution: neither T1;T2 nor T2;T1
这不是 serializable execution:既不是 T1;T2,也不是 T2;T1。
Two-phase locking can produce deadlock, e.g.
Two-phase locking 可能产生 deadlock,例如:
T3 T4
T3 T4。
get(x) get(y)
get(x) get(y)。
get(y) get(x)
get(y) get(x)。
The system must resolve deadlocks, usually by aborting
system 必须解决 deadlocks,通常通过 aborting。
Detect waits-for cycles -- maybe too hard when locks are distributed
Detect waits-for cycles——当 locks 是 distributed 时可能太困难。
Time out
Time out。
Wound-wait (pre-emptively abort newer xaction rather than waiting)
Wound-wait(抢先 abort newer xaction,而不是等待)。
Performance with 2PL
使用 2PL 时的 performance。
good: parallelism for transactions that use disjoint records
好处:使用 disjoint records 的 transactions 可以并行。
bad: locking has overhead even if lock is available
坏处:即使 lock 可用,locking 仍有 overhead。
bad: locking conflicts require waiting
坏处:locking conflicts 需要等待。
The Question: describe a situation where Two-Phase Locking yields
问题:描述一种 Two-Phase Locking 能够带来
higher performance than Simple Locking. Simple locking: lock every
比 Simple Locking 更高 performance 的情况。Simple locking:在任何
record before any use; release after abort/commit.
使用之前先 lock 每个 record;abort/commit 后 release。
Distributed transactions 与 failure
Next topic: distributed transactions versus failures
下一个主题:distributed transactions 与 failures。
how can distributed transactions cope with failures?
distributed transactions 如何应对 failures?
suppose, for our example, x and y are on different storage servers
以我们的例子为例,假设 x 和 y 位于不同的 storage servers 上。
suppose x's server adds 1, but y's crashes before subtracting?
假设 x 的 server 加了 1,但 y 的 server 在 subtracting 之前 crash 了?
or x's server adds 1, but y's realizes the account doesn't exist?
或者 x 的 server 加了 1,但 y 的 server 发现 account 不存在?
or x and y both can do their part, but aren't sure if the other will?
或者 x 和 y 都能完成自己的部分,但不确定对方是否也能完成?
it's a hard problem!
这是一个 hard problem!
We want "atomic commit":
我们想要“atomic commit”:
A bunch of computers are cooperating on some task
一组 computers 协作完成某项 task。
Each computer has a different role
每台 computer 都有不同 role。
We want atomicity: all execute, or none execute
我们希望有 atomicity:要么全部 execute,要么一个都不 execute。
Challenges: failures, performance
挑战:failures、performance。
We're going to look at a protocol called "two-phase commit"
我们将研究一种称为“two-phase commit”的 protocol。
Used by distributed databases for multi-server transactions
distributed databases 用它处理 multi-server transactions。
Two-Phase Commit 的 setting
The setting
场景设定。
Data is sharded among multiple servers
Data 被 sharded 到多个 servers 之间。
Each transaction runs on a "transaction coordinator" (TC)
每个 transaction 运行在一个“transaction coordinator”(TC)上。
For each read/write, TC sends RPC to relevant shard server
每个 read/write 都由 TC 向相关 shard server 发送 RPC。
Each shard server is a "participant"
每个 shard server 都是一个“participant”。
Each participant manages locks for its shard of the data
每个 participant 管理自己负责的 data shard 的 locks。
There may be many concurrent transactions, many TCs
系统中可能有许多 concurrent transactions 和许多 TCs。
TC assigns unique transaction ID (TID) to each transaction
TC 为每个 transaction 分配唯一的 transaction ID(TID)。
Every message, every piece of xaction state tagged with TID
每条 message、每一片 xaction state 都带有 TID tag。
To avoid confusion
这样可以避免 confusion。
没有 failure 的 2PC
Two-phase commit without failures:
没有 failures 时的 two-phase commit:
[time diagram: A, TC, B]
[time diagram:A、TC、B]。
[state (just for B): DATA, TID LOCKS TEMP PREPARED]
[state(仅针对 B):DATA、TID、LOCKS、TEMP、PREPARED]。
TC sends put(), get(), &c RPCs to A, B
TC 向 A、B 发送 put()、get() 等 RPCs。
A and B lock records (and wait if already locked).
A 和 B lock records(如果已经被 lock,就等待)。
Modifications are tentative, on a copy, only installed if commit.
Modifications 是 tentative 的,作用于 copy,只有 commit 时才 installed。
TC gets to END-X.
TC 到达 END-X。
TC sends PREPARE messages to A and B.
TC 向 A 和 B 发送 PREPARE messages。
If A is able to commit,
如果 A 能够 commit,
A responds YES.
A 回复 YES。
then A is in "prepared" state.
那么 A 进入“prepared” state。
otherwise, A responds NO.
否则,A 回复 NO。
Same for B.
B 也是如此。
If both A and B said YES, TC sends COMMIT messages to A and B.
如果 A 和 B 都回复 YES,TC 就向 A 和 B 发送 COMMIT messages。
If either A or B said NO, TC sends ABORT messages.
如果 A 或 B 中任意一个回复 NO,TC 就发送 ABORT messages。
A/B commit if they get a COMMIT message from the TC.
A/B 在从 TC 收到 COMMIT message 后 commit。
I.e. they copy tentative records to the real DB.
也就是说,它们把 tentative records copy 到 real DB。
And release the transaction's locks on their records.
并释放 transaction 在这些 records 上的 locks。
A/B send ACK (acknowledge) to say they have finished committing.
A/B 发送 ACK(acknowledge),表示已经完成 committing。
Why is this correct so far?
到目前为止为什么这是正确的?
Neither A nor B can commit unless they both agreed.
除非 A 和 B 都同意,否则 A 或 B 都不能 commit。
Participant failure 与 persistence
What if B crashes and restarts?
如果 B crash 并 restart 了怎么办?
If B crashed before it got the PREPARE, it can forget the transaction.
如果 B 在收到 PREPARE 之前 crash,它可以忘记该 transaction。
If B crashed after sending YES, B must remember (despite crash)!
如果 B 在发送 YES 之后 crash,B 必须记住 transaction(尽管发生了 crash)!
Because A might have received a COMMIT and committed.
因为 A 可能已经收到 COMMIT 并完成 commit。
So B must be able to commit (or not) even after a reboot.
所以即使 reboot 后,B 也必须能够 commit(或不 commit)。
PREPARE requires participants to save state that survives a crash.
PREPARE 要求 participants 保存能够跨越 crash 存活的 state。
Typically by writing to disk (slow).
通常通过写入 disk 来实现(很慢)。
B must save xaction state on disk before saying YES,
B 必须在回复 YES 之前把 xaction state 保存到 disk,
including locks and tentative data updates.
其中包括 locks 和 tentative data updates。
If B reboots, and disk says PREPARED but didn't receive COMMIT from TC,
如果 B reboot,disk 表明状态是 PREPARED,但它没有收到 TC 的 COMMIT,
B must ask TC, or wait for TC to re-send.
B 必须询问 TC,或等待 TC 重新发送。
And meanwhile, B must continue to hold the transaction's locks.
与此同时,B 必须继续持有 transaction 的 locks。
TC failure 与 2PC 决策
What if TC crashes and restarts?
如果 TC crash 并 restart 了怎么办?
If TC might have sent COMMIT before crash, TC must remember!
如果 TC 可能在 crash 前已经发送 COMMIT,TC 就必须记住它!
Since one participant may already have committed.
因为某个 participant 可能已经 commit。
Thus TC must write COMMIT to disk before sending COMMIT msgs.
因此 TC 必须在发送 COMMIT msgs 之前把 COMMIT 写入 disk。
And repeat COMMIT if it crashes and reboots,
如果 TC crash 并 reboot,还要重复发送 COMMIT,
or if a participant asks (i.e. if A/B didn't get COMMIT msg).
或者在 participant 询问时重复发送(也就是 A/B 没有收到 COMMIT msg 时)。
Participants must filter out duplicate COMMITs (using TID).
Participants 必须使用 TID 过滤 duplicate COMMITs。
What if TC never gets a YES/NO from B?
如果 TC 始终没有从 B 收到 YES/NO 怎么办?
Perhaps B crashed and didn't recover; perhaps network is broken.
可能 B crash 后没有 recover;也可能 network broken。
TC can time out, and tell participants to abort,
TC 可以 timeout,并通知 participants abort,
since TC has not sent any COMMIT msgs.
因为 TC 尚未发送任何 COMMIT msgs。
Good: allows servers to release locks.
这是好的:允许 servers release locks。
What if B times out or crashes while waiting for PREPARE from TC?
如果 B 在等待 TC 的 PREPARE 时 timeout 或 crash 了怎么办?
B has not yet responded to PREPARE, so TC can't have decided commit
B 还没有对 PREPARE 作出 response,所以 TC 不可能已经决定 commit。
so B can unilaterally abort, and release locks
因此 B 可以单方面 abort,并 release locks。
respond NO to future PREPARE
对未来的 PREPARE 回复 NO。
What if B replied YES to PREPARE, but doesn't receive COMMIT or ABORT?
如果 B 对 PREPARE 回复了 YES,却没有收到 COMMIT 或 ABORT 呢?
Can B unilaterally decide to abort?
B 能否单方面决定 abort?
No! TC might have gotten YES from both,
不能!TC 可能已经从两者都获得 YES,
and sent COMMIT to A, but crashed before sending to B.
并向 A 发送了 COMMIT,但在发送给 B 之前 crash。
So then A would commit and B would abort: incorrect.
这样 A 会 commit 而 B 会 abort:这是 incorrect 的。
B can't unilaterally commit, either:
B 也不能单方面 commit:
A might have voted NO.
A 可能投了 NO。
So: if B voted YES, it must "block": wait for TC decision.
所以:如果 B 投了 YES,它就必须“block”:等待 TC decision。
Forget transaction state
When can TC completely forget about a committed transaction?
TC 什么时候可以完全忘记一个 committed transaction?
If it sees an ACK from every participant for the COMMIT.
当它收到每个 participant 对 COMMIT 的 ACK 时。
Then no participant will ever need to ask again.
这样就不会再有 participant 需要询问。
When can participant completely forget about a committed transaction?
participant 什么时候可以完全忘记一个 committed transaction?
After it ACKs the TC's COMMIT message.
在它 ACK TC 的 COMMIT message 之后。
If it gets another COMMIT, and has no record of the transaction,
如果它再次收到 COMMIT,但已经没有该 transaction 的 record,
it must have already committed and forgotten, and can send ACK (again).
那么它一定已经 commit 并忘记了 transaction,可以再次发送 ACK。
2PC 的代价与 Raft 的边界
Two-phase commit perspective
从 two-phase commit 的角度看。
Used in sharded DBs when a transaction uses data on multiple shards
当 transaction 使用多个 shards 的 data 时,sharded DBs 会使用它。
But it has a bad reputation:
但它的 reputation 不佳:
slow: multiple rounds of messages
慢:需要 multiple rounds of messages。
slow: disk writes
慢:需要 disk writes。
locks are held over the prepare/commit exchanges; blocks other xactions
在 prepare/commit exchanges 期间 locks 一直被持有;会阻塞其他 xactions。
TC crash can cause indefinite blocking, WITH LOCKS HELD
TC crash 可能导致 indefinite blocking,而且 LOCKS HELD。
Thus usually used only in a single small domain
因此它通常只在单个 small domain 中使用。
E.g. not between banks, not between airlines, not over wide area
例如,不用于 banks 之间、不用于 airlines 之间,也不跨 wide area 使用。
Faster distributed transactions are an active research area.
更快的 distributed transactions 是一个活跃的 research area。
Raft and two-phase commit solve different problems!
Raft 和 two-phase commit 解决不同的问题!
Use Raft to get high availability by replicating
使用 Raft 通过 replication 获得 high availability,
i.e. to be able to operate when some servers are crashed
也就是即使一些 servers crash,仍然能够 operation。
the servers all do the *same* thing
这些 servers 都执行 same thing。
Use 2PC when each participant does something different
当每个 participant 执行不同的事情时,使用 2PC。
And *all* of them must do their part
并且它们 all 都必须完成自己的 part。
2PC does not help availability
2PC 不会提高 availability。
since all servers must be up to get anything done
因为要完成任何事情,所有 servers 都必须 up。
Raft does not ensure that all servers do something
Raft 不保证所有 servers 都执行某些事情。
since only requires a majority to execute.
因为它只要求 majority 执行。
High availability 与 atomic commit
What if you want high availability and atomic commit?
如果同时需要 high availability 和 atomic commit 呢?
Here's one plan.
这里有一个方案。
[diagram]
[diagram]。
The TC and servers should each be replicated with Raft
TC 和 servers 都应该分别使用 Raft 做 replication。
Run two-phase commit among the replicated services
在这些 replicated services 之间运行 two-phase commit。
Then you can tolerate failures and still make progress
这样既能容忍 failures,又能继续取得 progress。
Spanner uses this arrangement (after break).
Spanner 使用这种 arrangement(after break)。
Next:
接下来:
midterm on Thursday! in Walker.
周四 midterm!地点在 Walker。
read the papers, review lecture notes, look at old exams, look at your lab code
阅读 papers,复习 lecture notes,查看 old exams,并检查自己的 lab code。
http://dbmsmusings.blogspot.com/2019/01/its-time-to-move-on-from-two-phase.html
http://dbmsmusings.blogspot.com/2019/01/its-time-to-move-on-from-two-phase.html
机制小结
1. Serializability 与 linearizability 的关系
Linearizability 主要描述单个 operation 在并发 history 中的可观察顺序;serializability 描述一组 transactions 的整体结果是否等价于某个 serial transaction order。实现可以并行执行,但最终必须给出一个能够映射到合法 serial order 的结果。
2. 2PL 的安全性与代价
2PL 在第一次使用 record 时自动获取 lock,并持有到 transaction commit/abort 之后。这样可以阻止非 serializable interleavings;但 locks 会产生等待和 overhead,还可能出现 distributed deadlock,需要 cycle detection、timeout 或 wound-wait 等策略。
3. 2PC 的核心状态机
TC 先让 participants PREPARE。participant 只有在把 locks、tentative updates 等 state 持久化后才可以回复 YES;所有 participants 都 YES 时 TC 才能决定 COMMIT,否则决定 ABORT。participant 回复 YES 后不能自行选择结果,只能等待 TC 的 decision,这正是 2PC 在 failure 下可能 blocking 的根源。
4. Raft 与 2PC 不可互换
Raft 的目标是让多个 replicas 执行相同 computation,从而在少数 server failure 时继续提供 service;2PC 的目标是让执行不同工作的 participants 要么全部完成、要么全部不完成。若想同时得到 availability 和 atomic commit,可以分别用 Raft 复制 TC/servers,再在 replicated services 之间运行 2PC,但系统复杂度和性能代价都会增加。
本篇验收
- [x] 官方
l-2pc.txt已固定:333 行、285 个非空源行,SHA-256 与来源记录一致。 - [x] 源文本的非空行按原顺序逐行保留,中文译文紧随对应英文源行。
- [x] ACID、serializability、abort、pessimistic/optimistic concurrency control、2PL、deadlock 和 2PC failure cases 全部覆盖。
- [x] transaction time diagram、participant state 占位、最终 high-availability diagram 和参考 URL 均按原位置保留;源文件没有独立图片。
- [x]
BEGIN-X、END-X、2PL、TC、TID、PREPARE、COMMIT、ABORT、ACK、Raft 和 Spanner 等术语保留原样。 - [ ] Lecture 11 的中文译文仍建议进行一次人工术语审校;自动审计只验证源文覆盖与顺序,不证明翻译语义正确。
评论 (0)