TASK
Implementation
Understanding the trade-offs between 2PC and 3PC helps choose the right protocol for your use case.
Message complexity:
2PC (happy path):
Prepare → 2N messages (N requests, N replies)
Commit → 2N messages (N requests, N replies)
Total: 4N messages
3PC (happy path):
CanCommit → 2N messages
PreCommit → 2N messages
DoCommit → 2N messages
Total: 6N messagesBlocking scenarios:
2PC blocks when:
- Coordinator crashes after collecting all Yes votes
- Participant crashes after voting Yes but before receiving decision
3PC blocks when:
- Coordinator crashes before sending PreCommit
- Network partition separates coordinator from participants before PreCommit
- (Does NOT block if coordinator crashes after PreCommit)Real-world usage:
- 2PC: Widely used (XA transactions, databases, message queues)
- 3PC: Rarely used due to complexity and remaining blocking scenarios
- Consensus-based: Paxos/Raft are preferred for non-blocking commit
Performance comparison:
Request: {"type": "benchmark", "msg_id": 1, "protocols": ["2pc", "3pc"], "participants": 5, "transactions": 100}
Response: {"type": "benchmark_ok", "in_reply_to": 1, "results": {"2pc": {"avg_latency_ms": 45, "throughput_tps": 2200}, "3pc": {"avg_latency_ms": 68, "throughput_tps": 1450}}}When to use each:
- Use 2PC: Simple, widely supported, acceptable blocking risk
- Use 3PC: Need slightly better availability, can tolerate extra complexity
- Use consensus: Need true non-blocking commit, can tolerate higher latency
Sample Test Cases
Benchmark 2PC vs 3PC latencyTimeout: 30000ms
Input
{"src":"c0","dest":"benchmarker","body":{"type":"init","msg_id":1,"participants":["p1","p2","p3"]}}
{"src":"c1","dest":"benchmarker","body":{"type":"benchmark","msg_id":2,"protocols":["2pc","3pc"],"participants":3,"transactions":100}}
Expected Output
{"src": "benchmarker", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
Compare blocking scenariosTimeout: 5000ms
Input
{"src":"c0","dest":"comparator","body":{"type":"init","msg_id":1}}
{"src":"c1","dest":"comparator","body":{"type":"compare_blocking","msg_id":2,"protocols":["2pc","3pc"]}}
Expected Output
{"src": "comparator", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
Hints
Hint 1▾
2PC: 2 rounds (Prepare + Commit/Abort), 3PC: 3 rounds (CanCommit + PreCommit + DoCommit)
Hint 2▾
2PC blocks if coordinator crashes after Prepare, 3PC blocks if coordinator crashes before PreCommit
Hint 3▾
3PC reduces but doesn't eliminate blocking
Hint 4▾
3PC is rarely used in practice due to complexity and remaining blocking scenarios
Hint 5▾
Most systems use 2PC or consensus-based approaches (Paxos/Raft)
OVERVIEW
Theoretical Hub
Concept overview coming soon
Key Concepts
protocol comparisonmessage complexityblocking scenariosreal-world usageperformance trade-offs
main.py
python
1
2
3
4
5
6
7
8
9
10
11
12
13
#!/usr/bin/env python3
import sys
import json
def main():
# Your implementation here
for line in sys.stdin:
msg = json.loads(line)
print(json.dumps(msg), flush=True)
if __name__ == "__main__":
main()