Unit 4 of 12 · about 4 hours · implement

Hybrid logical clock state, persistence and implementation

Vectors cost a slot per node. HLC keeps causality in constant space while staying near wall time, which is what database timestamps use in practice.

You'll be able to

  • Run the Figure 5 send and receive rules on a short trace and explain what each of and records.
  • State the proved bounds on and on with the synchronization assumption each one needs.
  • Persist HLC state across restarts and bound NTP steps with the reset and ignore-out-of-bounds rules.

Covers

Hybrid logical clock state and persistenceLogical or hybrid clock implementation
1

Read

In this order, only the parts listed

0 of 2 done

  1. HLC algorithm, bounds and resilienceLogical Physical Clocks and Consistent Snapshots (Kulkarni et al., 2014)

    Section 3.3 with Figure 5 (send, local and receive rules), Theorems 1 to 4 with Corollaries 1 to 4, Section 3.4 (superposition on NTP), Section 4.1 (reset and ignore rules) and Section 5.1 (AWS c-value table)

    Read the paper as version TR 2014-04. Work the naive-algorithm counterexample in Section 3.2 before Figure 5, so the missing plus one reads as the fix.

  2. HLC summary and deployment numbersHLD Handbook - Clocks, Failure Detection and Consistent Hashing

    Theory, Hybrid Logical Clocks: state pair, 64-bit packing and the CockroachDB, YugabyteDB and MongoDB users

    Use as review after the paper. Its one-line receive rule glosses the four cases of Figure 5, so trust the paper for implementation.

2

Focus on

  • State is . On a local event or send, becomes ; increments if is unchanged and resets otherwise. On receive, becomes , and follows the four-way rule for which of the three inputs won.
  • Comparison is lexicographic. Theorem 1 gives causality one way: happened-before implies a smaller stamp, never the reverse, exactly like Lamport.
  • Theorem 2 keeps at or ahead of , and Corollary 1 bounds by the synchronization uncertainty . Theorem 4 reads as the length of a same- causal chain, which Corollary 3 bounds further.
  • HLC reads but never adjusts the physical clock. Section 4.1 resets to after a bound violation and ignores out-of-bounds messages, raising an alert. These are recovery actions outside the normal causality proof, not permission to rewind timestamps silently. Section 5.1's counter frequencies belong to its AWS and NTP setup, not yours.
Common mistakes · 3
  • Reading as proof of causality. Even with the paper's clock-error bound, a smaller HLC stamp does not establish happens-before.
  • Persisting only . If physical time has not advanced, resetting can reuse or rewind a stamp. Persist the pair before exposing it, or reserve a durable range that recovery skips.
  • Packing the pair into 64 bits and comparing the packed integer across layouts. Decode to first, since layouts disagree on the split.
3

Try it yourself

Closed-book, before checking anything

Closed-book, run Figure 5 from : local event at gives , another local at gives , a receive at of remote gives , and a local at gives . Name which case of the receive rule fires each time.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.A local event sees with state . What is the new stamp, and why does it not follow the wall clock down?Answer

    . The maximum keeps 7, and the counter records the event at that level, so stamps stay monotonic across the backward step.

  2. 2.What do Theorems 2 and 3 together say about a stamp with far ahead of ?Answer

    Some causally prior event saw a physical clock equal to that . A large gap means the node learned a fast remote clock, not that its own clock is wrong.

  3. 3.Why does the paper bound by chain length rather than by node count in practice?Answer

    Theorem 4 ties each count to a distinct same- causal predecessor. Long same-millisecond chains are rare, so measured stays in single digits while the node-count bound is loose.

  4. 4.An NTP step pushes an hour forward. What fires, and what is lost?Answer

    The normal local rule adopts the larger physical time and resets the counter. Other nodes may reject the resulting far-ahead messages under Section 4's guard. A forward jump alone need not violate the local bound, but it breaks the assumed synchronization envelope; the normal cross-node guarantees cannot simply be claimed.

5

Practice

  • Advance a Hybrid Logical Clock

    Implement local and receive per the four-way rule, including unchanged and backward physicalTime; the second example resets the counter when physical time strictly wins

    The page's rule matches Figure 5. Add persistence of the pair and the reset-and-ignore policy around it in your own harness.

You're done when

You hand-run the reproduce trace with the correct receive case each time, and your implementation passes the problem's examples with unchanged and backward physical times.

Only when you can do this without notes.

Leads to

Notes

Resources · 2 for this unit, 12 more in the section

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.

  • 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.

  • 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.

  • 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.

  • 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.

  • 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.