Study guides · Intermediate

Reason about failures in distributed transactions

Use reservation and payment histories to compare 2PC preparation and blocking with saga intermediate states, compensation and unknown outcomes.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. Identify the cross-resource commitment problem
  2. Trace a lost response between local commits
  3. Separate preparation from the global decision
  4. Explain the coordinator-failure wait
  5. Contrast compensation after local commits
  6. Choose from the actual business boundary
  7. Prepare evidence needed by recovery
  8. Keep a committed decision separate from its client response
  9. Solve unknown outcome versus final rejection
  10. Ask Logeia about this articleCheck your understanding

Identify the cross-resource commitment problem

Store inventory reservation in DB1 and a modeled payment in DB2. Committing DB1 before DB2 fails leaves only the reservation. Each store can provide local ACID transactions without making the sequence globally atomic. Read local transactions, partial failure and unknown results first. This guide compares coordinated commitment and later compensation using one reservation/payment history.

Specify the invariant before selecting a mechanism. Must both outcomes commit together, or may a temporary reservation later resolve to payment or cancellation? User-visible accepted, processing, complete and cancellation-pending states differ. An error response does not prove no reservation remains. Choose the allowed intermediate states and recovery owner rather than selecting a protocol merely because two databases exist. This separates a business requirement from the implementation components that might satisfy it.

Trace a lost response between local commits

DB1 commits reservation, DB2 receives payment and then its response disappears. A caller cannot infer from timeout whether DB2 did nothing or committed. Immediately cancelling DB1 can leave payment without reservation; resending a fresh payment identity can duplicate the effect. Adding a generic error handler does not remove the unknown-result boundary.

Separate two questions: aligning resource commitment and recognizing retries of the same business request. A distributed transaction does not necessarily remove a lost client response, so request identity can remain necessary. Operation identity, transaction identity and message identity may serve different roles. A recovery query also needs a contract describing which committed state it observes. No external payment service is invoked here; the example isolates the coordination problem without claiming to implement a real provider's idempotency or settlement behavior.

Separate preparation from the global decision

In two-phase commit, a coordinator asks every resource to prepare. A yes participant durably records sufficient prepared state to obey a later decision. After all prepare successfully, the coordinator records commit and disseminates it; a failed preparation can lead to abort. Hearing yes is not identical to every business update already being committed. Trace prepared state, durable decision and participant completion separately.

Prepared resources may retain locks. Waiting without knowing the coordinator's decision therefore has costs beyond an ordinary short local transaction. A participant cannot always abort merely because its timeout expired: commit may already be recorded and delivered elsewhere. Durable records, restart recovery and decision retransmission are part of the protocol, not optional operational decoration. The atomicity objective constrains what a participant may decide independently when communication is unavailable.

StageDB1DB2Coordinator
PrepareYes, preparedYes, preparedCollect participant votes
Record decisionMay still await decisionMay still await decisionPersist commit
Deliver and finishCommitCommitTrack completion

Explain the coordinator-failure wait

After both participants vote yes, a disconnected DB1 may not distinguish coordinator failure before commit recording from commit already recorded and delivered only to DB2. Unilaterally aborting in the latter history violates a shared decision. Basic two-phase commit can therefore block on an unknown outcome. Replicating the coordinator can improve availability under additional consensus and implementation rules, which must be examined separately.

PostgreSQL provides prepared-transaction mechanisms, but invoking preparation commands alone does not implement the external coordinator. Limits, monitoring, resolution of old prepared work and recovery ownership need explicit design. Operators must recover the actual decision rather than guess commit or abort from a transaction name. This guide presents principles and worked histories; it does not create prepared transactions or change 2PC configuration in the production database.

Contrast compensation after local commits

A saga commits reservation and payment locally, then applies explicit compensation when required. If payment is finally rejected after reservation, cancellation is another committed operation. Unlike holding both resources prepared, an intermediate reservation may be visible. Compensation is not time reversal: observations, notifications and external actions may remain after the corrective operation.

Unknown payment outcome is not a final rejection. Resolve it through the selected provider's stable-identity query or retry contract. Compensation can fail too, requiring durable retry, waiting or manual resolution states. Orchestration and event-driven coordination differ in control structure, but both need reconstructible progress. A saga is not simply another implementation of the same all-resource atomicity guarantee. It supports a business process whose permitted intermediate states and corrective actions are explicitly designed.

Choose from the actual business boundary

First consider whether required updates can fit in one database, reducing the distributed-commit problem itself. If multiple resources must commit together, verify that every participant supports the required prepare and recovery contract. A remote provider that does not participate cannot be placed in 2PC by the caller's preference alone. If the business permits intermediate states, compare saga or expiring reservation approaches with failure-time user experience.

Outbox preserves local update-to-send intent; inbox bounds a receiving local effect under replay. They can support a saga without turning it into one atomic commit across resources. Draw each mechanism's actual commit boundary. An arrow to another resource reintroduces a response-loss and retry question. Pattern composition must preserve those distinctions rather than treating several useful mechanisms as an automatic proof of global atomicity.

Prepare evidence needed by recovery

For coordinated commit, retain prepared participant state, coordinator decision and delivery progress. For a saga, retain step identity, outcome and compensation state. Neither is resolved solely by sorting log timestamps; use durable state and protocol rules. Verify that a restarted owner continues the same decision and a revived old owner cannot issue contradictory actions. Recovery should not depend only on an operator guessing from error strings.

Stop before and after prepare, decision, effect, response and compensation boundaries, then identify permitted next actions. A deterministic model and real process/database fault injection provide different evidence and must be reported separately. Disposable modeled resources can fix histories without contacting real inventory or payment services. Adoption still needs comparison with the selected products' documented guarantees and recovery procedures. The test plan should name the exact state each stop leaves behind.

Keep a committed decision separate from its client response

Even after all resources share a commit decision, the client response can disappear. A retry still needs to retrieve that transaction or business operation's existing result rather than create another operation. Record coordinated-commit safety tests separately from API idempotency tests after response loss. They answer different questions: whether resources agree, and whether a caller can safely recover knowledge of the outcome. A successful decision protocol does not automatically supply the latter API contract. The distinction also determines which identity must survive snapshots, retention and restarted ownership, so recovery does not forget a committed operation merely because its original connection ended.

Solve unknown outcome versus final rejection

Reservation committed but payment response was lost. Can reservation be immediately cancelled? The business and provider contract determine recovery, but timeout alone cannot establish that payment did not commit. Resolve unknown outcome and follow the required correction. Collapsing final rejection and unknown result loses information needed to choose the next operation.

A yes-voting 2PC participant unable to read the decision cannot safely choose abort if commit might already have reached another participant. Avoiding waiting is not sufficient justification for breaking the shared decision. Accept the cost or redesign the promised guarantee explicitly. The learning outcome separates coordinated commit, compensation, retry identity and user-visible state instead of expecting one mechanism to answer all four questions.

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Understand the systems behind the web · STEP 17

Choose a learning path