Saltar al contenido ForoHackers

Two-phase commit: all the commitment issues, none of the closure

ConsensoBases de datosSistemas distribuidos
#1035 · two_phase_commitment · · Denunciar

Hi, DBA here, and yes, the handle is a cry for help. Let me explain why two-phase commit (2PC) has a reputation.

The goal: a transaction touches two databases, and both must commit or neither. A coordinator drives it.

Phase 1 (prepare)   Coordinator -> A, B : "can you commit?"
                    A, B write PREPARED to their logs, keep locks, answer YES/NO
Phase 2 (commit)    If all YES: Coordinator logs COMMIT, tells A and B to commit
                    If any NO or timeout: Coordinator tells everyone to abort

The problem is blocking. Once a participant has voted YES, it cannot decide alone: it is in doubt. If the coordinator crashes after collecting the votes and before telling anyone, A and B must hold their locks and wait, maybe for a long time. No safe unilateral choice exists, because the coordinator may already have told the other one to commit.

Consequences in real life:

  • Locks held by in-doubt transactions stall everything that touches those rows. In PostgreSQL you will see them in pg_prepared_xacts, and an abandoned prepared transaction also holds back cleanup.
  • 2PC across services over a slow network multiplies latency: two round trips plus forced log writes at every participant.

Alternatives, each with trade-offs: sagas (a sequence of local transactions with compensating actions, no atomicity but eventual consistency you design for), or replicating the coordinator's decision with a consensus protocol so it does not die alone ("Paxos commit"). Pick on purpose.

It is not you. It is the failure model. Commitment issues are inherent to the protocol.

#1514 · race_condition_rita · · Denunciar

Failure model first, as always: 2PC is fine if the coordinator and participants fail by crashing and recovering with their logs intact, and it is only blocking (not unsafe). The unsafe versions are the ones that skip the forced log writes. Good post.

#1515 · mutex_mike · · Denunciar

One extra picture of the in-doubt window:

Coord:  --collect votes--[crash]..................recover--decide
A:      --prepared (locks held) ---------------------------> ?
B:      --prepared (locks held) ---------------------------> ?

Everything between [crash] and recover is wasted time with locks held. Three-phase commit tries to remove it, but it assumes bounded delays, which networks do not honour.

#1516 · legacy_cobol_whisperer · · Denunciar

On the mainframe side, two-phase commit has been in production for decades between the transaction monitor and the database. It works because both live in the same machine room under the same operations team, and the in-doubt cases are resolved by a human with a manual. The protocol is fine. The distance is the problem.

#1517 · null_terminated_hopes · · Denunciar

"In doubt" is also an accurate description of my emotional state. At least you gave it a protocol.

Inicia sesión o únete para responder.