论文:The Transaction Concept: Virtues and Limitations
本篇整理 Jim Gray 的《The Transaction Concept: Virtues and Limitations》。正文按 1981 年版本逐页翻译;每一页保留原始 PDF 页面图,原文中的 Figure 1–5、公式、列表、脚注和 References 均按原位置保留。transaction、atomicity、durability、consistency、logging、locking、nested transaction、time-domain addressing 等术语按数据库领域惯例保留 English 或中英并列。
论文:The Transaction Concept: Virtues and Limitations
相关文章:6.5840:Lecture 11 Distributed Transactions · 6.5840:Lecture 12 Spanner · 论文:Spanner:Google’s Globally-Distributed Database · 论文:The Design Philosophy of the DARPA Internet Protocols
TL;DR
- transaction 是什么:对 system state 的一种 transformation,要求 consistency、atomicity 和 durability;一组 actions 要么全部 commit,要么其 protected effects 都不对外留下。
- 本文的主要贡献:把 transaction concept 放回更一般的 programming/system context,比较 NonStop、update-in-place、time-domain addressing、logging + locking 等实现路线。
- 关键实现观察:logging 与 time-domain addressing 表面上差异很大,但两者都保存 state 的历史版本,并使用某种 serialization/locking 机制来管理并发。
- 重要限制:传统 transaction model 难以自然处理 nested transactions、持续数周或数月的 long-lived transactions,以及与 programming language 的统一。
- 后续影响:本文早于 “ACID” 缩写的流行,但已经明确讨论 atomicity、durability、consistency,并把 isolation 相关问题展开为 input stability、output hiding 和 locking。
名词解释
| 名词 | 含义 | 本文对应物 |
|---|---|---|
| transaction | 对 system state 的一致性 transformation | 具有 commit/abort 两种 outcome |
| consistency | transformation 遵守 system 的合法约束 | consistent state → new consistent state |
| atomicity | all-or-nothing | 全部 actions 生效,或 protected effects 全部撤销 |
| durability | commit 后效果跨越 failure 保留 | 依靠 stable storage、logging 等机制 |
| protected action | 可以并且必须 undo/redo 的 action | database/message operations |
| real action | 做出后不能 undo 的 action | cash dispenser、airplane wing 等真实设备操作 |
| compensating transaction | 通过另一个 transaction 抵消已提交效果 | 取消 reservation、补发款项 |
| time-domain addressing | 用 <name,time> 指向对象历史版本 | object 不被覆盖,而是 evolve |
| logging | 为 undo/redo 保存 log records | undo log、redo log、commit record |
| locking | 访问对象时动态建立冲突约束 | read lock、update lock、多粒度 lock |
| nested transaction | 把低层 transaction 作为高层 transaction 的 action | 通过 compensating transaction 撤销已对外可见的子操作 |
目录
| 章节 | 内容 |
|---|---|
| 版本与来源 | 论文版本、作者和固定 PDF |
| 原文页面与逐页翻译 | 11 个 PDF 页面按原页保留并翻译 |
| 事务机制索引 | transaction、logging、locking、recovery 和 nested transaction |
| 设计判断与边界 | 两种实现路线及其限制 |
| 本篇验收 | 页数、图片、Figure 和 References 覆盖 |
版本与来源
- 论文标题:The Transaction Concept: Virtues and Limitations。
- 作者:Jim Gray,Tandem Computers Incorporated。
- 版本:1981 年版本;论文发表于 VLDB 1981,正文页码 144–154。
- 本地固定来源:University of Texas PDF mirror;书目信息可由 DBLP record 交叉核对。
- 本地固定文件:
source/the-transaction-concept-virtues-and-limitations.pdf。 - PDF 固定信息:11 页;SHA-256:
ed78cdac79f6bb7aca9a0be65d8194efcd35b828dbebcc2b37f8e3c31ef1e9ef。 - 附件处理:每页按原始 PDF 页面矩形渲染为 PNG,存放于
assets/54 The Transaction Concept Virtues and Limitations/,正文按600宽度嵌入;由于原始页面宽高略有差异,保留各页原始比例和对应尺寸。 - 笔记类型:深度分析;理由是本文从定义、可靠性、并发控制、恢复实现一直讨论到 programming-language integration,形成完整的 transaction design 视角。
原文页面与逐页翻译
第 1 页:Abstract、transaction 定义与一般背景

The Transaction Concept: Virtues and Limitations
作者:Jim Gray,Tandem Computers Incorporated,19333 Vallco Parkway,Cupertino, California 95014。
Abstract
transaction 是一种 state transformation,具有以下 properties:atomicity(all or nothing)、durability(effects survive failures)和 consistency(a correct transformation)。
transaction concept 是构造 data-management applications 的关键。这个 concept 似乎也适用于 programming systems 的一般领域。
本文重新陈述 transaction concepts,并尝试把多种 implementation approaches 放在同一个 perspective 中。随后,本文描述几个需要进一步研究的领域:
- transaction concept 与 abstract data type 概念的 integration;
- 允许 transactions 由 sub-transactions 组成的技术;
- 处理持续极长时间(数天或数月)的 transactions。
Contents
- INTRODUCTION: What is a Transaction
- A GENERAL MODEL OF TRANSACTIONS
- IMPLEMENTATION TECHNIQUES
- Nonstop(TN):Making failures rare
- UPDATE IN PLACE:a poison apple?
- TIME-DOMAIN ADDRESSING:one solution
- LOGGING AND LOCKING:another solution
- LIMITATIONS OF KNOWN TECHNIQUES
- NESTED TRANSACTIONS
- LONG-LIVED TRANSACTIONS
- INTEGRATION WITH PROGRAMMING LANGUAGE
- SUMMARY
- ACKNOWLEDGMENTS
- REFERENCES
INTRODUCTION: What is a transaction?
transaction concept 源自 contract law。签订 contract 时,两个或更多 parties 会先协商一段时间,然后达成 deal。这个 deal 通过共同签署一份 document,或者通过其他行为(哪怕只是握手或点头)变得 binding。
如果 parties 彼此相当怀疑,或者只是希望更安全,他们会任命一个 intermediary(通常称为 escrow officer)来协调 transaction 的 commitment。
Christian wedding ceremony 就是这种 contract 的一个很好的例子。bride 和 groom 可能 “negotiate” 数天或数年,然后指定一位 minister 主持 marriage ceremony。minister 先询问是否有人反对 marriage;然后询问 bride 和 groom 是否同意结婚。如果他们都说 “I do”,minister 就宣布他们成为 husband and wife。
当然,contract 只是一种 agreement。如果个人愿意违法,他们可以违反 contract;但在法律上,contract(transaction)只有在一开始就是 illegal 时才可以被 annulled。对 bad transaction 的调整,是通过进一步的 compensating transactions(包括 legal redress)完成的。
transaction concept 由以下 properties 构成:
- Consistency:transaction 必须遵守 legal protocols。
- Atomicity:它要么发生,要么不发生;要么所有人都受 contract 约束,要么没有人受约束。
- Durability:transaction 一旦 committed,就不能被 abrogate。
第 2 页:General model、action 类型与 Nonstop 的开头

