# Lamport时间戳

**Lamport时间戳**是分布式系统中为每个事件指定的一个逻辑时钟值，使得如果一个事件能够因果地影响另一个事件，则前者收到的时间戳小于后者。该方案由当时在SRI International任职的美国计算机科学家Leslie Lamport在1978年发表于《美国计算机学会通讯》（*Communications of the ACM*）的论文《时间、时钟与分布式系统中事件的排序》（"Time, Clocks and the Ordering of Events in a Distributed System"）中提出。<sup>[1](https://www.microsoft.com/en-us/research/publication/time-clocks-ordering-events-distributed-system/)</sup>

| 关键事实 | 详情 |
|---|---|
| 起源 | Leslie Lamport，《时间、时钟与分布式系统中事件的排序》，《美国计算机学会通讯》21卷7期（1978年7月），第558–565页<sup>[1](https://www.microsoft.com/en-us/research/publication/time-clocks-ordering-events-distributed-system/)</sup> |
| 保证 | 若事件a先于事件b发生，则timestamp(a) < timestamp(b)；反之不成立<sup>[2](https://people.cs.rutgers.edu/~pxk/classes/417/exam/guide-final.html)</sup> |
| 算法 | 每个进程维护一个计数器：每发生一个本地事件即递增，发送每条消息时附带该计数器值，接收消息时置计数器 = max(计数器, T) + 1<sup>[2](https://people.cs.rutgers.edu/~pxk/classes/417/exam/guide-final.html)</sup> |
| 全序 | 时间戳相等时，按进程的任意排序打破平局，从而将因果偏序扩展为全序<sup>[2](https://people.cs.rutgers.edu/~pxk/classes/417/exam/guide-final.html)</sup> |
| 局限 | Lamport时间戳无法检测并发：较小的时间戳并不能说明两个事件是因果相关还是相互独立<sup>[2](https://people.cs.rutgers.edu/~pxk/classes/417/exam/guide-final.html)</sup> |
| 荣誉 | 该1978年论文获2000年PODC最具影响力论文奖（后更名为Edsger W. Dijkstra分布式计算奖），并于2007年获ACM SIGOPS名人堂奖<sup>[1](https://www.microsoft.com/en-us/research/publication/time-clocks-ordering-events-distributed-system/)</sup> |

## 先于发生关系与时钟条件

该方案建立在**先于发生**关系之上，记作\( a \rightarrow b \)，定义为满足三条规则的最小关系：若a和b是同一进程中的事件且a先于b，则\( a \rightarrow b \)；若a是某条消息的发送而b是该同一条消息的接收，则\( a \rightarrow b \)；且该关系具有传递性。<sup>[2](https://people.cs.rutgers.edu/~pxk/classes/417/exam/guide-final.html)</sup> 这一关系刻画的是潜在因果性：若\( a \rightarrow b \)，则来自事件a的信息可能已经通过系统的通信渠道到达事件b。若两个事件互不先于对方发生，则它们是并发的。<sup>[2](https://people.cs.rutgers.edu/~pxk/classes/417/exam/guide-final.html)</sup>

Lamport在不依赖物理时钟的情况下定义了这一排序，因为分布式系统没有单一的共享时钟，而真实时钟并不完美。**时钟条件**规定：对任意事件a和b，若\( a \rightarrow b \)则\( C(a) < C(b) \)，其中C是逻辑时钟系统。<sup>[3](https://lamport.azurewebsites.net/pubs/time-clocks.pdf)</sup> 该条件刻意是单向的。若逆命题也成立，则任意两个并发事件都必须携带相同的时间戳，这会在其他情形下与时钟条件矛盾；因此Lamport时钟时常为并发事件分配有序的时间戳。<sup>[3](https://lamport.azurewebsites.net/pubs/time-clocks.pdf)</sup><sup> • </sup><sup>[4](https://courses.cs.washington.edu/courses/cse452/26sp/lectures/lec07-notes.html)</sup> 由逆否命题可知，若\( C(a) \geq C(b) \)则a不先于b发生，但仅凭有序的时间戳永远无法确立先于发生关系。<sup>[4](https://courses.cs.washington.edu/courses/cse452/26sp/lectures/lec07-notes.html)</sup>

## 算法

每个进程维护一个初始为零的计数器，并遵循三条规则：<sup>[2](https://people.cs.rutgers.edu/~pxk/classes/417/exam/guide-final.html)</sup>

1. 发生内部事件时，计数器递增。
2. 发送消息时，计数器递增，并将该值附于消息中。
3. 接收时间戳为T的消息时，置计数器 = max(计数器, T) + 1。

由于沿因果相连事件链的每一步都会使计数器增大，因果有序的事件总会获得递增的时间戳。<sup>[2](https://people.cs.rutgers.edu/~pxk/classes/417/exam/guide-final.html)</sup> 在Lamport的原始表述中，每个进程在任意两个相继事件之间递增其时钟，而收到消息则迫使接收方时钟被设置为大于所携带的时间戳。<sup>[3](https://lamport.azurewebsites.net/pubs/time-clocks.pdf)</sup>

仅凭时间戳给出的是一个偏序，其中某些事件可能共享同一值。要实现全序，需在每个时间戳后附加进程标识符，并对所得数对按字典序排序：当\( t_1 < t_2 \)时\( (t_1, p_1) < (t_2, p_2) \)成立，当\( t_1 = t_2 \)且\( p_1 < p_2 \)时也成立。<sup>[2](https://people.cs.rutgers.edu/~pxk/classes/417/exam/guide-final.html)</sup> 这种打破平局的方式使每一对事件即使并发也具有确定的顺序。Lamport指出，所得全序取决于时钟的选择和平局规则，并非唯一；只有偏序是由事件系统所确定的。<sup>[3](https://lamport.azurewebsites.net/pubs/time-clocks.pdf)</sup>

## 论文的用途

Lamport的意图不止于打时间戳。他观察到，一个对事件进行全序排序的算法可以实现任意分布式系统，因为分布式系统可以被描述为由处理器网络实现的顺序状态机；对输入请求进行全序排序即可得到状态机复制。<sup>[5](https://lamport.azurewebsites.net/pubs/pubs.html)</sup> 作为示例，他给出了一个分布式互斥算法：进程共享单一资源，请求按时间戳所确立的顺序被授予，每个进程根据自身的请求队列在本地判断自己何时持有授权。<sup>[3](https://lamport.azurewebsites.net/pubs/time-clocks.pdf)</sup> 这种打时间戳的技术被用于许多互斥与死锁算法中。<sup>[6](https://www.cs.ru.nl/~ths/a3/html/h9/h9.html)</sup>

其思想渊源可追溯至狭义相对论。Lamport曾写道，相对论告诉我们，时空中的事件不存在不变的全序，只存在偏序——若e1能够因果地影响e2，则e1先于e2；该论文最初是为了纠正Paul Johnson和Bob Thomas关于维护数据库副本的一篇笔记，他们的时间戳算法允许违反因果性的行为。<sup>[5](https://lamport.azurewebsites.net/pubs/pubs.html)</sup>

## 局限与后继方案

核心局限在于Lamport时间戳无法检测并发。观察到timestamp(a) < timestamp(b)时，无法判断a是因果先于b，还是两个事件相互独立。<sup>[2](https://people.cs.rutgers.edu/~pxk/classes/417/exam/guide-final.html)</sup> 向量时钟为每个进程维护一个独立计数器，保留了偏序本身并使并发可检测，但其消耗的网络带宽和存储远多于Lamport的标量时钟；矩阵时钟则更进一步。<sup>[7](https://novaresearch.unl.pt/en/publications/causality-tracking-trade-offs-for-distributed-storage/)</sup> 由此产生一种依赖于工作负载的权衡：对某些工作负载，更昂贵的时钟带来显著收益，而对另一些则观察不到明显好处。<sup>[7](https://novaresearch.unl.pt/en/publications/causality-tracking-trade-offs-for-distributed-storage/)</sup>

**混合逻辑时钟**（Hybrid logical clocks）将这一思想扩展到现代数据库。每个CockroachDB节点维护一个HLC，其物理分量来自节点粗同步的系统时钟，逻辑分量则基于Lamport时钟；逻辑部分在每次节点间消息交换时追踪因果性，类似向量时钟但开销更小。<sup>[8](https://www.cockroachlabs.com/pdf/cockroachdb-the-resilient-geo-distributed-sql-database-sigmod-2020.pdf)</sup> Google的Spanner走了另一条路，使用由GPS接收器和原子钟支持的TrueTime API来分配具有全局意义的提交时间戳，将时钟不确定性通常保持在10毫秒以下。<sup>[9](https://research.google.com/pubs/archive/39966.pdf)</sup> 混合时钟在MongoDB和CockroachDB等数据库中也有实际应用，给出的版本号同时携带日期时间和可排序的逻辑分量。<sup>[10](https://martinfowler.com/articles/patterns-of-distributed-systems/hybrid-clock.html)</sup>

## 荣誉

该1978年论文获2000年PODC最具影响力论文奖（后更名为Edsger W. Dijkstra分布式计算奖），并于2007年获ACM SIGOPS名人堂奖。<sup>[1](https://www.microsoft.com/en-us/research/publication/time-clocks-ordering-events-distributed-system/)</sup> Lamport称这是他被引用最多的论文，同时指出许多读者忽略了其中的状态机论证，只记住了因果关系或互斥示例。<sup>[5](https://lamport.azurewebsites.net/pubs/pubs.html)</sup>

## References

1. [Time, Clocks and the Ordering of Events in a Distributed System, Microsoft Research page for the 1978 CACM paper](https://www.microsoft.com/en-us/research/publication/time-clocks-ordering-events-distributed-system/)
2. [Final Exam Study Guide, Rutgers CS 417 (Paul Krzyzanowski)](https://people.cs.rutgers.edu/~pxk/classes/417/exam/guide-final.html)
3. [Leslie Lamport, Time, Clocks, and the Ordering of Events in a Distributed System (full text PDF)](https://lamport.azurewebsites.net/pubs/time-clocks.pdf)
4. [Lecture 7: More Virtual Clocks, University of Washington CSE 452 notes](https://courses.cs.washington.edu/courses/cse452/26sp/lectures/lec07-notes.html)
5. [Leslie Lamport's publication page with his own commentary on the 1978 paper](https://lamport.azurewebsites.net/pubs/pubs.html)
6. [A3 Distributed Systems, chapter 9, Radboud University](https://www.cs.ru.nl/~ths/a3/html/h9/h9.html)
7. [Causality Tracking Trade-offs for Distributed Storage, Universidade NOVA de Lisboa (IEEE NCA 2020)](https://novaresearch.unl.pt/en/publications/causality-tracking-trade-offs-for-distributed-storage/)
8. [CockroachDB: The Resilient Geo-Distributed SQL Database, SIGMOD 2020](https://www.cockroachlabs.com/pdf/cockroachdb-the-resilient-geo-distributed-sql-database-sigmod-2020.pdf)
9. [Spanner: Google's Globally-Distributed Database, OSDI 2012](https://research.google.com/pubs/archive/39966.pdf)
10. [Hybrid Clock, Patterns of Distributed Systems, martinfowler.com](https://martinfowler.com/articles/patterns-of-distributed-systems/hybrid-clock.html)

---
*Initially written Oct 15, 2026*

Topic: 物质世界与数学 › 测量与时间 › 计时与时间标准 › Time standards, precision, and technical time › Distributed clock synchronization algorithms

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/lamport-timestamp

English original: https://www.edgepedia.org/lamport-timestamp
