Overview
Timestamps from physical clocks can be inconsistent with causality, even if those clocks are synchronized using NTP
In contrast, logical clocks are designed to capture causal dependencies by counting the number of events occurred
Lamport Timestamps
- Each node maintains a timestamp (counter):
- On any event occurring at the node:
- On sending the message : ;
- On receiving :
The algorithm assumes a crash-stop model (or a crash-recovery model if the timestamp is maintained in a permanent storage)
Let be the value of after the event
- If then
- However, does not imply ; Possible that for
Ordering
Using Lamport timestamps we can extend the happens-before partial order into a total order
We use the lexicographic order over (timestamp, node name) pairs: that is, we first compare the timestamps, and if they are the same, we break ties by comparing the node names:
- Let be the name of the node at which event occurred. Then the pair uniquely identifies event
- Define a total order using Lamport timestamps:
Properties:
- This relation puts all events into a linear order: for any two events we have either or
- It is a causal order:
- Given Lamport timestamps and with we canβt tell whether or ; That is, we canβt tell whether two events are concurrent
Vector Clocks
Vector clocks allow us to detect when events are concurrent. While Lamport timestamps are just a single integer (possibly with a node name attached), vector timestamps are a list of integers, one for each node in the system
- Assume nodes in the system:
- Each node maintains a vector timestamp of size ; Initially all zeros:
- On any event occurring at node :
- On sending the message at node : ;
- On receiving at node : for every ;
Ordering
Define the following order on vector timestamps in the system with nodes:
- iff for all
- iff for all
- iff and
- iff and
Define vector timestamp of event as
iff
The partial order over vector timestamps corresponds exactly to the partial order defined by the happens-before relation
Dotted Version Vectors
References
- Distributed Systems Notes. Martin Kleppmann
- Distributed Systems 4.1: Logical time - YouTube
- Designing Data-Intensive Applications: The Big Ideas Behind Reliable, Scalable, and Maintainable Systems. Martin KleppmannTODO
- Understanding Distributed Systems (2nd ed). Roberto VitilloTODO
- Distributed Systems 5.1: Replication - YouTubeTODO