A GENERAL MODEL OF TRANSACTIONS
把 transaction concept 转移到 computer science 领域后,我们可以观察到:身边的大多数 transactions(banking、car rental 或购买 groceries),都可以在 computer 中表示为对 system state 的 transformations。
system state 由具有可变 values 的 records 和 devices 构成。system state 还包含关于 records values 的 assertions,以及关于这些 values 允许如何变化的 assertions。这些 assertions 称为 system consistency constraints。
system 提供一组 actions,用于读取和转换 records、devices 的 values。一组构成 consistent state transformation 的 actions,可以组合成一个 transaction。
transactions 保持 system consistency constraints:它们遵守这些 laws,把 consistent states 转换为新的 consistent states。
transactions 必须 atomic 且 durable:要么所有 actions 都执行,transaction 被称为 commit;要么 transaction 的 effects 一个都不 survive,transaction 被称为 abort。
为了允许某些 actions 被忽略,并处理另一些无法 undo 的 actions,上述定义还需要略微 refinement。
对 entities 执行的 actions 可分类如下:
| 类型 | 含义 |
|---|---|
| unprotected | 如果 transaction 必须 abort,action 不需要 undo 或 redo;如果需要重建 entity value,也可以不依靠该 action。 |
| protected | 如果 transaction 必须 abort,action 可以并且必须 undo 或 redo;如果需要重建 entity value,也可以依靠它。 |
| real | action 一旦完成,就不能 undo。 |
对 temporary files 的 operations,以及 intermediate messages 的 transmission,是 unprotected actions 的例子。传统 database 和 message operations 是 protected actions 的例子。transaction commitment,以及对 real devices(cash dispensers、airplane wings)的 operations,是 real actions 的例子。
每个 transaction 被定义为恰好有两个 outcome 之一:committed 或 aborted。committed transactions 的所有 protected 和 real actions 都会 persist,即使发生 failures 也是如此。
另一方面,aborted transaction 的 protected 和 real actions 所产生的 effects,永远不会对其他 transactions 可见。transaction 一旦 commit,它的 effects 只能通过运行后续 transactions 改变。
例如,如果某人的工资少发了,正确的 corrective action 是运行另一个 transaction,额外支付差额。这类 post-fact transactions 称为 compensating transactions。
一个 simple transaction 是 actions 的线性序列。一个 complex transaction 可能在 transaction 内部包含 concurrency;某个 action 的启动可能依赖一组 actions 的 outcome。这类 transactions 看起来像是包含嵌套在其中的 transactions,但 nested transactions 的 effects 只对其所在 transaction 的其他部分可见。
Figure 1:两个 transactions。T1 是 actions 的 simple sequence。T2 是一个更 complex 的 transaction,展示了 transaction 内部的 parallelism 和 nesting。
Nonstop (TM): Making failures rare
获得 transaction atomicity 和 durability 的一种方法,是构造一个永不 failure 的 perfect system。
假设你构造了永不 failure 的 perfect hardware,以及严格执行预期行为的 software。你的 system 会很受欢迎,所有 transactions 也总能成功。
但 system 仍会偶尔 failure,因为把 system 适配到实际 environment 的人会犯错误(application programming errors),操作 system 的人也会犯错误(data-entry 和 procedural errors)。
即使管理非常谨慎,system 仍会每几个月或几年 failure 一次;而且由于 data-entry error 或 authorization error,至少每 100 个 transactions 中就会有 1 个 failure。
第 3 页:可靠性、fail-fast modules 与 NonStop

