Transacciones Distribuidas
Commit en dos fases · 2PC · XA
Escrito por Darjus Kazakevic · revisado el
Prepare, then commit, and everyone waits if the coordinator dies.
Two-phase commit (2PC) makes one transaction span two or more stores, such as two databases. A coordinator runs it. In phase one it asks every participant to prepare: write the changes durably, keep the locks, and vote yes or no. In phase two it tells everyone to commit if all voted yes, or to abort if any voted no.
The weak point is the gap between the phases. A participant that voted yes cannot decide alone, so it holds its locks until the coordinator answers. If the coordinator crashes, those locks stay until it recovers. Every commit also waits twice for the slowest participant. That is why most microservice systems use a Saga instead.
En esencia — A coordinator collects a yes from every participant, logs one commit decision, then tells them all. A participant that voted yes must wait for that decision.
Cuándo recurrir a él
Recurre a él cuando
- One commit must span two stores, with no moment where only one changed
- A few nearby participants that all support prepare, such as Postgres
- Inside one distributed database, where the engine runs it for you
Reconsidéralo cuando
- Availability matters more than atomicity: use a Saga and compensate
- A database write plus a message: an outbox needs no coordinator
- Participants are third-party APIs that cannot prepare and wait
Conceptos clave
- Coordinator (Transaction manager)
- The process that runs the protocol and makes the single decision: commit or abort. It writes the decision to its own durable log before telling anyone, so it can finish the work after a restart.
- Prepare and commit
- Prepare: the participant writes its changes and a prepare record to disk, keeps its locks, and promises it can commit. Commit: it makes the changes visible and releases the locks. A yes vote cannot be taken back.
- Blocking on coordinator failure (In doubt)
- A participant that voted yes cannot commit, because another may have voted no. It cannot abort, because the coordinator may have committed. It waits, holding locks, and every transaction that needs those rows waits too.
- XA
- The X/Open standard interface for 2PC between a transaction manager and the resources it coordinates. Java's JTA uses it. Postgres, MySQL (InnoDB) and Oracle support it, and so do some message brokers such as ActiveMQ.
- Where it runs today
- Mostly inside one distributed database: Spanner, CockroachDB and YugabyteDB run 2PC between their own shards. Kafka transactions use a similar commit across partitions. Across separate products it is rare.
Reglas de diseño
- Price the blocking before you choose it
- A participant that voted yes holds its locks until the coordinator decides, and a dead coordinator stops them all. If that stall is not acceptable, use a Saga with compensating steps.
- Log the decision before you send it
- The coordinator's durable commit-or-abort record is the transaction. Write it before telling any participant, so a restarted coordinator finishes the work instead of guessing.
- Watch for in-doubt transactions
- Alert on prepared transactions older than a minute. In Postgres, the pg_prepared_xacts view lists them. An orphan keeps its locks and blocks VACUUM until someone commits or rolls it back.
- Keep participants few and close
- Every commit waits twice for the slowest participant. Two or three databases in one region is workable; ten services across regions is not.
Errores habituales
- Orphaned prepared transactions. A coordinator bug leaves one prepared for days; it holds row locks, blocks VACUUM, and the table bloats.
- Sending the decision before logging it. The coordinator crashes, restarts with no record, and aborts a transaction that some participants already committed.
- Using 2PC between microservices over HTTP. One slow service now holds locks in every other service.
- Treating a timeout as abort. A participant that voted yes must not abort on its own; it must ask the coordinator.