{"canonical_url":"https://engineer.company/glossary/two-phase-commit/","content_markdown":"# Two-phase commit\n\n## What it is\n\nTwo-phase commit is a protocol for making a single commit decision across\nseveral transactional participants. Each participant may own a different set of\nrecords and use its own transaction log. A coordinator collects their votes and\nrecords the decision they will all have to follow. The protocol is used when the\noperation's contract calls for committing all of its participating effects\ntogether or rolling them all back.\n\nIn the first phase, the coordinator asks each participant to prepare. A\nparticipant that votes yes records enough durable state to carry out a later\ncommit after a restart. Its local work is prepared and its final outcome is\nstill pending. The coordinator can decide to commit once every participant has\nprepared successfully. A participant can refuse to prepare, which gives the\ncoordinator a reason to decide to abort the operation.\n\nIn the second phase, the coordinator communicates the recorded decision. If a\nreply is lost, recovery has to distinguish uncertainty about delivery from\nuncertainty about what was decided. Once the durable decision is commit, the\nsystem can keep sending that decision or recover it from durable records. A\nparticipant that prepared should follow the shared outcome even after its\noriginal connection has closed. PostgreSQL exposes this prepared state through\nexplicit transaction commands. A transaction manager typically supplies the\ncoordination around them.\n\n## Why it matters\n\nConsider a transfer with entries in two separate ledgers. Each ledger could\ncommit its own update and return success independently. If the connection to the\nsecond ledger fails around that commit, an operator needs to know which entries\nare durable and which request may safely be repeated. A shared outcome changes\nthe recovery task. Both ledgers agree to accept the durable decision before the\ncoordinator reports the transfer as committed.\n\nThe resulting guarantee depends on the participating systems. Every participant\nmust implement prepare and later resolve the same transaction consistently. An\nordinary HTTP callback or payment request often gives no such prepared state to\nthe caller. Pick the operation boundary by listing which effects actually have a\ncompatible durable protocol. If an effect belongs to a system outside the\ntransaction, give it a separate identity and recovery path.\n\nThere is an operating cost in addition to the extra messages. A prepared\ntransaction can hold locks while waiting for the coordinator to resume or for an\noperator to recover the decision. In PostgreSQL, a long-lived prepared\ntransaction can also interfere with cleanup of old data versions. The risk\nbecomes more visible when a participant is slow, a deployment interrupts a\ncoordinator, or a human has to investigate during an outage. An organisation\nthat adopts the protocol needs a way to find each outstanding operation and\nresolve it safely.\n\n## What it looks like in practice\n\nAn illustrative transfer uses three durable records. The coordinator holds a\ntransaction identifier that is shared by both participants. Each participant\nassociates it with its prepared update. The coordinator records the final\ndecision before it tells any participant to finish. A useful test pauses the\nsystem after both sides prepare, after the coordinator records commit, and after\none side receives that decision. Restart the coordinator at each point and make\nthe expected next step explicit.\n\nMeasure the reader's experience as well as the records. During a delay, a\nrequest status might show that the transfer is pending. Once recovery completes,\nthe client can look up the transfer using the original operation identifier.\nProgress logs should connect the coordinator's durable decision and each\nparticipant's result. An alert can identify prepared operations that are older\nthan an agreed threshold and direct an operator to the system that owns their\noutcome.\n\nEngineer's\n[engineering note on limited-stock checkout](/notes/selling-limited-stock-without-overselling/)\ncompares this decision with a checkout spanning stock, order and payment\nservices. The article points out that payment providers in the proposed workflow\ndo not join a shared prepared transaction. It describes a sequence of local\nsteps, including an expiring stock hold and a payment authorisation. That\nexample helps identify a practical boundary before considering the protocol.\nList each participant and ask what durable decision it can actually honour.\n\n## Where it goes wrong\n\nA prepared transaction is easy to mistake for an ordinary connection that timed\nout. An operator may see that the client which issued the prepare command is\ngone and think it is safe to discard the work. The prepared record exists\nindependently of that session. Recover the decision first, identify the\ncorresponding transaction on each system, and use the established policy for an\noperation whose outcome is still unknown. Ad hoc cleanup can break the agreement\nthe protocol was chosen to preserve.\n\nA failed request can also look different from a failed operation. When a server\nconfirms commit but the reply disappears, another attempt with a fresh\nidentifier can create a second update. Treat the operation's identity as part of\nthe durable record, and make it possible to query the current outcome. Test lost\nacknowledgements at the prepare and commit stages separately. The first affects\nwhether the coordinator can safely choose commit. The second can occur after the\ndecision is already fixed.\n\nWatch for long periods when others are blocked behind prepared work. Measure the\nduration from prepare to resolution and rehearse a coordinator restart while\ntraffic is active. Make recovery instructions name the correct source of the\ndecision and the consequences for each participant. A two-phase commit supports\na particular form of agreement. Availability under a disconnected participant,\ngraceful client responses and the cost of holding locks still need explicit\ndesign choices.\n\n## Further reading\n\n- [PostgreSQL two-phase transactions](https://www.postgresql.org/docs/18/two-phase.html).\n  Explains the database commands that participate in external coordination.\n- [PostgreSQL PREPARE TRANSACTION](https://www.postgresql.org/docs/18/sql-prepare-transaction.html).\n  Describes prepared state, durability and operational effects.\n- [The PostgreSQL pg_prepared_xacts view](https://www.postgresql.org/docs/18/view-pg-prepared-xacts.html).\n  Shows how to identify transactions awaiting resolution.\n\n\u003chttps://engineer.company/glossary/two-phase-commit/\u003e\n","date_modified":"2026-11-25T07:18:46Z","description":"Follow prepare, the durable commit decision and recovery when a resource manager is left waiting for the outcome.","formats":{"gemtext":"https://engineer.company/glossary/two-phase-commit/index.gmi","gopher":"https://engineer.company/glossary/two-phase-commit/gophermap","html":"https://engineer.company/glossary/two-phase-commit/","json":"https://engineer.company/glossary/two-phase-commit/index.json","markdown":"https://engineer.company/glossary/two-phase-commit/index.md","pdf":"https://engineer.company/glossary/two-phase-commit/index.pdf"},"kind":"page","language":"en","title":"What is two-phase commit?","url":"https://engineer.company/glossary/two-phase-commit/","version":1}