Hypothetical scenario. Northwind needs an inspectable record of approved money movement. Dev starts with a mutable balance and payment-status field. Priya asks how a correction remains visible and which records establish local intent versus a provider’s external effect.
Maya: “Show why each accepted movement exists.”
Dev: “And show the separate condition that stops two different requests from spending the same available amount.”
Build a simplified ledger authority with balanced posting sets, journal identity and a coordinated spending invariant. The case uses one selected currency and sign convention; it is not a complete accounting policy. Keep provider evidence distinct from local order completion.
Which authority establishes each money-movement fact?
The local application owns its order and intended payment operation. The provider owns the evidence about the external payment it processes. A local “failed” value cannot override a provider-confirmed effect, and a provider event does not automatically establish that the correct local order was fulfilled.
Separate requested, authorized, captured, rejected and unresolved states according to the selected provider's actual contract. Authorization and capture may be distinct operations; do not infer one from the other. A timeout is a transport observation, not a final payment state.
Likewise, a refund is another external action with its own identity and outcome. Calling it rollback can conceal that the original charge occurred and that the corrective action can fail or remain pending.
Keep intent, effects and postings connected
Create a stable payment-operation identity bound to order, actor, amount and currency. Persist intent before invoking the provider, then reuse the provider-supported idempotency identity for attempts of that operation. Reject parameter-changing reuse. Persist confirmed evidence and the corresponding local transition under a suitable transaction.
Use provider events as inputs to a state machine, with authenticated origin, deduplication and object identity checks. A delayed event must not undo a later confirmed state. Where evidence is incomplete, retrieve the provider's documented current status or retain an unresolved case for authorized review.
Stripe's idempotency documentation illustrates why provider details matter: behavior differs across its API versions, and retention and parameter rules constrain reuse. This case does not assume that every provider or version offers the same guarantee.
Northwind chooses Postgres for one local ledger authority. A journal identity names the movement, and its posting rows name the affected accounts and currency. The transaction accepts the complete posting set and its operation identity together. A uniqueness constraint rejects replayed journal identities; the posting procedure verifies the sum before commit. An ordinary per-row CHECK constraint cannot assert a sum across arbitrary rows.
Where spending is bounded by an available balance, the write must coordinate that limit too. Lock the relevant balance rows in a consistent order or use an isolation and retry strategy whose behavior has been tested. Two individually balanced journals can otherwise consume the same available funds. The balance invariant and the spending limit answer different questions, and the service needs both.
Toy concurrent-transfer example. Suppose an account has 100 units available and two workers each propose a 70-unit transfer. Each alone appears affordable because 100 − 70 = 30. Together they require 70 + 70 = 140, exceeding availability by 40. Under the chosen no-overdraft contract, only one can be accepted; after its commit, the other evaluates against 30 and is rejected. Both journals could have summed to zero, so balanced postings alone would not have prevented this conflict.
Preserve a compensating journal rather than editing a completed movement. Keep provider action identity separate from the journal identity, with an explicit link between them. A refund’s payment-provider outcome and its local accounting entry are separate evidence boundaries. Recovery must identify an existing provider effect before deciding whether a missing local journal is safe to create.
Show the money movement as records and conditions
Hypothetical scenario. A payment intention K names tenant, amount, currency and beneficiary. A local transaction claims K, checks available funds and records the reservation or postings required by the selected business step. The external provider operation has its own stable identity tied back to K.
| Record | Fields | Protected fact |
|---|---|---|
| Intention K | scope, fingerprint, amount, currency | Same business meaning on retry |
| Reservation | account, amount, state | Spending capacity held |
| Journal / postings | business ref, accounts, signed amounts | Declared posting invariant |
| Provider operation | K-linked identity, evidence | Remote effect reconciliation |
A journal groups postings under one business reference. Balanced postings are necessary for the toy signed-ledger model but do not prove the right account was charged, the spending limit held or the provider acted once. Those are separate invariants with separate enforcement boundaries. Keep amounts in the selected exact currency units and define rounding at any conversion boundary.
Funds reservation is a conditional authority decision. Two independently balanced transfers can both read the same old balance and overspend unless the transaction serializes that condition. Audit reconstruction needs immutable posting history and explicit correcting entries; silently editing an old balance erases the evidence needed to explain it. Settlement and available balance should not be collapsed into one status field.
Derive balanced records and recovery work
Hypothetical scenario. Use a simplified signed-posting convention for one currency: a transfer of 100 units records −100 against one modeled account and +100 against another. Their sum is −100 +100 = 0. The signs define this toy model; they are not a statement about a particular organization's debit/credit chart.
A partial record containing only+100 has sum 100 and violates the chosen balance invariant. Persist both postings and the transaction identity atomically. A row-level amount check alone cannot establish a cross-row sum; PostgreSQL's constraint reference explicitly limits what ordinary check constraints can enforce across rows.
A corrective transfer of 30 units records+30 and−30, again summing to 0. After the original transfer and correction, the first modeled account has−100 +30 = −70 and the second has 100 −30 = 70. Preserving the original and corrective records explains the history more clearly than overwriting 100 with 70.
In the wild: immutable movements and mutable balances
Square’s Books report describes immutable journal and book-entry records, corrections through new entries, and a mutable current balance. Its implementation uses Spanner. The transferable issue is the relationship between movement history and current state, not a requirement that Northwind select the same database.
Balanced journals overspend the same account
Hypothetical failure scenario. Two independently balanced transfers both read the same available balance and pass an application-side check. Their posting sets each sum to zero, but their combined spending exceeds the no-overdraft limit. The algebra is correct while the allocation decision is not coordinated.
Make the spending check and accepted balance transition part of the same authoritative decision as the complete journal. Use an appropriate lock, conditional transition or isolation strategy and define whole-operation retries. A unique journal identity prevents repeating one accepted movement; it does not resolve competition between different legitimate movements.
Keep local completion precise
Report local ledger completion only after the chosen transaction commits its legal posting set and identity. Report provider status only from supported external evidence. Fulfillment consumes a separate durable obligation and must recognize repeated delivery. These facts can progress at different times without being compressed into one ambiguous paid flag.
Preserve the original and corrective journal identities and their relationship. A correction is a new authorized action, not permission to overwrite the record that explains the earlier movement. Validate actor, account, amount and currency alongside the balance sum. A balanced transfer to the wrong account remains wrong.
The next page introduces lost provider replies, delayed webhooks and restoration. The baseline local invariant remains in force while external evidence is incomplete. Recovery must reconstruct the accepted operation rather than infer that a missing local status means no money moved.
Challenge the proposed mechanism
A transfer balances to zero but names the wrong beneficiary. Which test should reject it?
Reveal the design reasoning
Check the intention fingerprint, approved beneficiary and authorization, not merely the posting sum. Also test concurrent spending and provider retry identity. Arithmetic balance is only one part of correctness.
Decision and reconsideration: Protect available funds transactionally and preserve audit history. Reconcile provider evidence under the original payment intention.
Defend the journal
Every journal sums to zero. Can two concurrent transfers still overspend?
Coordinate the available limit
Yes. Distinct balanced journals can compete for the same funds. The no-overdraft decision needs an enforcing concurrency boundary in addition to balanced postings.
Can an ordinary row-level CHECK establish the sum of an arbitrary posting set?
Locate cross-row enforcement
No. The chosen posting procedure and transaction must enforce the complete set under the relevant concurrency model. Individual row validation is not a cross-row sum proof.
A correction changes a hundred-unit movement to seventy. Should the original entry be edited?
Preserve explanatory history
Under this model, add the thirty-unit corrective movement with its own identity. The original and correction explain the resulting net seventy-unit position.
The baseline records a movement under a selected authority. Page 48 introduces uncertain provider outcomes and settlement evidence, requiring reconciliation rather than a new guess about what happened.
Northwind decision log
Accept complete balanced journals under one local authority.
Coordinate spending limits and preserve original movement history.
Next, reconcile provider evidence without repeating external effects.
Go deeper: concurrency and durable history
- Book chapter: Concurrency Control and Recovery in Database Systems, chapter 1: The Problem — Philip A. Bernstein, Vassos Hadzilacos and Nathan Goodman. Identify atomicity and recovery obligations.
- Book chapter: Concurrency Control and Recovery in Database Systems, chapter 2: Serializability Theory — the same authors. Analyze competing transitions. This is a historical conceptual reference, not current provider guidance.
- Video lecture: F2023 #19: Database Logging — CMU Database Group. Separate durable records from acknowledged application effects.
- Primary document: Books, an immutable double-entry accounting database service — Łukasz Strzałkowski. Inspect the distinction between immutable movement and current balance.