0006-reservation-protocol-posting-lifecycle.md 5.4 KB

Reservation protocol and the posting lifecycle

  • Status: superseded by ADR-0025
  • Authors: Cesar Rodas
  • Date: 2026-06-29
  • Targeted modules: kuatia-types (PostingStatus, ReservationId), kuatia-storage, kuatia (saga)
  • Associated tickets/PRs: N/A

Context and Problem Statement

A commit must consume each input posting exactly once. That holds while many commits run at the same time. It also holds because the commit itself is a multi-step saga (ADR-0002) over a simple store (ADR-0003), with no global transaction.

Two sagas must never both spend one posting. A posting that one saga reserved must not be finalized or released by another. So how does a commit get exclusive, recoverable ownership of its inputs without a lock on an account balance?

Decision Drivers

  • Double-spend safety: a posting can be consumed at most once, unconditionally.
  • No lock on a hot row: keep the UTXO concurrency of ADR-0001, and do not serialize on an account balance.
  • It survives the saga's lifetime: a reservation must hold across reserve → validate → finalize, and across a crash and the recovery after it.
  • Ownership: only the saga that reserved a posting can finalize it or release it.
  • It fits simple storage: one atomic conditional update expresses it, and that update returns an affected-row count (ADR-0003).

Considered Options

Option 1: Database row locks (SELECT … FOR UPDATE) per posting

Lock the posting rows for as long as the commit runs.

Pros:

  • Good, because it gives strict mutual exclusion within one database transaction.

Cons:

  • Bad, because it needs a transaction that covers the whole commit, and the saga model avoids that deliberately (ADR-0002 and ADR-0003).
  • Bad, because a held lock blocks another worker, and it does not survive a crash. The lock then goes, and no record says that something claimed the posting.
  • Bad, because it ties the design to a transactional store that takes a lock.

Option 2: Optimistic balance CAS per account

Guard each commit with a compare-and-set on the account balance.

Pros:

  • Good, because it avoids long-held locks.

Cons:

  • Bad, because it serializes on a balance per account, which is the hot row that ADR-0001 set out to avoid. It also mixes two questions: did something spend this posting, and did the balance change.
  • Bad, because on its own it records no exclusive ownership of specific inputs for a recovery to read.

Option 3: A three-state posting lifecycle with a reservation token

A posting moves Active → PendingInactive → Inactive. reserve_postings(ids, rid) moves it Active → PendingInactive and stamps each one with a ReservationId. That is one atomic conditional update, with … WHERE status = Active.

release_postings moves a posting PendingInactive → Active for the rid that owns it. A finalize moves it PendingInactive (owned by rid) → Inactive. The reservation id is durable, because the write-ahead record persists it (ADR-0003). Every later mutation takes ownership as its condition.

Pros:

  • Good, because a reservation is one atomic conditional update. Two sagas cannot both move one Active posting to PendingInactive. The second one sees zero affected rows. Double-spend safety is then unconditional, and it needs no lock.
  • Good, because the PendingInactive state plus the ReservationId is durable ownership. It survives the multi-step saga and a crash, so a recovery can tell a posting this saga reserved from a posting that another one took.
  • Good, because it expresses cleanly over simple storage, through a count rather than a lock. It also keeps a balance out of the critical section.
  • Good, because compensation is natural. A release reverts the reservation.

Cons:

  • Bad, because a posting carries a lifecycle state and an optional reservation column. That is more than an immutable UTXO holds.
  • Bad, because a crash can orphan a reservation, and then a recovery must resolve it by a roll-forward. A dropped lock would have resolved itself. ADR-0003 handles this.

Decision Outcome

Chosen option: Option 3, the three-state posting lifecycle with a durable ReservationId. It is the only option that gives unconditional double-spend safety with no lock, and durable ownership that a recovery can read across a multi-step saga.

It also expresses itself as the atomic instructions that the simple store offers, and each of those returns a count.

Positive Consequences

  • reserve_postings is the concurrency gate. The saga reads its affected-row count to learn that it took the reservation. See how ADR-0003 reads a count.
  • A recovery tells three cases apart: this saga reserved the posting, this saga already finalized it, or another transfer took it. That is what makes a roll-forward with tracked phases safe (ADR-0003).
  • A balance never enters the critical section, so the UTXO concurrency survives.

Negative Consequences

  • A posting is not a pure immutable UTXO. It carries a status and a reservation.
  • A recovery resolves a reservation that a crash orphaned. No lock release does it.

Links