由此可以得出两个结论:
- 不必构造一个 perfect system;一个每 1,000 年才 failure 一次的 system,已经足以让大多数 customers 满意。
- 即使 system perfect,一些 transactions 也会因为 data-entry error、insufficient funds、operator cancellation 或 timeout 而 abort。
本节讨论 “almost perfect” systems 的技术,并解释它们与 transaction processing 的关系。
Imperfection 有多种形式。system 可能因为 design error 或 device failure 而 failure。error 可能对 user 可见,也可能由 redundant checks 检测出来。
如果 system 做了错误的事情(没有检测到 error),就称为 unreliable。如果 system 没有在指定 time limit 内做正确的事情,就称为 unavailable。显然,高 availability 比高 reliability 更难实现。
John Von Neumann 被认为提出了这样的观察:可以用 unreliable components 构造非常 reliable 且 available 的 system。
Von Neumann 的想法,是在大规模 redundancy 上使用 majority logic(例如用 20,080 根 wires 实现一根 wire 的效果),从而把 mean time to failure 提高到几十年。Von Neumann 思考的是 neurons 和 vacuum tubes:它们的 mean time to failure 只有几天,却在一个 system 中以巨大数量(数百万或数十亿)使用。
此外,Von Neumann 的 model 是 flat 的,因此 chain 中任何一个 failure 都会破坏整条 chain。
幸运的是,computer systems 不需要 28,000 倍的 redundancy 才能得到很长的 mean time to failure。与 Von Neumann 的 nerve nets 不同,computer systems 由 modules hierarchical composition 而成,每个 module 都会 self-check:它要么正确工作,要么检测到 failure 并停止做任何事情。这类 modules 称为 fail-fast。
fail-fast computer modules(例如 processors 和 memories)的 mean time to failure 以 months 计。由于一个 system 通常由少量 modules 组成(典型地少于 100 个),要提高 system reliability,只需要非常有限的 redundancy。
考虑 disks 这个简单例子。一个典型 disk 大约每年 failure 一次。Failure 原因可能是 disk 上的 bad spots、spindle 的 physical failure,或通向 disk 的 electronics failure。修复 disk 或取得 spare disk 替换它,大约需要一小时。
如果 disks 做 duplexing(mirroring),并且它们 independent failure,那么一对 disks 同时 down 的时间大约是每 3,000 年一次。更 realistic 的 analysis 得出的 mean time to failure 是 800 年。因此,一个有 8 对 disks 的 system,大约每个世纪才会有一次 unavailable disk pair。
不使用 mirroring,同一个 system 每年会有大约 8 次 unavailable disk。
虽然 duplexed disks 从 1960 年代后期就已经使用,但我们迟迟没有从这个经验推广出以下观察:
- modules 的 mean time to failure 以 months 计;
- modules 可以做成 fail-fast:要么正确工作,要么 failure;
- spare modules 可以让 mean time to repair 看起来只有 seconds 或 minutes;
- 对这样的 modules 做 duplexing,可以得到以 centuries 计的 mean time to failure。
把这些 ideas 系统地应用到 hardware 和 software,可以产生 highly reliable、highly available systems。
High availability 要求重新思考许多 system-design concepts。比如 system maintenance:必须能够在 system 正常运行时把 components 插入 system。
在 hardware level,这要求 Underwriters Laboratory 批准系统周围没有 high voltages,也要求 components 能够承受 high power drains、surges,以及其他许多条件。在 software level,这意味着不存在 “SYSGEN”,并且任何 program 或 data structure 都可以在 system 运行时被替换。这些都与大多数 current system designs 有重大不同。
提供 continuous service 的 commercial systems 已经开始出现在 marketplace 中。最知名的可能是 Tandem systems。Tandem 把自己的 high-availability approach 称为 NonStop(这是 Tandem 的 trademark)。这类 Tandem systems 的 mean time to failure 在 1 年到 10 年之间。
在 hardware level,modules 和 paths 都做 duplexing,所有 components 都按 reliable、fail-fast operation 设计。在 software level,system 被组织成 message-based operating system;每个 process 可以有一个 backup process,当 primary process 或其 supporting hardware failure 时,backup process 继续 primary 的工作。
Alsberg 提出了相关的 technique。
构造 highly available system 并不容易。即使有了这样的 system,如果没有其他 tools,编写 fault-tolerant applications 仍然 non-trivial:当 primary process failure 时由 backup process takeover 是 delicate 的。backup process 必须以某种方式从 primary 停下的地方继续 computation,同时不能把 failure 传播给其他 processes。
第 4 页:Transaction 与 fault-tolerant application

编写 fault-tolerant applications 的一种 strategy,是让 primary process 在每次 operation 之前把自己的 state “checkpoint” 给 backup process。如果 primary failure,backup process 就从 primary 离开的地方继续 conversation。
在这种 event 中 resynchronize requestor 和 server processes 非常 subtle。
编写 fault-tolerant software 的另一种 strategy,是把某次 computation 的所有 processes 收集为一个 transaction;failure 时,把它们全部 reset 到 transaction 的 initial state。
发生 failure 时,transaction 被 undo(退回到一个 save point 或开头),然后由新 process 从该处继续。transaction management 提供的 backout 和 restart facilities,使 application programmer 不必关心 failures 或 process pairs。
transaction concept 的 implementors 必须使用 primitive process-pair mechanism,并处理 NonStop 的 subtleties;但此后,所有 programmers 都可以依赖 transaction mechanism,从而轻松编写 fault-tolerant software。
这样的 system 中,program 看起来与 conventional system 中的 program 没有区别,只是包含以下 verbs:BEGIN-TRANSACTION、COMMIT-TRANSACTION 和 ABORT-TRANSACTION。
使用 transaction concept,还允许 application programmer 在 system state 或 input data 看起来不正确时 abort transaction。这个 feature 不需要额外 cost,因为 undo transaction 的 mechanism 本来就已经存在。
此外,如果 transactions 通过 logging 实现,transaction manager 还可以根据 old state 和 log 重建 system state。这使得 transaction 在 multiple failures 存在时仍然具有 durability。
总之,NonStop(TM) techniques 可以让 computer systems 的表观 failure rate 达到 decades 或 centuries。但在实践中,systems 仍会因为 operator error(大约每年一次)和 application program errors(每年数次)而以 months 或 years 为单位 failure。这些因素成为 system reliability 的主要限制,而不再是 manufacturer 提供的 software 或 hardware。
本节说明了为什么需要 transaction concept 来简化 fault-tolerant applications 的实现。
实现 transaction concept 有两条看似不同的路线:time-domain addressing,以及 logging plus locking。下面几节会解释并对比这两种 approaches。
先预览一下:logging 把所有 objects 的 current state 聚集在一起,把 old versions 放入称为 log 的 history file;time-domain addressing 则把每个 object 的完整 history(所有 versions)和 object 放在一起。
两种 organization 都有独特的 virtues。
UPDATE IN PLACE: a poison apple?
当 bookkeeping 通过 clay tablets 或 paper and ink 完成时,accountants 建立了关于 good accounting practices 的明确 rules。
其中一个 cardinal rule 是 double-entry bookkeeping,使 calculations 能 self-check,从而 fail-fast。第二个 rule 是绝不修改账簿;如果犯了错误,就进行 annotation,并在账簿中写入新的 compensating entry。这样,账簿就成为 business transactions 的完整 history。
早期 computer systems 遵守这些 rules。bookkeeping entries 被表示为 punched cards 或 tape 上的 records。一次 run 读取 old master 和当天 activity(以 punched cards 上的 records 表示),产生 new master。old master 从不 update。
这部分是良好 accounting practices 的结果,也部分是 cards 和 tape 的技术属性造成的:写一盘新 tape 比重写旧 tape 容易。
direct-access storage(disks 和 drums)的出现改变了这一点。现在可以只 update file 的一部分。与其每次某个部分更新时复制整个 disk,不如只更新发生变化的部分,用它构造 new master。
其中一些 techniques,尤其是 side files 和 differential files,没有 update old master,因此仍遵守良好 accounting practices。但出于 performance reasons,大多数 disk-based systems 都被诱导去做 update in place。
第 5 页:Time-domain addressing 与版本历史

