Search DistillSys

Find a concept

Type at least two characters to search lessons, designs, papers, and interview prep.

Transactionsadvanced7 min read

Distributed Transactions & 2PC

Coordinate one atomic outcome across independent participants—and understand the blocking cost.

2–5 minute refresher
Mental model

Prepare everyone → record one decision → commit or abort everyone

30second
refresher
Two-phase commit coordinates one atomic outcome across participants. In prepare, each participant durably promises it can commit. The coordinator then records and broadcasts commit or abort. The protocol protects atomicity but can block when the decision is unavailable.

What problem does 2PC solve?

Some invariants span resources: debit one account and credit another, or reserve inventory and record payment. Without coordination, a partial success can expose an impossible business state.

Decision guide

Key trade-offs

ChoiceWhat you gainWhat it costs
2PCAtomic outcome across transactional participantsBlocking, locks, and coordinator dependency
SagaIndependent local commits and long-running workflowsVisible intermediate states and compensations
Single-owner redesignSimpler correctness and failure recoveryLess service or data independence
What happens if?

Coordinator fails after every participant prepares

Participants cannot safely infer commit or abort. They remain in-doubt until the coordinator recovers or a replicated transaction manager exposes the recorded decision.
What happens if?

Commit succeeds but the client times out

The client must retry with the same transaction or idempotency identifier. Treating the retry as new work can double-apply the business operation.

When to avoid it

Do not stretch synchronous 2PC across unreliable, independently operated services by default. If the business can tolerate intermediate state and compensating actions, a saga often aligns better with service ownership.

Senior interview modeWhy is classic two-phase commit called blocking?Show answer
Once a participant votes yes, it cannot unilaterally abort or commit. If the coordinator’s decision is unreachable, the participant may hold prepared state and locks until that decision becomes available.
#transactions#two-phase-commit#atomicity#saga