ARCHIVED from builddistributedsystem.com on 2026-04-28 — URL: https://builddistributedsystem.com/tracks/coordinator/tasks/task-9-1-two-phase-commit
TASK

Implementation

Implement 2PC: Phase 1 sends PREPARE, collects votes. Phase 2 sends COMMIT if all YES, else ABORT.

Sample Test Cases

All participants vote yesTimeout: 5000ms
Input
{
  "src": "c0",
  "dest": "n1",
  "body": {
    "type": "init",
    "msg_id": 1,
    "node_id": "n1",
    "node_ids": [
      "n1"
    ]
  }
}
Expected Output
{"src":"n1","dest":"c0","body":{"type":"init_ok","in_reply_to":1,"msg_id":0}}

Hints

Hint 1
Phase 1: Prepare - ask all participants
Hint 2
Phase 2: Commit/Abort based on votes
Hint 3
Log decisions for recovery
OVERVIEW

Theoretical Hub

Two-Phase Commit

2PC ensures all-or-nothing across nodes. The blocking problem: if coordinator crashes after PREPARE, participants are stuck.

Key Concepts

2PCatomic commitprepare-commit
main.py
python
Implement Two-Phase Commit - The Coordinator | Build Distributed Systems