TIME-DOMAIN ADDRESSING: one solution
许多 system designers 把 update-in-place 看成一种 cardinal sin:它违反了数百年来遵守的传统 accounting practices。
已经有多个 system proposals,使 objects 永远不被修改;相反,一个 object 被认为拥有 time history,object addresses 也从单纯的 name 变成 <name,time>。
在这种 system 中,object 不是被 “updated”,而是被 “evolved”,从而拥有额外 information。evolve 一个 object,就是创建一个 new value,并把它追加为 object 在当前时间的 value。
old value 继续存在;只要指定该 value 曾经 current 的时间 interval 中任意一个时间,就可以 address 它。
这类 systems 称为 time-domain addressing 或 version-oriented systems。(有些人称它们为 immutable object systems,但我认为这是 misnomer,因为 objects 确实会随时间改变 values。)
Davies 和 Bjork 提出过一种名为 “general ledger” 的 time-domain addressing implementation,其中每个 entity 都有一条 values 的 time sequence。
他们的 system 不仅保存这些 values,还保存 dependency chain;如果后来发现了错误,就可以执行 compensating transaction,并把 new value 传播给所有依赖错误 data 的 transactions。
这种 system 的 internal bookkeeping,以及预期的 poor performance,使大多数研究者对它失去兴趣。University of Newcastle 的 Graham Wood 说明,dependency information 会 exponential growth。
Dave Reed 提出了最完整的、基于 time-domain addressing 的 transaction system proposal。
在 Reed 的 proposal 中,一个 entity E 有一组 values Vi,每个 value 在一个 time period 内有效。例如,entity E 及其 value history 可以写成:
E: <V0, [T0,T1)>, <V1, [T1,T2)>, <V2, [T2,*)>
这表示:E 从时间 T0 到 T1 的 value 是 V0;在 T1 时变成 V1;在 T2 时变成 V2,而 V2 是 current value。
每个 transaction 都被分配一个 unique execution time,所有 reads 和 writes 都相对于该时间解释。
在时间 T3 执行的 transaction 读取 entity E 时,得到该 entity 在 T3 的 value。在上面的例子中,如果 T3 > T2,则 value V2 的有效 period 会变成 [T2,T3)。
在时间 T3 执行的 transaction 把 value V3 写入 entity E 时,就会开始一个新的 time interval:
E: <V0, [T0,T1)>, <V1, [T1,T2)>, <V2, [T2,T3)>, <V3, [T3,*)>
如果 T2 >= T3,transaction 就会 abort,因为它试图 rewrite history。
transaction 的 writes 都依赖于一个 commit record。transaction commit 时,system validate(使之 valid)该 transaction 的所有 updates;transaction abort 时,system invalidate 所有 updates。
具体做法是把 commit record 的 state 设置为 commit 或 abort,然后 broadcast transaction outcome。
这是 Reed proposal 的简化描述。完整 proposal 还有许多其他 features,包括 nested transaction mechanism。此外,Reed 不使用 real time,而使用 pseudo-time,以避免实现 global clock 的困难。
关于这一 proposal 的易懂介绍,可以参见 [Reed] 和 [Svobodova]。
Figure 2:object
E的 versions 表示。图中展示了三个 committed versions 和一个 proposed version。当 versionV3commit 或 abort 时,commit record 和 object header 会更新。图改编自 [Svobodova]。
Reed 认为,这个 proposal 同时统一解决了 concurrency-control problem 和 reliability problem。此外,该 system 允许 applications 完整使用 time-domain addressing 的能力。例如,可以轻易询问:“year-end 时账簿是什么样子?”
time-domain addressing proposals 存在一些问题:
- Reads are writes:读取会推进 object 上的 clock,因此会 update object header,可能增加 I/O activity。
- Waits are aborts:在大多数 locking systems 中,conflicts 会让一个 process 等待另一个 process;在 time-domain systems 中,conflicts 会 abort writer。这可能排除执行大量 updates 的 long-running “batch” transactions。
- Timestamps 增加 overhead:每次 data access 都需要处理 timestamp;例如读取一百万个 timestamps 会有显著成本。一个简单的两级 hierarchy 可以让 sequential(whole-file)和 direct(single-record)locking 在同一 data 上共存。
第 6 页:Time-domain 的限制与 logging 的引入

- Real operations 与 pseudo-time:如果读取或写入 real device,那么它是在某个 real time 被读取或写入的;例如 nuclear reactor 的 control rods,或会接收和发放 money 的 automated teller machine。
目前还不清楚 real time 如何与 pseudo-time 对应,以及对 real devices 的 writes 应如何建模为 versions。
从这个列表可以看出,time-domain addressing system 的实现细节还没有全部解决。当然,这个 concept 本身是 valid 的。除了最后一个 issue,其他问题都是 performance issues,很可能会由真正尝试构建这类 systems 的人解决。
许多人对这种 approach 很 enthusiastic;他们一定会找到消除或缓解这些问题的方法。尤其是,Dave Reed 和他在 MIT 的 colleagues 正在构建这样的 system。
LOGGING AND LOCKING: another solution
logging 和 locking 是实现 transaction concept 的另一种方法。
传奇中的 Greeks——Ariadne 和 Theseus——发明了 logging。Ariadne 给了 Theseus 一团 magic ball of string;Theseus 搜索 Labyrinth、寻找 Minotaur 时把线展开。杀死 Minotaur 后,Theseus 沿着线回到 entrance,而没有永远迷失在 Labyrinth 中。
这根 string 就是他的 log,使他能够 undo 进入 Labyrinth 的过程。但 Minotaur 不是 protected object,因此 Theseus 离开时,Minotaur 的死亡并没有被 undo。
Hansel 和 Gretel 在寻找 berries、走进 woods 时复制了 Theseus 的 trick。他们沿途留下 crumbs,使他们能够通过反向跟踪返回,也使父母能够沿正向 trail 找到他们。
这是第一个 undo 和 redo log。不幸的是,一只 bird 吃掉了 crumbs,造成了第一个 log failure。
logging 的基本 idea 是:每个 undoable action 不仅要执行 action,还必须留下 string、crumb 或 undo log record,使 operation 可以被 undo。同样,每个 redoable action 不仅要执行 operation,还必须生成 redo log record,使 operation 可以被 redo。
根据 Hansel 和 Gretel 的经验,这些 log records 应由坚固材料构成,而不是由 bird 能吃掉的东西构成。在 computer 中,这意味着 records 应放在 stable storage 中——通常通过把 records 放在多个 non-volatile devices 上实现,并且这些 devices 具有 independent failure modes。
系统还应偶尔记录每个 object 的 stable copy,这样就可以从 old state 重建 current state。
Figure 3:DO-UNDO-REDO protocol。每个 protected action 的 execution 都会生成一个 log record,使 action 可以 undo 或 redo。unprotected actions 不需要生成 log records。不能 undo 的 actions(称为 real actions)使用相关但略有不同的 protocol,见下一幅图。
database operations 的 log records 非常简单,形式如下:
NAME OF TRANSACTION:
PREVIOUS LOG RECORD OF THIS TRANSACTION:
NEXT LOG RECORD OF THIS TRANSACTION:
TIME:
TYPE OF OPERATION:
OBJECT OF OPERATION:
OLD VALUE:
NEW VALUE:
old 和 new values 可以是 object 的完整 copies,但更常见的是只编码 object 中发生改变的部分。
例如,对 file 中某个 record 的 field 进行 update 时,通常记录 file、record 和 field 的 names,以及 old/new field values,而不是记录整个 file 或整个 record 的 old/new values。
一个 transaction 的 log records 会被 threaded together。要 undo 一个 transaction,就依次 undo 它 log 中的每个 action。
这种 technique 既可用于 program 发出的 transaction abort,也可用于清理 system problem(如 deadlock 或 hardware failure)导致未完成的 uncommitted transactions。
如果 object 的 current state 丢失,可以从 stable storage 中的 old state 出发,使用 redo log redo old state 之后所有 recent committed actions,从而重建 current state。
第 7 页:Redo、restartability 与 two-phase commit

