Hybrid Logical Clocks & Causality
Distributed and off-grid databases cannot rely on physical wall clocks (NTP). Device system clocks drift, can be set backwards by users, and disagree across peers.
AeroLog integrates Hybrid Logical Clocks (HLC) as a first-class engine primitive, based on the formalization by Kulkarni et al.
Structure of an HlcTimestamp
Every record header in AeroLog contains an 18-byte monotonic clock coordinate:
pub struct HlcTimestamp {
pub physical_time: u64, // Epoch time in milliseconds
pub logical_counter: u16, // Sub-millisecond event sequence
pub node_id_hash: u64, // Deterministic peer tie-breaker
}
Total Causal Ordering
AeroLog achieves a strict total ordering across all mutations using tuple comparison:
Where:
1. physical_time tracks the passage of wall-clock time.
2. logical_counter increments when multiple mutations occur within the same physical millisecond.
3. node_id_hash provides a mathematically deterministic tie-breaker if two independent nodes write during the exact same logical counter tick.
Forward Clock Ratcheting
When an AeroLog node receives remote changes during peer synchronization:
sequenceDiagram
participant NodeA as Node A (Local)
participant NodeB as Node B (Remote)
NodeB->>NodeA: Send ChangeRecord<br/>(HLC: pt_b, lc_b, node_b)
Note over NodeA: Update Local HLC:<br/>max(local_pt, remote_pt, wall_clock)<br/>Increment logical counter if equal
NodeA->>NodeA: Apply ChangeRecord to<br/>Segment & KeyDir
- The local clock generator observes
(pt_b, lc_b). - The local clock updates its internal state: \(\text{next\_pt} = \max(\text{local\_pt}, \text{remote\_pt}, \text{current\_wall\_clock})\).
- This guarantees that any subsequent write produced locally is strictly greater than all previously observed remote events, ensuring causal consistency across wireless mesh networks.