Skip to main content

Hybrid Logical Clocks (HLC)

Each node has a separate clock, and clocks drift apart. If Kahuna ordered events by each node's wall clock alone, a later write on a node with a slow clock could look older than an earlier write on another node. Hybrid Logical Clocks (HLCs) solve this problem. Their timestamps are close to real time, but they always move forward and respect cause and effect. This means that Kahuna can order events across nodes safely.

An HLC combines physical time with a logical counter. Timestamps keep moving forward even when several events happen in the same millisecond or when clocks are slightly skewed.

You use HLC timestamps directly in historical AS OF reads and snapshot holds. Point-in-time restore targets and the hlc() script function also use them. Their physical part is Unix epoch milliseconds. This means that you can use a wall-clock time as a target.

HLCs have these uses in Kahuna.

  • transaction IDs
  • MVCC visibility (which stored version of a key a read sees)
  • historical AS OF reads
  • snapshot holds
  • leases and expiration metadata
  • replication and restore metadata
  • durable transaction decision records and completion receipts

Raft remains the source of truth for committed ordering in each partition, and actors serialize local state on each node. HLCs provide the time model used by MVCC and cross-partition coordination.

HLC Format in Kahuna​

A Kahuna HLC carries three components.

HLC = (nodeId, physicalTimeMillis, logicalCounter)
  • nodeId: Node component used to keep generated timestamps unique across nodes.
  • physicalTimeMillis: Wall-clock time in milliseconds.
  • logicalCounter: Counter used when physical time does not advance between events.

This lets timestamps remain monotonic for a node even under rapid successive events or modest clock skew.

Transaction Timestamps​

Each transaction receives an HLC transaction ID. That ID is carried through the transaction. It appears in the transaction state, for example in MVCC entries and durable decision records.

HLCs support transaction behavior in several places.

  • On begin: The transaction receives an HLC identity.
  • On reads: MVCC chooses the version visible to the transaction or requested snapshot timestamp.
  • On commit: Participant writes receive committed HLC metadata through the partition commit path.
  • On recovery: Durable decision records and completion receipts are keyed by transaction HLC.

Transaction isolation comes from several mechanisms together, for example HLCs and MVCC, locks and write intents, and two-phase commit. Read validation and Raft ordering also contribute. Which guarantee you get depends on the locking mode and read options; see Transaction Reads and Locks.

MVCC Visibility​

Kahuna uses HLC metadata as version timestamps for key/value records. This example shows a simplified record.

{
"transactionId": { "node": 1, "physical": 1713351023980, "logical": 3 },
"key": "user:123",
"value": "..."
}

MVCC reads filter out versions that are in the future relative to the transaction timestamp or requested snapshot HLC. Historical reads such as GET key AS OF <timestamp> return the newest revision whose commit timestamp is at or before the requested time.

On the write path, the leader applies a committed mutation with its HLC metadata, followers apply the same committed operation from the Raft log, and later MVCC and snapshot reads use that metadata to decide visibility.

HLC visibility alone does not prevent phantoms: keys that another transaction adds to a bucket or range after you read it. Preventing them depends on prefix/range locks under pessimistic transactions or validation under optimistic transactions.

Conflict Detection​

Kahuna does not use last-write-wins semantics for committed key/value state.

Conflicts are handled in other ways.

  • Raft orders committed mutations per partition.
  • Compare-value and compare-revision operations reject stale updates explicitly.
  • Pessimistic transactions use locks and write intents to block conflicts.
  • Optimistic transactions validate read observations and concurrent write intents at commit.
  • HLC metadata determines MVCC visibility and historical snapshots.

This gives applications explicit conflict behavior. Kahuna doesn't silently choose the write with the largest timestamp.

Locks, Expiration, and Sequencing​

HLCs also appear in coordination metadata. This means that time-based behavior doesn't depend on perfectly synchronized clocks.

  • Leases use HLC-based expiration metadata.
  • Sequences store update metadata and idempotency records through the key/value subsystem.

Fencing tokens are not HLC timestamps. They are per-lock counters that increase each time the lock changes hands. Protected resources can then reject stale lock holders.

HLC Timeline​

The example below shows why a receiving node doesn't simply use its local wall clock. Node 2's clock is 2 ms behind Node 1. Timestamps are shown as (physical, logical); the node component is omitted.

Node 2 doesn't issue a timestamp lower than one that it already saw. This means that the replicated write is ordered before all later actions of Node 2, even though Node 2's wall clock is behind.