有些 actions 不需要生成 log records。对 unprotected objects 的 actions(例如写入 scratch file),以及不改变 object state 的 actions(例如读取 object),都不需要 log records。
另一方面,有些 actions 一开始只能生成 log records,等 transaction commit 时才实际 apply。
不能 undo 的 real action 必须推迟到 transaction commit。在 log-based system 中,做法是保留 deferred operations 的 redo log。
当 transaction 成功 commit 后,recovery system 使用这个 log 首次执行 deferred actions。为使这些 actions 可 restart,它们会被命名(例如使用 sequence number),这样 duplicate actions 就会被丢弃。
Figure 4:real actions(不能 undo 的 actions)必须推迟到 commit。logging approach 会把 deferred operations 的 redo log 作为 commit completion 的一部分 apply。
另一个 detail 是:undo 和 redo operations 必须 restartable。也就是说,如果 operation 已经 undo 或 redo,再次执行它不应损坏或改变 object state。
restartability 的需求来自 undo/redo processing 期间可能发生 failures。通常使用 disk pages 的 version numbers,或 virtual circuits/sessions 的 sequence numbers 来实现 restartability。
基本上,undo 或 redo operation 会读取 version/sequence number;如果它已经是 desired number,就什么也不做;否则就转换 object 和该 sequence number。
Figure 5:UNDO 和 REDO 必须 restartable;如果 action 已经发生,再次执行时不能改变 object。
在 log-based scheme 中,transaction commit 通过把 commit record 写入 log 来 signal。如果 transaction 对多个 logs 有贡献,就必须小心保证 commit 要么出现在所有 logs 中,要么不出现在任何 log 中。
multiple logs 经常出现在 distributed systems 中(因为通常每个 node 有一个或多个 logs),但 central systems 中也会出现。
使 commit 成为 atomic action 的最简单 strategy,是只允许 transaction 的 active node 决定 commit 或 abort;所有其他 participants 都是 slaves,向 active node 询问 commit/abort decision。
Rosenkrantz、Sterns 和 Lewis 描述了这样一种 scheme。
通常,希望允许 transaction 中每个 participant 在 commit 之前 unilateral abort transaction。如果某个 participant 这样做,所有其他 participants 也必须 abort。
two-phase commit protocol 的目标,是尽量缩短 node 不允许 unilateral abort transaction 的时间。它与 wedding ceremony 非常相似:minister 问 “Do you?”,participants 回答 “I do”(或 “No way!”),然后 minister 说 “I now pronounce you” 或 “The deal is off”。
在 commit 时,two-phase commit protocol 先从每个 participant 得到 agreement,确认 transaction 已经 prepared to commit。participant 一旦对 prepare request 说 “I do”,就放弃 unilateral abort 的权利。
如果所有 participants 都同意 commit,commit coordinator 就 broadcast commit message;如果没有达到 unanimous consent,transaction 就 abort。
这种 protocol 已知有许多 variations,未来可能还会发表更多 variations。
第 8 页:并发、输入稳定性、locking 与 Summary 的开头

