0023-atomic-storage-commit.md 7.2 KB

Atomic storage commit, retiring the write-ahead saga and legend

  • Status: accepted
  • Authors: Cesar Rodas
  • Date: 2026-08-12
  • Targeted modules: kuatia-storage, kuatia-storage-sql, kuatia (ledger, saga)
  • Associated tickets/PRs: N/A

Context and Problem Statement

ADR-0003 made storage a set of simple single-update primitives. It moved crash-safety into a write-ahead record with tracked phases, which is PendingSaga or PendingTransition, plus a roll-forward recovery in Ledger::recover.

A commit was a sequence of primitives, reserve → deactivate → insert → store_transfer → append_event, and they shared no transaction. So the write-ahead log was the only protection against a half-applied commit.

That design also left the overdraft floor and the freeze and close guards best-effort under concurrency. It re-checked them just before the writes, and not atomically with them.

Two things brought that design back up for review.

First, the legend saga machine had already shrunk to two thin adapters that only a test used. The path for one commit was a linear method and not a saga.

Second, the whole write-ahead machinery existed because the store could not commit a transfer atomically. If it can, then the two-step reservation, the write-ahead record and the recovery are all unnecessary.

So must the store apply a whole transfer atomically, and reverse the per-primitive model of ADR-0003?

Decision Drivers

  • Simplicity where it is safe. Kuatia targets a single transactional database (SQLite or PostgreSQL) or an in-process store. On each, a whole-commit transaction is available and is the simplest correct primitive.
  • Strict guards. The overdraft floor and the freeze and close checks must hold atomically with the write, and not as a best effort.
  • No orphaned state. An all-or-nothing commit leaves nothing for recovery to reconcile.
  • One place for correctness. Validation stays in the pure core. The store enforces the stateful guards inside its transaction. There is no third home to keep in step, and the saga's count contract was that third home.

Considered Options

Option 1: Keep ADR-0003 (dumb primitives + write-ahead recovery)

Pros:

  • Good, because it composes across several resources and several shards, and it needs no database transaction.

Cons:

  • Bad, because the write-ahead record, the tracked phases and the roll-forward recovery are a large surface. That surface exists only to join primitives that are not atomic.
  • Bad, because the floor, the freeze guard and the close guard all stay best-effort under concurrency.

Option 2: Atomic storage commit (chosen)

Add one CommitStore sub-trait, with commit_envelope and commit_transition. Each one applies in one store transaction. On SQL that is one BEGIN..COMMIT. In memory it holds every relevant lock across the apply.

The pure core still validates first. The transaction then re-checks the three stateful guards inside itself:

  • Double-spend. The delete-affected-count is the atomic single-winner claim.
  • Freeze and close.
  • The overdraft floor. Rust sums it from the live rows, and SQL never does.

Pros:

  • Good, because it deletes the two-step reservation, PendingSaga, PendingTransition, Ledger::recover and the SagaStore keyspace. A crash cannot half-apply a commit, so there is nothing to recover.
  • Good, because the floor, the freeze guard and the close guard all become strict. That removes the best-effort consequence of ADR-0003.
  • Good, because legend and the count-contract helpers go. A composition of several transfers is then a plain runner, run_movements, over commit and reverse, which unwinds in last-in-first-out order.

Cons:

  • Bad, because crash-safety now rests on a transaction inside one backend. It composes across neither a shard nor a second database. That closes the cross-shard UTXO ambition that ADR-0002 and ADR-0003 kept open, and this record accepts that trade for a single-database library.
  • Bad, because pool.begin() on SQLite is a deferred transaction. The delete-affected-count stays the backstop against a double-spend, which is the same property the earlier design depended on.

Decision Outcome

Chosen: Option 2. The store owns the atomic commit boundary. The reservation index, the write-ahead recovery, SagaStore and legend all go.

This supersedes ADR-0003. It refines ADR-0002, because there is no saga pipeline for one commit any more. The atomic commit_transition takes over the transition recovery of ADR-0020. The commit-safety map of ADR-0021 moves the double-spend guard, the floor and the freeze and close guards into commit_envelope.

Positive Consequences

  • Fewer parts. There is no write-ahead log, no recovery and no saga machine.
  • The floor, the freeze guard and the close guard are all strict.
  • One home for validation, the pure core, plus one home for enforcement, the store transaction.

Negative Consequences

  • There is no commit across a shard and none across several resources. If anything ever needs one, that is a new design and not a change to this one. The next section says where it belongs.
  • The commit path no longer uses the general reserve_postings and release_postings primitives. They stay as code that the conformance suite tests, and a later change can remove them.

When a saga is the right tool (and why not here)

The removal of the saga is not a verdict against the pattern. A saga earns its keep when a workflow spans separate pieces that cannot share one transaction. Several services are such pieces, and so are several databases, and so is a sharded store where one commit cannot reach every partition.

There a saga sequences the local steps, and on a failure it compensates the steps that finished. It buys atomicity that the resources underneath cannot give on their own. It pays for that with a write-ahead record, tracked phases and compensation logic.

Kuatia was not that case. Every primitive that the commit saga drove lived in one store, behind one transactional boundary, and those primitives were reserve, deactivate, insert, store_transfer and append_event.

So the saga coordinated pieces that were coupled already. It re-implemented in application code the atomicity that the database gives for free. Where one ACID transaction is available, that transaction is the simpler and the stricter primitive, and a saga around it is ceremony.

The escape route stays open, and it belongs one layer down. A storage backend can one day shard its data, so that the postings of a transfer span partitions that no single transaction covers. That backend then implements CommitStore::commit_envelope with a saga inside itself. A two-phase commit is one way, and a local transaction per shard plus compensation is another.

The CommitStore contract says to apply the whole envelope atomically or to reject it. It says nothing about how a backend reaches that atomicity.

So the saga then lives where those separate pieces are. The ledger core knows nothing of it, and still calls commit_envelope and maps the outcome. The pattern moves to the layer that has the distribution problem, and it leaves the core, which does not have one.