Northwind: Transactions and workflows: atomic decisions or explicit intermediate states
Outcome. After this lesson, you can choose prepared state or a compensating workflow.
Hypothetical scenario. At Northwind, Inventory changes succeed while the payment response is uncertain.
In short. Name compensation and reconciliation states explicitly. Distinguish prepared transactions from compensatable steps.
A reservation and a payment disagree
Hypothetical scenario. A marketplace reserves an item and then asks a separate payment service to charge the buyer. The payment response is lost. Releasing the item immediately can conflict with a payment that actually succeeded; charging again can repeat the effect. The workflow needs an explicit uncertain state.
A local transaction gives a commit boundary within a particular transactional resource. A business workflow can cross several resources, each with its own authority and failure behavior. Distributed atomic commit and a compensating workflow offer different contracts for that boundary.
The consensus lesson examined agreement about an ordered decision. Here the decision is whether several participants commit changes together, or whether the business permits intermediate committed steps that may later require additional corrective actions. Start from the invariant rather than choosing a fashionable workflow pattern.
Sequential success is not atomic success
Calling two services in order does not create one transaction. After the first succeeds, the second can fail, time out or become unavailable. The first effect may already be visible to other operations. A retry or a rollback function in the caller cannot automatically restore the world as though nothing happened.
Even a local transaction should cover the actual business invariant. If inventory and reservation records can safely share one authoritative store, that may be simpler than dividing them into services and reconstructing their atomicity over a network.
Identify irreversible effects early. An email cannot be made unseen, a shipment cannot always be recalled and a refund is a new financial action with its own timing and failure modes. Describing every corrective step as rollback hides the consequences users can already observe.
Prepare a decision or record a workflow
In two-phase commit, participating resources first prepare to follow a final commit or abort decision. A prepared participant cannot independently assume abort merely because the coordinator is temporarily unreachable. The decision and recovery protocol determine what happens next.
A saga-style workflow instead commits local steps and records the next action or compensation. It must define intermediate states, concurrent interactions, retries, deadlines and unresolved cases. It does not provide the same isolation as one transaction across all steps.
PostgreSQL 17's PREPARE TRANSACTION reference describes prepared transactions intended for an external transaction manager and warns about leaving them unresolved. The two-phase transaction documentation describes their durable state. A database exposing this mechanism does not mean an arbitrary external provider participates in it.
Choose prepared state or a compensating workflow
Northwind wants to reserve inventory and charge a card, but the two state changes belong to different authorities. A local Postgres transaction can protect related rows inside one database boundary. Two-phase commit asks participating resource managers to prepare before a final commit decision. A saga instead accepts intermediate business states and records steps that require completion, compensation or reconciliation.
Prepared is not equivalent to committed. A prepared participant retains the state needed to honor the eventual decision, and unresolved prepared transactions need an external manager and recovery procedure. PostgreSQL's documentation warns that this machinery is intended for such managers. Exposing a prepared state without a durable decision path can leave resources tied up after the initiating process disappears.
In a hypothetical saga, one hundred orders each reserve two items before requesting payment. If ten payment outcomes remain unknown, 10 × 2 = 20 items are associated with unresolved outcomes. That is not evidence of ten declined payments. A policy that releases all twenty items immediately could conflict with later confirmed payments. Define expiry, reconciliation and any corrective action against the original order identity.
Compensation is a new business action, not a reversal of time. A refund can have its own authorization, costs and uncertain result. If its response disappears, repeat reconciliation using the same corrective-operation identity rather than inventing another refund. Start by asking whether the invariant can remain inside one database transaction. Move it across services only when the ownership and workload benefits justify the additional intermediate states and operational burden.
Calculate exposure and reconciliation work
Hypothetical scenario. Choose 100 purchase workflows. Sixty complete both reservation and payment. Twenty fail payment with a definite rejection and require reservation release. Twenty have an uncertain payment outcome and remain pending. The accounting is 60 + 20 + 20 = 100; pending must not be counted as either confirmed success or definite failure.
If each workflow reserves one item, the uncertain group holds 20 items until reconciliation or a declared expiry transition. Releasing them after a chosen five-minute timer is a business rule that must account for payments confirmed after that timer. A timer does not establish that a provider action never occurred.
Suppose reconciliation can inspect ten pending operations per minute while five new uncertain operations arrive per minute. With a backlog of 20, the net drain is 10 − 5 = 5 operations per minute, giving 20 ÷ 5 = 4 minutes under constant rates and successful inspections. Inspections that remain inconclusive do not count as resolved operations.
For a prepared-resource example, choose 30 prepared transactions each holding two distinct row locks. That is 30 × 2 = 60 held locks under the chosen model. Their impact depends on which rows other transactions need, not just the total. One frequently requested row can cause more disruption than many unused rows.
These numbers expose operational obligations rather than asserting a universal throughput or timeout. A workflow diagram without pending counts, recovery capacity and unresolved-state ownership omits a substantial part of the design.
In the wild: Choose prepared state or a compensating workflow
The primary reference establishes this concrete distinction: prepared PostgreSQL transactions require an external manager and unresolved-state handling.
Compensation preserves a different invariant
The business may accept a temporary reservation followed by release if payment fails. It may not accept a shipment before payment authority is established. Define the allowed states and which transitions make irreversible commitments. Sometimes reordering steps or using a provider-supported authorization stage reduces exposure.
Compensations can fail and require retries with their own operation identities. They may also race with a late success. Record which decision the workflow has made, which effects are confirmed, and how late evidence is interpreted. Do not allow two independent recovery workers to issue incompatible corrective actions.
Atomic commit can be appropriate when all participants support the chosen protocol and the application requires its contract. It also introduces prepared-state recovery obligations. Replicating the coordinator can protect decision state, but cannot make an unreachable participant receive a decision immediately.
The refund succeeds but the status write fails
Hypothetical scenario. A workflow decides to refund a payment after a reservation expires. The provider refunds it, but the local process crashes before recording completion. A recovery worker issues another refund because the local state still says pending.
This is the idempotency problem inside compensation. Use the provider's supported identity and reconciliation capabilities, persist the intended corrective action, and distinguish requested, confirmed and unresolved states. Compensation is not exempt from partial failure.
An operational queue should identify unresolved cases and the authorized human decision path where automation cannot establish the outcome. Record the evidence and preserve the original logical operation identity. A manual action without that context can repeat the same mistake as an automated retry.
Three claims to reject in a design review
“A saga is a distributed transaction with automatic rollback.” It permits intermediate commits and uses business-specific corrective actions. Visibility and isolation differ.
“Two-phase commit is the same as consensus.” Atomic commit asks participants to follow one transaction outcome; consensus solves agreement under its specified model. A system can use consensus within its commit machinery without making the concepts identical.
“Expiry resolves uncertainty.” It establishes a policy deadline. Evidence about an external action can still arrive afterward and require reconciliation.
Keep the invariant as local as practical
Prefer a clear local transaction where the ownership and operational requirements allow it. When a workflow genuinely crosses authorities, choose between supported atomic commit and explicit intermediate states from the business requirement, not from a blanket rule about microservices.
Document every externally visible state, the permitted transitions, the owner of recovery and the evidence needed to finish it. Include conflicting user actions such as cancellation while payment is pending. A happy-path sequence alone does not specify the workflow.
The final architecture cases combine these decisions. For each, identify where atomicity is necessary, where asynchronous derived state is acceptable and which failures require an explicit pending or unavailable outcome.
Limits of this model
Prepared transactions require compatible participants and a durable transaction manager. Sagas require meaningful intermediate states and corrective actions. A compensation can fail or become uncertain itself; it cannot promise that users or external systems never observed the earlier action.
Check yourself: decide what remains unresolved
A payment call timed out. Can the reservation be released as though payment definitely failed?
Not from the timeout alone. Apply the declared expiry and reconciliation policy, accounting for a late confirmed payment. Preserve a pending state where the outcome remains unknown.
The compensation call succeeded remotely but no local completion record exists. What follows?
Reconcile using the same corrective operation identity and supported provider evidence. Blindly repeating a refund can duplicate the corrective effect.
Inventory and reservation invariants fit one database transaction. Must they become a saga to scale the organization?
No. Service boundaries should not casually weaken the required invariant. Evaluate ownership and workload evidence against the additional intermediate states and recovery burden.
Northwind decision log
Name compensation and reconciliation states explicitly.
Distinguish prepared transactions from compensatable steps.
Expose unresolved payment outcomes to reconciliation.
Go deeper: Choose prepared state or a compensating workflow
- Video lecture: Two-phase commit — Martin Kleppmann. Understand prepared state and why a lost coordinator can leave a decision unresolved.
- Book chapter: Microservices Patterns, chapter 4: Managing transactions with sagas — Chris Richardson. Read Managing transactions with sagas while considering how to choose prepared state or a compensating workflow.
- Book chapter: Reactive Design Patterns, chapter 8: Delimited Consistency — Roland Kuhn with Brian Hanafee and Jamie Allen. Read Delimited Consistency while considering how to choose prepared state or a compensating workflow.
- Primary source: PostgreSQL 17: PREPARE TRANSACTION — PostgreSQL Global Development Group. Expose unresolved payment outcomes to reconciliation.
Related blog: Retry identity for uncertain business steps.