如果 transactions concurrent 运行,一个 transaction 可能读取另一个 transaction 的 outputs(updates 或 messages)。如果第一个 transaction abort,那么 undo 它就要求 undo 被第二个 transaction 读取的 updates 或 messages。
这又要求 undo 第二个 transaction;但第二个 transaction 可能已经 commit,因此无法 undo。为了避免这种 dilemma,一个 transaction 的 real 和 protected updates(也就是 undoable updates)必须在 transaction commit 之前对其他 transactions hidden。
为了确保读取两个相关 records,或者重复读取同一个 record,都能得到 consistent results,还必须 stabilize transaction 读取的 records,并让它们在 transaction commit 前保持不变。否则,一个 transaction 可能再次读取同一个 record,却得到两个不同的答案。
实现这种 input stability 和 output hiding 似乎有许多 ways,但它们都可以归结为以下 ideas:
- 一个 transaction 有一组 inputs,记作
I。 - 一个 transaction 有一组 outputs,记作
O。 - 其他 transactions 可以读取
I,但不能读取或写入O。
一些 schemes 会预先猜测 input/output sets,并在 transaction scheduling 时进行 set intersection(或 predicate intersection),判断新 transaction 是否可能与已运行 transactions 冲突。
在这种情况下,new transaction 的 initiation 会被 delay,直到它与任何 running transaction 都不冲突。IMS/360 似乎是第一个尝试这种 scheme 的 system;后来它被广泛重新发现。该 scheme 并不十分 successful,IMS 在 1973 年放弃了 predeclaration(称为 “intent scheduling”)。
更简单、更 efficient 的 scheme,是在 object 被访问时 lock 它。这种 technique 动态计算 transaction 的 I 和 O sets。如果 object 已经 locked,requestor 就等待。
通过区分两种 lock modes,可以容纳 multiple readers:一种表示 update access,另一种表示 read access。read locks 彼此 compatible,而 update locks 彼此不 compatible。
一个重要 generalization,是允许 multiple granularities 的 locks。有些 transactions 想 lock 数千 records,另一些只想 lock 少量 records。
一种 solution,是允许 transaction 以单个 predicate 发出 lock,恰好覆盖它想 lock 的 records。检测 lock conflict 就需要 evaluate 或 test membership in predicates;这通常很 expensive。
一种 compromise,是挑选 fixed set of predicates,把它们组织成 directed acyclic graph,并从 root 到 leaf 进行 locking。这在 generality 和 efficiency 之间取折中。
如果 transaction T 等待 transaction T',而 T' 又等待 T,两个 transactions 就会永远 stalled,形成 deadlock。
deadlock 虽然 rare,但必须处理。必须通过 timeout 或在 who-waits-for-whom graph 中寻找 cycles 来 detect deadlock;然后选择一组 victims,用 log abort 它们,并释放它们的 locks。
实践中,waits 似乎很少(大约每 1,000 个 transactions 一个),deadlocks 则像 miracles 一样罕见。但数据显示,deadlocks per second 会随着 multiprogramming level 的平方、以及 transaction size 的四次方增长;这说明未来当 transactions 更大或并发 transactions 更多时,deadlocks 可能成为问题。
SUMMARY
前面的 sections 讨论了实现 transaction concept 的两种表面不同 approaches:time-domain addressing 和 logging。
为了使 log operations restartable,object 或 object fragments 会被 tag 上 version numbers。因此,大多数 logging schemes 都包含一种 time-domain addressing。
如果给每个 log record 一个 timestamp,那么 log 就可以实现 time-domain addressing。假如 Gretel 在每个 crumb 上写下一个 time,那么我们就可以通过跟随 crumbs,找出他们在某个时间所在的位置,直到遇到目标 time interval。
logging systems 把 old value 写入 log,因此并没有真的 discard old values。相反,log 是 state 的 time-domain-addressable version,而 disk 保存 state 的 current version。
time-domain addressing schemes 会把 old versions “garbage collect” 到某种看起来非常像 log 的东西中,并使用 locks serialize 对 object headers 的 updates。
我的结论是:尽管 time-domain addressing 和 logging schemes 在外部看起来不同,但在 internal structure 上它们相似之处多于差异。
实现 time-domain addressing 似乎存在困难。Dave Reed 通过类比断言:每个 locking/logging trick 都有一个对应的 time-domain-addressing trick。如果这是真的,那么两种 schemes 都是 viable transaction implementations。
第 9 页:Known techniques 的限制与 nested transactions

LIMITATIONS OF KNOWN TECHNIQUES
transaction concept 的目的是简化某些 applications 的 programming。事实上,在 airline reservation、electronic funds transfer 或 car rental 等领域,transaction concept 非常 effective。
但这些 applications 都具有简单、短 duration 的 transactions。我认为 current transaction models 存在以下困难:
- Transactions 不能嵌套在 transactions 内。
- Transactions 被假设只持续 minutes,而不是 weeks。
- Transactions 没有与 programming languages 统一。
NESTED TRANSACTIONS
考虑实现一个 travel-agent system。该 system 中的一个 transaction 包括:
- Customer 打电话给 travel agent,告知 destination 和 travel dates。
- Agent 与 airlines 协商 flights。
- Agent 与 car-rental companies 协商 cars。
- Agent 与 hotels 协商 rooms。
- Agent 收到 tickets 和 reservations。
- Agent 把 tickets 给 customer,并取得 credit-card number。
- Agent 向 credit card billing。
- Customer 使用 tickets。
Customer 并不少见地会 cancel trip;此时 agent 必须 undo transaction。
到目前为止描述的 transaction concept,在这个例子下会崩溃。与其他 organizations 的每次 interaction,都是与该 organization 进行的一次 transaction(atomic、consistent、durable transformation)。
interaction 完成后,agent 不能 unilateral abort 它;相反,agent 必须运行 compensating transaction 来 reverse 前一个 transaction(例如 cancel reservation)。
Customer 把整个 scenario 看成一个 transaction。Agent 看到 scenario 的 fine structure,把每个 step 当作 action。Airlines 和 hotels 只看到单独 actions,但把它们视作 transactions。
这个例子说明:在下一个 abstraction level 上,actions 也可能是 transactions。
一种看起来有帮助的 approach,是把 transaction 看成以下东西的 collection:
- 对 unprotected objects 的 actions;
- 可以 undo 或 redo 的 protected actions;
- 可以 defer 但不能 undo 的 real actions;
- 可以通过调用 compensating transactions undo 的 nested transactions。
nested transactions 与 protected actions 不同,因为 nested transaction 的 effects 在 parent transaction commit 之前就对外部世界可见。
当运行 nested transaction 时,它会以 side effect 的形式返回该 nested transaction 对应的 compensating transaction 的 name 和 parameters。如果 parent transaction 被 undo,这些 information 就从 parent transaction 的 log 中取出并调用。
这个 log 必须对 user 可见(成为 database 的一部分),这样 user 和 application 才知道已经做了什么,以及还需要做什么或 undo 什么。
在大多数 applications 中,一个 transaction 已经存在对应的 compensating transaction;因此生成 compensating transaction(编码它,或调用它)并不是很大的 programming burden。
如果其他方法都失败,compensating transaction 至少可以向 human 发送一条消息:“Help, I can't handle this”。这可能不令人满意,但总比今天普遍使用的完全 manual process 好。
至少在这个 proposal 中,recovery system 会跟踪 transaction 做过什么,以及为了 undo 它必须做什么。
目前,application programmers 通过一种称为 “scratchpad” 的 technique 实现这类 applications;在 IMS 中叫 scratchpad,在 CICS 中叫 “transaction work area”。
application programmer 把 transaction state(即自己的 log)作为一条 record 存放在 database 中。每次 transaction 变为 active 时,它读取自己的 scratchpad,从而重新建立 transaction state。
然后,transaction 要么继续推进,并把新的 scratchpad 插入 database;要么 abort,并把 scratchpad 当作需要 undo 的事项的 log。
在这个例子中,application programmer 实际上是在实现 nested transactions。这应该成为 host transaction-management system 中的 general facility。
有人认为 nested transactions 根本不是 transactions。它们确实拥有一些 transaction properties:
- 对 state 的 consistent transformation;
- 要么所有 actions commit,要么通过 compensation undo;
- 一旦 commit,就不能 undo;
- 使用
BEGIN、COMMIT和ABORTverbs。
但它们没有 atomicity property:其他 participants 可以看到 nested transaction 的 uncommitted updates,而这些 updates 随后可能通过 compensation 被 undo。
第 10 页:Long-lived transactions 与 programming-language integration

