**Hybrid Logical Clocks: The Invisible Timekeeper for Causal Consistency**
Problem: āWhere Do I Get the Causal Order?ā
TLDR: š°ļø Hybrid Logical Clocks (HLCs) let each node locally stamp writes, preserving causal order across regions while keeping clocks simple.
šÆ System Scenario & Requirements
System Goal
TLDR: š°ļø Hybrid Logical Clocks (HLCs) let each node locally stamp writes, preserving causal order across regions while keeping clocks simple.
Actors & Components
- Client
- Region
- App
- KV Node 1
- KV Node 2
Functional Requirements
- TLDR: TLDR: š°ļø Hybrid Logical Clocks (HLCs) let each node locally stamp writes, preserving causal order across regions while keeping clocks simple.
- Without HLCs, you risk lost causality or heavy clock sync.
- Because every multiāregion KV store that promises causal consistency needs a lightweight, deterministic timestamping mechanism.
Constraints & Assumptions
- Constraints are derived from the rebuilt scenario stages.
ā” Execution Stages & Decision Trace
Stage 1 ā Solution: Hybrid Logical Clocks + CRDTs
Hybrid Logical Clocks combine a physical timestamp with a logical counter, producing a total order that respects realātime order and causality.
- Timestamp format : <physical ms, logical counter .
Deep Dive
Scenario context
*TLDR: š°ļø Hybrid Logical Clocks (HLCs) let each node locally stamp writes, preserving causal order across regions while keeping clocks simple. Without HLCs, you risk lost causality or heavy clock sync.
Why it matters? Because every multiāregion KV store that promises causal consistency needs a lightweight, deterministic timestamping mechanism.*
In a multiāregion, multiāmaster KV store, writes can happen conā¦
This walkthrough focuses on Problem: āWhere Do I Get the Causal Order?ā.
Before making a design choice, clarify:
- In a multiāregion, multiāmaster KV store, writes can happen concurrently on any node.
- If a client reads a value that was updated after a previous write, the system must guarantee that the read reflects that causality.
- Relying on physical clocks leads to drift, while vector clocks explode in size.
Concepts in play: consistency-replication, database-sharding
Stage 1 ā Solution: Hybrid Logical Clocks + CRDTs
- Hybrid Logical Clocks combine a physical timestamp with a logical counter, producing a total order that respects realātime order and causality.
- Timestamp format : <physical ms, logical counter .
- On write, set ts = max(local ts, incoming ts) and increment the logical counter if timestamps are equal.
- Store the resulting HLC as the writeās metadata.
Observable outcome: Store the resulting HLC as the writeās metadata.
Stage 2 ā How It Works in Practice
- When a write occurs, the node updates its local HLC with Tick(remoteHLC) before stamping the operation.
- On read, the client merges all replicasā CRDTs, using the highest HLC per key to ensure causal visibility.
// Simplified Go example
type HLC struct {
Physical int64 // ms since epoch
Counter int64
}
func (h *HLC) Tick(remote *HLC) {
// local physical clock
now := time.Now().UnixMilli()
maxPhysical := max(now, remote.Physical)
if maxPhysical == h.Physical && maxPhysical == remote.Physical {
h.Counter = max(h.Counter, remote.Counter) + 1
} else {
h.Counter = 0
}
h.Physical = maxPhysical
}
Observable outcome: On read, the client merges all replicasā CRDTs, using the highest HLC per key to ensure causal visibility.
Stage 3 ā Tradeāoffs
- Pros : No global clock sync; compact metadata; deterministic causality.
- Cons : Requires careful handling of clock drift; HLCs can still be out of sync if physical clocks diverge significantly, but the logical counter mitigates this.
- Bottom line : HLCs are the lightweight, reliable backbone that lets a multiāregion, multiāmaster KV store enforce causal consistency while keeping metadata minimal and operations fast.
Observable outcome: Bottom line : HLCs are the lightweight, reliable backbone that lets a multiāregion, multiāmaster KV store enforce causal consistency while keeping metadata minimal and operations fast.
End-to-end outcome
In a multiāregion, multiāmaster KV store, writes can happen concurrently on any node.
Alternatives considered
The source focuses on one primary design. A second architecture is not compared, so no alternative is invented here.
Failure and degraded modes
The source does not isolate a separate failure mode. Limits that matter for the scenario are called out in the stage decisions above.
Key learnings
- Hybrid Logical Clocks combine a physical timestamp with a logical counter, producing a total order that respects realātime order and causality.
- When a write occurs, the node updates its local HLC with Tick(remoteHLC) before stamping the operation.
- Pros : No global clock sync; compact metadata; deterministic causality.
