A Tour of Temporal Typing Liveness & Fairness · 5/5
Liveness & Fairness · Page 5 of 5

Case Study: Two-Phase Commit

Here is a complete distributed protocol in ~90 lines of Python: Lamport's Two-Phase Commit.

A transaction manager (tm_state) coordinates a set of resource managers (rms), each stored in a finite map dict[RM, RMState] updated with Python's dict spread syntax {**s.rm_state, r: "prepared"}:

  • Safety (consistent): no resource manager commits while another aborts.
  • Safety (commit_requires_all_prepared): the TM never commits unless every RM has prepared.
  • Liveness (decision_propagates): once the TM commits, every RM eventually commits.

Because a finished transaction has no more actions enabled, we set check_deadlock=False on the model.

Explore & Break It: First click Check to verify all 38 states pass. Then try weakening tm_commit's guard by removing and s.tm_prepared == s.rms — click Check to see the checker find a premature commit! (Or comment out rm_rcv_commit's fairness to break decision_propagates.)
Click Check (or press Shift+Enter) to run the front-end and check this specification. Switch to TLA⁺ to view its TLA⁺ meaning.
---- MODULE TwoPhaseCommit ----
CONSTANT rms
VARIABLES rm_state, tm_state, tm_prepared

Consistent ==
    ~(\E r1, r2 \in rms : rm_state[r1] = "committed" /\ rm_state[r2] = "aborted")

CommitRequiresAllPrepared ==
    tm_state = "committed" => \A r \in rms : rm_state[r] \in {"prepared", "committed"}

DecisionPropagates ==
    (tm_state = "committed") ~> (\A r \in rms : rm_state[r] = "committed")
====