LONG-LIVED TRANSACTIONS
travel-agent example 的第二个问题,是 transactions 突然变成 long-lived。
目前,最大的 airlines 和 banks 大约有 10,000 个 terminals,并在任意时刻有约 100 个 active transactions。这些 transactions 存活一两秒,随后永远消失。
现在假设出现 lifetime 为数天或数周的 transactions。这在 travel、insurance、government 和 electronic mail applications 中并不罕见。这样将会有数千个 concurrent transactions。
至少在 database applications 中,deadlock frequency 会随着 multiprogramming level 的平方,以及 transaction size 的四次方增长。
你可能认为这足以反对 locking、支持 time-domain addressing。但 time-domain addressing 也有同样的问题。
我对这个问题的 solution,是接受较低程度的 consistency,使得只有 “active” transactions 持有 locks。
这意味着 uncommitted transactions 的 updates 对其他 transactions 可见。反过来,这又意味着一个 transaction 的 UNDO 和 REDO operations 必须与其他 transactions 的 DO operations commute。
也就是说,如果 T1 update entity E,然后 T2 update E,接着 T1 abort,那么 T2 的 update 不应被 undo。
如果某个 object 只通过 additions 和 subtractions 操作,并且 log 记录 delta 而不是 old/new value,那么就可能让 UNDO 和 REDO 与 DO commute。
IMS Fast Path 利用了 plus 和 minus 可以 commute 这一事实来减少 lock contention。没有人知道这种 trick 可以推广到多远。
long-running transactions 的一个小问题是:current systems 往往在 system restart 时 abort 它们。
当只有 100 个 active transactions,且 people 正在 terminals 前等待重新提交时,这种做法虽然可以想象(但并不友好)。当 system restart 导致 10,000 个 transactions 丢失时,在 restart 时全部 discard 旧 transactions 就不可想象了。
可以使用 transaction save points 跨越 system restarts 保留 active transactions:transaction 声明一个 save point;如果 system restart,就把 transaction(program 和 data)reset 到最近的 save point。
INTEGRATION WITH PROGRAMMING LANGUAGES
transaction concept 应如何反映到 programming languages 中?我倾向的 proposal,是提供 BEGIN、SAVE、COMMIT 和 ABORT verbs。
每当定义一个新的 object type 及其 operations 时,该 type 的 protected operations 必须生成 undo/redo log records;如果 object 是 shared 的,还必须 acquire locks。
type manager 必须提供 UNDO 和 REDO procedures;它们接受 log records,并重建 object 的 old 和 new versions。
如果 operation 是 real operation,那么必须 defer 它;log manager 必须在 commit time 调用 type manager,真正执行该 operation。
如果 operation 是 nested transaction,那么 operation 必须把 compensating transaction 的 name,以及传给 compensating transaction 的 input,放入 undo log。
此外,type manager 必须参与 system checkpoint 和 restart,或者采用其他方式处理 system failures 和 media failures。
我不确定这个 idea 是否适用于 general case,也不确定 transaction concept 是否真的可以推广到 non-EDP areas of programming。logging 的 performance 可能高到无法接受。
不过,transaction concept 在 database area 中非常 convenient,也可能适用于 conventional transaction processing 之外的某些 programming areas。
Brian Randell 和他在 Newcastle 的 group 在这个领域提出过一个 proposal。Interlisp 等 artificial-intelligence languages 支持 backtracking 和 UNDO-REDO facility。Barbara Liskov 一直在探索把 transactions 加入 Clu language 的 idea,并且可能发现一种新的 approach。
第 11 页:Summary、Acknowledgments 与 References

