TxTransactions Lab
GitHub
IV · Implement distributed decisions

Lose the coordinator.

Both participants vote yes. One learns commit. The other only sees a timeout. Make a safe decision without pretending the timeout is evidence of abort.

You will: trace prepare and commit records, explain two-phase commit blocking, and recover a known decision. Prerequisites: durable decisions and recovery. Runtime: a teaching model. Its assumptions appear before the scenario controls.

Two-phase commit (2PC) first collects durable participant promises, then records and distributes one final decision. Prepared means the participant can honor that later decision after restart; it has not yet learned a final outcome.

Concept diagram · before the experiment

Prepared is a durable promise, not a final decision

In phase one, each participant persists PREPARED before voting yes and retains required resources. In phase two, the coordinator durably records COMMIT before telling participants. If one participant receives COMMIT and another times out, the prepared participant must wait for authoritative resolution.
Read the diagram: Trace the durable writes before the messages that rely on them. A YES vote promises the participant can obey the eventual decision after a restart. A timeout says that communication failed; it does not prove that the global decision was abort. Open full diagram

Predict before running

If A has committed while B is prepared, may B abort just because the coordinator is unreachable? What can B actually infer from the timeout?

Control the experiment

The page loads Baseline with two participants and a coordinator that becomes unreachable. Select Step through both PREPARED records, the durable COMMIT and A’s receipt of the decision. Compare durableDecision with what B has learned.

Two participants with durable prepare/decision records. The coordinator commits and reaches A, then becomes unreachable from B. Stable storage survives; Byzantine faults are excluded.

Edit the experiment

Change the JSON settings and select Step or Run to end. Restore scenario reloads the selected preset.

Select Step to inspect the initial state.

    The diagram separates the coordinator’s durable global decision from the decision each participant has actually received. A prepared B remains labelled as holding resources while its decision-delivery edge is pending.

    Inspect the evidence

    Step through durable prepare, the coordinator’s durable COMMIT, A’s notification and the lost connection to B. Prepared means B has promised it can commit later and must retain recoverable state; it is not a commit visible to the application.

    Change one thing

    Select Run to end in Baseline. Compare A, B and the agreement check. Choose Repair and run again. Do the participants disagree, or is B still prepared with resources held? Compare progress. Choose recovered and run again. Which saved decision allows B to finish? Explain the difference between preserving agreement and finishing the work.

    Compare two histories behind a timeout

    An unreachable coordinator could have crashed before or after recording the decision. B’s timeout cannot distinguish those histories.

    Check the prepared promise

    After a durable YES vote, unilateral timeout abort is unsafe without another protocol rule that establishes the global outcome.

    Why this result occurs

    Two-phase commit first obtains durable promises, then communicates one durable decision. The waiting repair preserves atomic agreement while blocking B. Recovery of the existing COMMIT lets B finish. Durable participant state, decision retention, coordinator recovery and operational handling of prepared work belong in the design. Atomic commit alone does not specify all isolation between transactions.

    Under the hood: promises must reach stable storage first

    PREPARE is a local recovery boundary. A participant flushes enough state to finish later before sending YES; its prepared record and held resources survive the uncertainty window. The coordinator flushes one decision before notifying participants. A participant can then install that decision, record completion and release resources according to its local protocol.

    The trace exposes each participant's stable log, coordinator records, messages and resource retention. Coordinator restart reads its durable COMMIT; it does not invent a decision from elapsed time. The panel is a global observer's view: B cannot directly inspect an unreachable coordinator's log merely because you can see it in the model.

    Read the engine code executed by this lab

    These functions come directly from the running engine. Shared helpers and every mechanism are in the complete downloadable module.

    function twoPhaseCommit(config) {
      choose(config,'resolution',['timeout-abort','wait','recover']);
      const s={A:'active',B:'active',durableDecision:null,coordinator:'reachable',progress:'running',stableLogs:{coordinator:[],A:[],B:[]},messages:[],heldResources:{A:true,B:true}};
      const t=recorder(s,s=>[check('participants never decide commit and abort',!([s.A,s.B].includes('committed')&&[s.A,s.B].includes('aborted')))]);
      for(const p of ['A','B']){
        s.stableLogs[p].push({type:'PREPARED',tx:'T1'});s[p]='prepared';s.messages.push({from:p,to:'coordinator',vote:'YES'});
        t.emit(`${p} flushes PREPARED before sending its YES vote`);
      }
      s.stableLogs.coordinator.push({type:'COMMIT',tx:'T1'});s.durableDecision='commit';t.emit('Coordinator flushes the COMMIT decision before notifying participants');
      s.messages.push({from:'coordinator',to:'A',decision:'COMMIT'});s.stableLogs.A.push({type:'COMMIT',tx:'T1'});s.A='committed';s.heldResources.A=false;
      t.emit('A records COMMIT, installs its effect and releases held resources');
      s.coordinator='unreachable';s.progress='blocked';t.emit('Lose the coordinator before B receives the decision');
      if(config.resolution==='timeout-abort'){
        s.stableLogs.B.push({type:'ABORT',tx:'T1'});s.B='aborted';s.heldResources.B=false;s.progress='unsafe completion';t.emit('B guesses ABORT from a timeout despite its durable PREPARED promise');
      }else if(config.resolution==='recover'){
        // Reconstruct from the durable coordinator record; do not decide from the timeout.
        const record=s.stableLogs.coordinator.find(r=>r.tx==='T1');s.durableDecision=record.type.toLowerCase();s.coordinator='recovered';
        s.messages.push({from:'coordinator',to:'B',decision:record.type});s.stableLogs.B.push(copy(record));s.B=record.type==='COMMIT'?'committed':'aborted';s.heldResources.B=false;s.progress='complete';t.emit('Restart coordinator, look up its durable decision and resolve B');
      }else t.emit('B keeps its PREPARED record and resources until a decision can be learned');
      return t.finish();
    }

    A real system also needs transaction identities, participant lists, duplicate-message handling and a policy for forgetting resolved state. The replicated-log engine shows how a recorded decision can survive a leader failure. It does not make an unprepared participant's YES vote appear.

    Transfer the idea

    Change the history so B votes NO before the decision. Specify the coordinator’s outcome and what A must retain while waiting. Then describe what happens if the coordinator loses its durable storage rather than merely restarting.

    Success criteria: cite PREPARED and the durable coordinator decision; show that waiting preserves agreement while B retains resources; distinguish blocked completion from an unsafe timeout abort and explain authoritative resolution.

    Primary reference: PostgreSQL PREPARE TRANSACTION.

    Your design record

    Explain what happened and cite the run or SQL result that supports it.

    Write your design notes

    Download notes and evidence

    Downloads include current editor text. Run evidence stays on this page until you leave.