QA
Senior LevelSoftware Engineering Principles

Compare Two-Phase Commit (2PC) protocol versus Sagas for distributed transaction management.

❓ Interview Prompt: Compare Two-Phase Commit (2PC - Prepare/Commit, atomic, blocking, coordinator SPOF) vs Saga Pattern (Eventual consistency, compensating actions, non-blocking).

Comparing distributed transaction coordination protocols:

| Feature | Two-Phase Commit (2PC) | Saga Pattern |

|---|---|---|

| Consistency | Strong ACID consistency across databases | Eventual Consistency (BASE) |

| Mechanism | Phase 1: Prepare (Lock resources); Phase 2: Commit/Abort | Sequence of local transactions with compensating rollbacks |

| Performance | Slower; holds database locks across network | High performance; non-blocking local transactions |

| Failure Risk | Coordinator node is Single Point of Failure (SPOF); locks hang | Requires writing complex compensating rollback logic |

Code Implementation Example

typescript
// 2PC Coordinator Phase 1 (Prepare)
const participants = [db1, db2];
const votes = await Promise.all(participants.map(p => p.prepare(txId)));
const allOk = votes.every(v => v === 'VOTE_COMMIT');

// 2PC Coordinator Phase 2 (Commit or Abort)
await Promise.all(participants.map(p => allOk ? p.commit(txId) : p.abort(txId)));
💡Executing Two-Phase Commit (2PC) Prepare and Commit phases.
🎯 Interview Key Takeaway

Use 2PC for micro-second strong consistency across SQL databases, and Sagas for long-running microservice workflows.

#2PC#Saga Pattern#Distributed Transactions#Senior
Back to all Software Engineering Principles Questions→

More Software Engineering Principles Interview Questions

Beginner LevelSoftware Engineering Principles

Explain the 5 SOLID Principles of Object-Oriented Software Design with practical refactoring examples.

Beginner LevelSoftware Engineering Principles

What are DRY (Don't Repeat Yourself), KISS (Keep It Simple, Stupid), and YAGNI (You Aren't Gonna Need It)?

Beginner LevelSoftware Engineering Principles

Why is Composition Over Inheritance a fundamental object-oriented design principle?