Databases 10 min read

Understanding Database Transactions, ACID Properties, and Two‑Phase/Three‑Phase Commit Protocols

This article explains the ACID properties of database transactions, distinguishes between single‑node and distributed transactions, describes the two‑phase and three‑phase commit protocols, discusses transaction states, fault tolerance, and recovery mechanisms, and shows how replication and consensus can provide high availability for transaction processing.

IT Architects Alliance
IT Architects Alliance
IT Architects Alliance
Understanding Database Transactions, ACID Properties, and Two‑Phase/Three‑Phase Commit Protocols

Data sharing and concurrent programming are common needs in computer applications; the transaction mechanism of databases helps backend developers write correct concurrent programs by relieving mental burden, thanks to the ACID properties (Atomicity, Consistency, Isolation, Durability).

While single‑node transactions involve elements on one machine, distributed transactions span multiple machines (e.g., transferring money between accounts stored on different MySQL instances). A distributed transaction is a global transaction composed of several local sub‑transactions, each preserving ACID, but guaranteeing ACID for the global transaction is non‑trivial.

Two‑Phase Commit (2PC) is a protocol that coordinates the commit of a distributed transaction using a coordinator and multiple participants. In the prepare phase , the coordinator logs the intent and sends a prepare T message; participants check if they can commit, log, and reply with ready T or abort T . In the commit phase , if all participants are ready, the coordinator logs and sends commit T , after which participants log and apply the changes.

The protocol’s outcome can be visualized as a voting process where each participant holds a veto power. If all votes are affirmative, the transaction commits; otherwise it aborts. After the final state is determined, further actions such as roll‑forward (for committed) or rollback (for aborted) are required, and locks must be released.

Fault tolerance is achieved by handling network partitions (retry, idempotence, timeout) and crashes of coordinators or participants (high availability via primary‑backup, Paxos or Raft groups). Recovery procedures involve scanning logs: depending on which logs exist (e.g., only a C record, or both C and A records), the system can decide to terminate, roll forward, or roll back the transaction.

When a transaction is in an in‑doubt state due to insufficient information, recovery may require consulting other participants or waiting for the coordinator to become available. The possible states are outstanding, committed, aborted, or complete, and the correct action depends on the logs observed.

Three‑Phase Commit (3PC) extends 2PC by adding a backup coordinator and replicating commit information to at least k coordinators, allowing the system to tolerate multiple coordinator failures.

Combining Replicated State Machines (RSM) with 2PC provides high availability: the coordinator’s log is replicated via a Paxos group, enabling automatic failover and leader election.

Finally, separating the transaction processing layer from the storage layer (e.g., Percolator over Bigtable) allows a stateless transaction service that leverages reliable distributed storage for high availability.

distributed systemsDatabaseTransactions2PCACIDRecovery3PC
IT Architects Alliance
Written by

IT Architects Alliance

Discussion and exchange on system, internet, large‑scale distributed, high‑availability, and high‑performance architectures, as well as big data, machine learning, AI, and architecture adjustments with internet technologies. Includes real‑world large‑scale architecture case studies. Open to architects who have ideas and enjoy sharing.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.