0002-saga-commit-pipeline.md 6.1 KB

Saga commit pipeline instead of a single/distributed transaction

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

Context and Problem Statement

A commit of a transfer is not one write. It must consume the input postings and create the output postings. It must persist the transfer record and index that record under every involved account. It must emit the events. All of this must stay consistent, and it must recover if the process crashes part way through.

Kuatia is an embeddable library, so it must not need an external transaction coordinator. It must also compose a multi-transfer workflow with no global lock, and an FX trade and a multi-leg settlement are two such workflows. So how do we make a commit both consistent and crash-safe, without one transaction that covers everything?

Decision Drivers

  • Crash-safety: a crash must never leave created value with no recorded transfer, and must never leave a recorded transfer with no value. Recovery must converge.
  • Composability: several transfers must combine into one logical workflow, and a rollback must cover that whole workflow.
  • No external coordinator: embeddable, no XA transaction manager or separate service to operate.
  • Avoid a cross-shard or cross-resource transaction: the model must not depend on one database transaction that covers everything. A later UTXO-style sharding then stays open.
  • Keep storage simple: push the domain decisions out of the storage layer (see ADR-0003). A monolithic transaction on the store side works against that.

Considered Options

Option 1: One database transaction (monolithic commit)

Do everything inside one BEGIN … COMMIT: the deactivate, the insert, the record, the index and the events.

Pros:

  • Good, because it gives strict atomicity on one database.
  • Good, because recovery is trivial. The database rolls back a partial commit.

Cons:

  • Bad, because it does not compose across several transfers or resources. A multi-leg workflow cannot be one database transaction unless it holds locks across all of it.
  • Bad, because it spans neither a shard nor another resource. It pins the design to one transactional store.
  • Bad, because it pulls the domain logic into the storage layer, and that covers the guards, the ownership and the index decisions. It is the opposite of the simple-storage goal (ADR-0003).

Option 2: Two-phase commit / XA across resources

Coordinate a distributed transaction over the resources involved.

Pros:

  • Good, because it offers cross-resource atomicity.

Cons:

  • Bad, because it needs a coordinator and resources that support XA. That is heavy to operate, and it conflicts with the goal of an embeddable library with no external service.
  • Bad, because 2PC blocks. A coordinator failure can leave the resources locked.
  • Bad, because it is overkill for a library meant to drop into an application.

Option 3: Saga with per-step compensation (the legend crate)

Model the commit as a pipeline of small steps, reserve → finalize, and give each step a compensating action. legend drives the pipeline, retries a step by itself, and compensates in last-in-first-out order. Crash-safety comes from a write-ahead record plus idempotent recovery, rather than from a global transaction.

Pros:

  • Good, because the steps compose. A higher-level multi-transfer saga combines the same primitives, and its compensation covers the whole workflow.
  • Good, because it needs no global transaction, no distributed transaction and no coordinator. That suits an embeddable library.
  • Good, because a reserve of the inputs gives unconditional double-spend safety and locks no balance. It moves a posting Active → PendingInactive and stamps it with a ReservationId, as one atomic conditional update per posting.
  • Good, because it keeps storage simple. Each step issues a simple instruction, and the saga owns the decisions (ADR-0003).

Cons:

  • Bad, because a commit passes through brief intermediate states rather than one atomic change. A posting is reserved, or finalized in part, in those states.
  • Bad, because idempotency and the check of the end state become the saga's responsibility and not the database's.
  • Bad, because some guarantees stay best-effort without an atomic guard at commit time. The CappedOverdraft floor is one of them, and it cuts across the model. See ADR-0003.

Decision Outcome

Chosen option: Option 3, a saga commit pipeline, which is reserve → finalize through legend. It is the only option that composes a multi-transfer workflow, needs no external coordinator and keeps storage simple.

It still gives crash-safety, through a write-ahead record plus idempotent recovery. It still gives unconditional double-spend safety, through the reservation protocol.

Positive Consequences

  • The same steps drive both a single commit and multi-transfer workflows, with automatic retry and LIFO compensation on logical failure.
  • Reservation makes a double-spend of a consumed posting impossible, and it needs no global lock. So the UTXO concurrency model of ADR-0001 survives.
  • Storage stays a thin record keeper. The correctness of the commit lives in one place that a test can reach.

Negative Consequences

  • Crash-safety is not a rollback by the database. It is a write-ahead record plus a roll-forward recovery, and the saga must implement that idempotently. See ADR-0003.
  • Within a commit there are observable intermediate posting states.
  • The exact recovery model and the best-effort guards are not simple, and ADR-0003 covers them.

Links

  • Refined by ADR-0003. That record gives the storage contract, says how to read a count, and defines the durable recovery with tracked phases that the saga depends on.
  • Builds on ADR-0001, which gives the posting lifecycle that the reserve step uses.
  • Background: architecture.md, transfers.md.