← Back to microservices patterns map
🔒
Microservices Pattern

Two-Phase Commit (2PC)

A coordinator asks participants to prepare, then commits or rolls back all participants.

consistency

Detailed Description

2PC provides atomicity but fights the autonomy and availability goals of microservices.

Most microservice systems prefer sagas, outbox, idempotency, and compensation instead.

Visual Diagram

2PC Protocol
  Phase 1 — PREPARE:
    Coordinator → "prepare?" → DB-1, DB-2, DB-3
    DB-1 → "ready ✓"
    DB-2 → "ready ✓"
    DB-3 → "ready ✓"

  Phase 2 — COMMIT:
    Coordinator → "commit!" → all DBs
    all commit atomically ✓

  If DB-2 says "not ready":
    Coordinator → "rollback!" → all DBs

Tradeoffs

Pros

True atomicity across resources

Cons

Blocking, coordinator SPOF, poor scalability

Examples: XA transactions; rarely preferred in microservices