SUMMARY
Transactions 并不是新 idea;它们可以追溯到数千年前。
把 transformation 设计成 consistent、atomic 和 durable,是一种 simple 且 convenient 的 idea。许多 implementation techniques 已经为人所知,我们也对其中大多数拥有 practical experience。
然而,我们的 transaction concept 以及现有 implementation techniques,不足以应对许多 applications 的任务。
它们无法处理 nested transactions、long-lived transactions,也可能无法很好地融入 conventional programming systems。
我们也许正在看到 Peter Principle 的运作:“every good idea is generalized to its level of inapplicability”。
但我相信,这里勾勒的问题(long-lived 和 nested transactions)必须解决。我乐观地认为,transaction concept 为 structuring applications 提供了一个 convenient abstraction。
实现这类 applications 的人会遇到这些问题,并已经采用一些 expedient solutions。本文的一个贡献,是把这些问题抽象出来,并勾勒出 common-use techniques 的 generalizations,用于处理这些问题。
我预计,这些 general techniques 最终能够支持 long-lived 和 nested transactions。
ACKNOWLEDGMENTS
本文显然受益于 References 中列出的 authors。此外,nested 和 long-lived transactions 的讨论,来自与 Andrea Barr、Bob Good、Jerry Held、Pete Homan、Bruce Lindsay、Ron Obermarck 和 Franco Putzolu 的讨论。
Wendy Bartlett、Andrea Barr、Dave Gifford 和 Paul McJones 为本文 presentation 做出了多项贡献。
REFERENCES
- [Alsberg] Alsberg, P. A.; J. D. Day,“A Principle for Resilient Sharing of Distributed Resources”,Proc. 2nd Int. Conf. on Software Engineering,IEEE,1976。
- [Bartlett1] Bartlett, J. F.,“A Nonstop Operating System”,Eleventh Hawaii International Conference on System Sciences,1978。
- [Bartlett2] Bartlett, J. F.,“A Nonstop Kernel”,Proceedings of Eighth Symposium on Operating Systems Principles,ACM,1981;亦为 Tandem TR 81.4。
- [Beeri] Beeri, C.; R. Obermarck,“A Resource Class Independent Deadlock Detection Algorithm”,IBM RJ-3077 (38123),1981。
- [Bernstein] Bernstein, P. A.; D. W. Shipman; J. B. Rothnie,“Concurrency Control in a System for Distributed Databases (SDD-1)”,ACM TODS,Vol. 5,No. 1,1980。
- [Barr] Barr, A. J.,“Transaction Monitoring in Encompass: Reliable Distributed Transaction Processing”,Proceedings of Very Large Database Conference,1981;亦为 Tandem TR 81.3。
- [Davies and Bjork] Davies, C. T.; Bjork, L. A.,private communication,1972。
- [Bjork] Bjork, L. A.; C. T. Davies,“The Semantics of the Preservation and Recovery of Integrity in a Data System”,IBM TR-82.540,1972。
- [Eswaran] Eswaran, K. E.; J. N. Gray; R. A. Lorie; I. L. Traiger,“On the Notions of Consistency and Predicate Locks”,CACM,Vol. 19,No. 11,1976。
- [Gray1] Gray, J.,“Notes on Database Operating Systems”,Operating Systems — An Advanced Course,Springer-Verlag Lecture Notes in Computer Science,Vol. 60,1978。
- [Gray2] Gray, J.,“A Transaction Model”,Automata Languages and Programming,Springer-Verlag Lecture Notes in Computer Science,Vol. 80,1980。
- [Gray3] Gray, J.; P. Homan; H. Korth; R. Obermarck,“A Strawman Analysis of Deadlock Frequency”,待发表于 SIGOPS Review。
- [Heistand] Heistand, R. E.,“Airlines Control Program System, Concepts and Facilities”,IBM form number GH20-1473-1,1975。
- [Japan] Papers from the Tutorial on Reliable Business Systems in Japan,AFIPS Press,1978。
- [Katzman] Katzman, J. A.,“A Fault-Tolerant Computing System”,Eleventh Hawaii International Conference on System Sciences,1978。
- [Obermarck] Obermarck, R.,“IMS/VS Program Isolation Feature”,IBM RJ2879 (36435),1980。
- [Randell] Randell, B.,“System Structure for Fault Tolerance”,IEEE Transactions on Software Engineering,Vol. 1,No. 2,1975。
- [Reed1] Reed, D. P.,“Naming and Synchronization in a Decentralized System”,MIT/LCS TR-205,1978。
- [Reed2] Reed, D. P.,“Implementing Atomic Actions on Decentralized Data”,Proc. Seventh ACM/SIGOPS Symposium on Operating Systems Principles,1979。
- [Rosenkrantz] Rosenkrantz, D. J.; R. D. Stearns; P. M. Lewis,“System Level Concurrency Control for Database Systems”,ACM TODS,Vol. 3,No. 2,1977。
- [Severence] Severence, D. G.; G. M. Loman,“Differential Files: Their Application to Maintenance of Large Databases”,ACM TODS,Vol. 1,No. 3,1976。
- [Svobodova] Svobodova, L.,“Management of Object Histories: the Swallow Repository”,MIT/L&R-243,1980。
- [von Neumann] Von Neumann, J.,“Probabilistic Logics and the Synthesis of Reliable Organisms From Unreliable Components”,Automata Studies,Princeton University Press,1956。
- [Wood] Wood, W. G.,“Recovery Control of Communicating Processes in a Distributed System”,University of Newcastle upon Tyne TR-158,1980。
事务机制索引
| 机制 | 解决的问题 | 主要代价或边界 |
|---|---|---|
| atomicity | commit/abort 的 all-or-nothing | real actions 不能 undo,必须 defer 或 compensation |
| durability | failure 后保留 committed effects | 需要 stable storage、redo、checkpoint 和 restart |
| consistency | 只把 consistent state 转换为新的 consistent state | correctness 依赖 application/legal protocol |
| fail-fast module | 把错误转为可检测 failure | 需要 self-check、冗余和 spare module |
| time-domain addressing | 保留 object 的完整历史 | reads 可能成为 writes,conflict 可能导致 abort,real-time 难映射 |
| undo/redo logging | 支持 abort、restart 和 recovery | 需要 stable log、version/sequence number 和 commit protocol |
| locking | 隐藏 outputs、稳定 inputs、控制并发 | deadlock、lock contention、多粒度冲突 |
| two-phase commit | 在多个 logs/nodes 间形成一致 commit decision | prepare 后 participant 暂时失去 unilateral abort 权利 |
| nested transaction | 把低层 transaction 组合成更高层 action | effects 可能提前可见,需 compensating transaction |
| save point | 跨 restart 保留 long-lived transaction | 仍需定义程序、data 和外部 effects 的恢复语义 |
设计判断与边界
- 本文的 transaction 不等于单纯数据库 update:它统一处理 data transformation、failure recovery、external device effects 和 application-level compensation。
- Atomicity 依赖 action 分类:protected actions 可 undo/redo,unprotected actions 不必恢复,real actions 必须 defer 或以 compensation 处理。
- Logging 与 time-domain addressing 是互相可模拟的:log record 加 timestamp 可以形成 time-domain history;time-domain system 的 old versions 也会被组织成类似 log 的结构。
- Isolation 在本文中表现为 input stability 和 output hiding:即便论文没有使用后来流行的 ACID 缩写,相关问题已经被明确提出。
- 跨组织事务不能由单一 coordinator 任意撤销:travel-agent example 说明,nested transaction 与 compensating transaction 是处理外部已提交 effects 的必要工具。
- long-lived transaction 的核心困难是持锁与恢复成本:要处理数周事务,系统可能必须降低 consistency,让 active transactions 才持锁,并要求操作具备 commute 性质。
- transaction concept 的推广需要 language/runtime 支持:仅在 database manager 中提供 API,不足以处理 arbitrary object types、real operations 和 nested transactions。
本篇验收
- [x] 已固定 Jim Gray 1981 年版本,共 11 页;SHA-256 为
ed78cdac79f6bb7aca9a0be65d8194efcd35b828dbebcc2b37f8e3c31ef1e9ef。 - [x] 已按 PDF 页序保留第 1–11 页页面图;原始页面尺寸略有差异,PNG 按各页原比例渲染并按
600宽度嵌入。 - [x] 已覆盖 Abstract、transaction model、unprotected/protected/real actions、Nonstop、update-in-place、time-domain addressing、Figures 1–5、logging、locking、restartability、two-phase commit、nested transactions、long-lived transactions、programming-language integration、Summary、Acknowledgments 和 References。
- [x] 已保留公式、版本区间、log-record 字段、
BEGIN/SAVE/COMMIT/ABORTverbs、I/Osets、deadlock 关系和原文页序。 - [x] 已补充术语表、事务机制索引、设计边界和与 distributed-transactions 相关笔记的导航。
参考资料
评论 (0)