Skip to main content
PreparationZone Logo
PrepZone

Lamport Timestamps and Vector Clocks

Establish happened-before without synchronized clocks — detecting concurrent ShardPay transfers.

Pass checkpoint below

Why this matters

  • Causal consistency requires tracking dependencies between events, not just timestamps.
  • Vector clocks grow with node count — use them in bounded domains like a single shard's in-memory buffer, not cluster-wide.
  • Interviewers distinguish happened-before from concurrent and ask how you resolve ties.
  • Misordering audit events triggers false compliance alerts and expensive manual reconciliation.
Event AL=1
Event BL=2
Event CL=2
Lamport timestamps establish happened-before without wall-clock sync.

Lamport logical clocks

Each ShardPay ledger node maintains a Lamport counter. On a local event the counter increments. Before sending a message the node increments again and attaches the timestamp. On receive the node sets its counter to max(local, received) + 1.

If event A's Lamport timestamp is less than event B's, then A might have happened before B — but equality does not prove concurrency; it may mean a tie that requires a secondary key. ShardPay breaks ties with node_id lexicographic order so every pair has a total sort for audit export.

Walkthrough: cross-shard transfer chain

Account A on shard 1 sends funds to account B on shard 2, which immediately forwards to account C on shard 3. Three nodes emit audit events. Lamport timestamps propagate through the RPC chain: debit on shard 1 gets (5, node-1), credit on shard 2 receives (5) and emits (6, node-2), debit on shard 3 emits (7, node-3). Compliance export sorts 5 < 6 < 7 and sees the correct causal chain even though wall clocks on the three nodes differ by 400ms.

Key points

  • Lamport timestamp — a single integer per node that respects happened-before: if A → B causally, then L(A) < L(B). ShardPay stamps every audit row with Lamport time so cross-shard exports sort causally without a central clock; ties use node_id.
  • Happened-before (→) — a partial order: A → B if A caused B directly or transitively. ShardPay's dispute tooling filters "all events that happened-before this chargeback" using Lamport order plus transfer-id linkage.
  • Concurrent events — neither A → B nor B → A; Lamport timestamps may be equal or incomparable without extra structure. Two unrelated merchant payouts on different shards are concurrent; ShardPay does not force a global order between them.
  • Vector clock — an array of counters, one per node, that detects true concurrency: if neither vector dominates the other, events are concurrent. ShardPay uses 8-element vectors inside a single shard's conflict buffer for optimistic balance updates before commit.
  • Causal delivery — a messaging guarantee that if send(A) happens-before send(B), consumers observe A before B. ShardPay's audit relay tags Kafka headers with Lamport time so downstream SIEM consumers respect causal order per partition key.

The rest of this guide is part of Pro

Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.

Still to come · 2 sections

  • Vector clocks for concurrency detection
  • Practical limits and merge rules

Test yourself

Answer these before moving on — recall is what makes it stick.