Unit 2 of 12 · about 4 hours · derive, implement
Happens-before, Lamport clocks and total-order tie breaking
Causal order is what a system can know without synchronized clocks. Lamport stamps are the cheapest way to label it, and their limits motivate every later clock in this section.
You'll be able to
- Define happens-before from its three rules and decide for event pairs whether one precedes the other or they are concurrent.
- Compute Lamport timestamps for a small message history with rules IR1 and IR2, and state which orders the stamps prove and which they leave open.
- Build the total order with node-id tie breaking and explain why it is causal yet arbitrary on concurrent events.
Covers
Read
In this order, only the parts listed
0 of 2 done
- Partial order, logical clocks and the total orderTime, Clocks, and the Ordering of Events in a Distributed System (Lamport, 1978)
The Partial Ordering (happens-before rules 1 to 3), Logical Clocks (Clock Condition, C1, C2, IR1, IR2) and Ordering the Events Totally with the five-rule mutual-exclusion algorithm
Skim Anomalous Behavior and Physical Clocks on first pass. They motivate bounded physical clocks, which return in the hybrid clock unit.
- Happens-before and Lamport clocks workbookCambridge Concurrent and Distributed Systems Notes (Kleppmann) - Time, Clocks and Ordering
Section 3.3, Slides 60 to 64: message reorder example, physical timestamps against causality, happens-before and concurrency; Section 4.1, Slides 65 to 68: Lamport algorithm, properties and total order; Exercises 8, 10 and 11
Focus on
- Happens-before has three rules: same-node order, send before matching receive, and transitivity. Concurrent means no message path either way, not simultaneity.
- IR1 increments the clock between successive events. IR2 attaches the send stamp to the message, and the receiver moves past and adds one, so the receive itself counts as an event.
- The Clock Condition says implies . The converse is false. Distinct events with equal stamps are concurrent when the clock rules hold.
- Order lexicographically: compare timestamps first and use node IDs only for ties. This extends happens-before but imposes an arbitrary order on concurrent events. Persist the clock or establish a safe recovery epoch before restarting; ordered eventual delivery is an extra assumption of the paper's mutual-exclusion protocol, not of timestamp assignment.
Common mistakes · 3
- Reading as proof that a caused b. It only rules out b happening before a; a and b may be concurrent.
- Skipping the plus one on receive. Then successive receives of same-stamped messages stop advancing the clock and C1 breaks.
- Assuming the paper's mutual-exclusion algorithm works over a real network. It assumes every message is eventually received in order, so a real build must add sequence numbers and retransmission.
Try it yourself
Closed-book, before checking anything
Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.A sends m then has a local event; B receives m. Which pairs are ordered and which are concurrent?Answer
Send precedes receive and each node's own events are ordered. B's receive is concurrent with A's later local event, since no message connects them.
2.Stamps read and . Could a have caused b?Answer
Possibly, but not proven. The stamps rule out b happening before a and leave happened-before versus concurrent open.
3.Two events share stamp 3. Are they the same event, ordered, or concurrent?Answer
If they are distinct events and the Lamport rules hold, they are concurrent: happens-before would require strictly increasing stamps. Equal stamps alone do not identify an event; the timestamp and node ID together do, assuming no reuse after restart.
4.Why does the total order need node names, and what does it settle for concurrent events?Answer
Names make every pair comparable so each event has one place in line. For concurrent events the order is deterministic but arbitrary, while happened-before pairs always keep their order.
Practice
- Advance a Lamport Logical Clock
Implement
timestamps: a 0 entry adds 1, a positive entry sets the clock tomax(clock, entry) + 1; the worked examples map[0,5,0]to[1,6,7]This drills the Slide 66 rule exactly. State the rule from memory before you open the page.
You're done when
You stamp the reproduce history and the Exercise 10 diagram without notes, and your simulator returns the problem's example outputs with the rule stated from memory.
Only when you can do this without notes.
Leads to
Notes
Resources · 2 for this unit, 12 more in the section
- Time, Clocks, and the Ordering of Events in a Distributed System (Lamport, 1978)Leslie Lamport · required
Read the partial ordering and logical clocks sections: happens-before, the Clock Condition, rules IR1 and IR2, and the total order. Its ties are broken arbitrarily, so the total order is not causality.
- Cambridge Concurrent and Distributed Systems Notes (Kleppmann) - Time, Clocks and OrderingUniversity of Cambridge (Martin Kleppmann) · support
Use Chapter 3 (physical clocks, NTP and monotonic clocks, happens-before) and Section 4.1 (Lamport and vector clocks with worked examples) as the derivation workbook: work Exercises 7 to 13. Each section marks where its public lecture video starts.
Also in this section
- DDIA 2e - Chapter 9 The Trouble with Distributed SystemsMartin Kleppmann and Chris Riccomini · primary
Read clocks, pauses, unreliable networks, partial failures and knowledge limits. For each failure, record what a node observes, what is true globally and what action is safe under that uncertainty.
- Logical Physical Clocks and Consistent Snapshots (Kulkarni et al., 2014)University at Buffalo · support
Read the hybrid logical clock algorithm and its bounds; the paper reports the logical counter staying well below its proved bound in practice, so measure your own.
- SWIM: Scalable Weakly-consistent Infection-style Process Group Membership (Das et al., 2002)Cornell University · support
Read the direct and indirect probing detector, infection-style dissemination, the suspicion mechanism and incarnation numbers. Suspicion timeouts depend on the workload.
- The phi Accrual Failure Detector (Hayashibara et al., 2004)JAIST · support
Read how phi is computed from the heartbeat inter-arrival history and how a threshold trades detection time for false suspicion. Its normal approximation misleads under bursty delay, so test with pause traces too.
- How to Do Distributed Locking (Kleppmann, 2016)Martin Kleppmann · required
Read the process-pause, network-delay and fencing-token sections with DDIA 2e Chapter 9 "Distributed Locks and Leases". Treat the Redlock critique as dated to 2016; the lasting lesson is that the storage service must reject a write that carries an older token.
- Building Distributed Systems - The TimekeeperBuild Distributed Systems · optional
Only the first three tasks are free (clock reads and backward jumps, a monotonic wrapper, a split-brain lease). Tasks are graded in the browser after login, so treat a pass as a drill and keep your local harness as the evidence.
- Building Distributed Systems - The GossiperBuild Distributed Systems · optional
Only the first three tasks are free (basic broadcast, tree topology, random gossip). Tasks are graded in the browser after login, so treat a pass as a drill and keep your local harness as the evidence.
- HLD Handbook - Clocks, Failure Detection and Consistent HashingHLD Handbook · required
Read Part 3 clocks and ordering, failure detection and consistent hashing. Use its exercises to compare assumptions, then verify claims against your own histories.
- Gossip Glomers - Unique IDs and BroadcastFly.io and Jepsen · required
Each challenge runs locally under Maelstrom (v0.2.4; the pages were tested on 0.2.3) with its checker and, where the page says so, the partition nemesis; keep the run directory as the history. Do challenge 2 (unique IDs under partition with total availability) and 3a to 3c (broadcast, then with partitions). A pass shows uniqueness or convergence in that run, not ordering or recency.
- MIT 6.5840 - Failure and Paper Discussion LensMIT PDOS · support
Use the RPC, GFS, Raft and consistency lectures to identify which timing assumptions affect progress and which invariants must not depend on time. Do not start the Raft lab yet.
- Replicated Log and Stream Processor - Time and Membership SliceRoadmap-Final · support
Add incarnation IDs, logical timestamps, gossip membership, suspicion metrics and fenced ownership. Retain seeds and minimized traces for every false-failure or stale-actor case.
- AlgoMaster System Design practice - clock, gossip and lease drillsAlgoMaster · support
Use only the named problems: Advance a Lamport Logical Clock, Compare Two Vector Clocks, Update a Vector Clock on Receive, Advance a Hybrid Logical Clock, Simulate Gossip Rumor Spread and Implement a Lease Lock with Fencing Tokens. Each states its function signature and checks small deterministic cases.