Skip to main content

MVCC and Transactions

MVCC (multi-version concurrency control) means that several versions of each key are stored. A value isn't overwritten in place. Kahuna uses MVCC to store the proposed values of a transaction next to the committed value. It also uses MVCC to retain older committed revisions for historical reads. This page gives the per-key details behind transaction conflicts and snapshot reads. These details include MVCC entries and write intents, and revisions and the snapshot floor that protects long-lived historical reads.

Transaction Lifecycle describes the transaction coordinator and operation registration. It also describes the finalization fence and two-phase commit. For the design and when to use each mode, see Distributed Transactions.

Transaction IDs​

Transactions use Hybrid Logical Clock (HLC) timestamps as transaction IDs. HLC timestamps provide an ordering that works across nodes and preserves causal progression. MVCC entries and write intents are keyed by this ID.

MVCC Entries​

KeyValueEntry stores the current value and metadata for a key. It can also hold MvccEntries, keyed by transaction ID. Each KeyValueMvccEntry contains a proposed or versioned value. It also contains revision and expiration metadata, with state and HLC metadata.

This lets Kahuna keep transactional state separate from the committed current value until the transaction commits.

The first latest transactional point read pins a committed value or absence per key, including a committed unsettled intent. These pins do not form a common transaction-start snapshot. Follow-up point reads or writes can abort immediately if the observation is stale. Finalization also validates registered dependencies.

Snapshot reads are different. A read at a fixed HLC timestamp selects the newest revision whose commit timestamp is at or before that timestamp. Because it is a historical view, it does not create a live read dependency for latest-state validation.

In this example (timestamps simplified), revision 3 committed after the read timestamp. This means that a read at T = 25 returns revision 2.

Write Intents​

A write intent records that a transaction intends to modify a key. Kahuna uses write intents to prevent conflicting updates and to make commit/rollback deterministic.

There are three important levels.

  • Key write intent: protects a single key.
  • Prefix write intent: protects a bucket or prefix, which matters for operations such as get by bucket.
  • Range write intent or lock: protects an ordered interval, which matters for bounded range reads.

Prefix and range locks block conflicting mutations while held. They are leader-local and can disappear on failover. Range renewal is best-effort. For these reasons, validation remains necessary.

Pessimistic transactions acquire these locks before or during operations. Optimistic transactions don't take exclusive locks for reads. They validate read observations and concurrent write intents at commit. See Locking Modes.

Unprepared session-owned write intents and no-expiry range locks have a liveness ceiling. If the owning transaction disappeared and the ceiling has passed, the next touch can drop the orphaned state. The state then can't block reads or scans forever. Prepared durable intents are exempt because their outcome belongs to the canonical decision record.

Revisions and Snapshots​

Each key tracks a revision counter. Reads can request a specific revision, and recent revisions can be cached. Transactions use revision and HLC metadata to decide which value is visible and whether a concurrent write invalidates a commit attempt.

SET ... NOREV advances the current revision as usual, but it skips the archived historical revision record on purpose. That reduces write amplification for cache-style values, but it also means a skipped revision cannot be served later by GET ... AT <revision> or by a historical snapshot read that needs that exact archived version.

Snapshot Floor​

The MVCC snapshot floor protects historical reads that must remain valid for longer than the normal revision-retention window. A client acquires a leased snapshot hold at timestamp T; while that hold is live, cleanup must retain the revision that was visible at or before T, and all newer revisions.

The reported effective floor is the minimum timestamp across live snapshot holds. The pruning floor includes all registered snapshot holds, even expired ones, until replicated release or purge. Hold state is replicated through partition 0 (the system partition) and restored from local snapshots. Acquire and renew operations can enter through any node, and so can release operations. They are routed to the partition 0 leader before they are committed.

The floor constrains both revision cleanup paths.

  • In memory, Kahuna retains the normal newest RevisionRetention revisions and the floor boundary. It also retains committed archive revisions that aren't flushed to the backend yet. Unflushed revisions can exceed the retention target.
  • On disk, persistent revision cleanup must not delete the boundary revision or anything newer, even when count-based or age-based retention would otherwise remove it.

Historical reads first try the in-memory archive. If the requested timestamp is older than the in-memory window, persistent read paths use the on-disk revision history. All historical read types use the same rule, for example point reads and prefix scans. They return the newest revision whose commit timestamp is at or before the requested snapshot timestamp.

When a snapshot floor pins a boundary revision, the in-memory archive can become the pinned boundary plus the newest retained revisions. If the revisions between them were trimmed from memory, a boundary hit in that gap is treated as a cache miss. The read then uses persisted revision history. This prevents a held floor from returning an older value when a newer disk-only revision is visible at the requested timestamp.

Large snapshot scans can continue through backend revision history. They don't load all scanned keys back into the hot actor cache. This means that historical analytics and recovery-style reads don't displace current working-set entries.

Persistent cleanup is budgeted. A targeted cleanup pass deletes at most PersistentRevisionCleanupBatchSize records and stops when PersistentRevisionCleanupTimeBudget expires, leaving remaining keys queued for later. This prevents revision pruning from monopolizing the persistence writer when a small set of keys has very deep history.

After restart, durable snapshot holds are loaded with a startup grace window before expired leases are purged. That grace protects a timestamp long enough for a holder to renew after full-cluster downtime. The underlying historical revisions must be present.

The hold API is described in Snapshot Holds.

Historical Read Fences​

Snapshot reads can step over an open foreign writer by raising its commit floor to the requested timestamp: the writer must commit strictly later. Prepared or commit-claimed intents can't be pushed. They can cause safe-time waits when their commit could fall in the snapshot. See Fixed-timestamp reads. The serving actor folds eligible read timestamps into its HLC, and durable commit timestamps are minted above the maximum participant staged timestamp. A read more than five seconds ahead of the serving HLC returns MustRetry, and it isn't served unfenced; kahuna.kv.snapshot_clock_fence_refused_total counts refusals. Retry the same cluster-minted timestamp once the clock catches up; a caller-created future timestamp may remain refused.

Disk-history fallback returns MustRetry while needed committed revisions are unflushed. kahuna.kv.revisions.retained_unflushed_total counts retention beyond the memory target; kahuna.kv.revisions.history_reads_fenced_total counts fenced fallback reads. TTL uses the current read time. It doesn't use historical time. Snapshot holds protect retention and cannot recreate pruned or NOREV history.

For newer-head precedence over lingering intents and range-lock compatibility, see Transaction Reads and Locks. Durable Settlement covers the optional materializing-resolve encoding.

The shared default for in-memory RevisionRetention is 4 in all configurations: core, embedded, server. --revisions-to-cache applies on clustered servers, and values below 1 aren't allowed. Persistent keys can consult older disk history subject to retention/fences; ephemeral keys cannot recover older revisions after memory history is trimmed.