ACID Transactions
ACID defines the guarantees that make a transaction behave as a reliable unit of work. In backend systems, these guarantees help ensure that related database changes are completed safely even when errors or concurrent operations occur.
Atomicity means a transaction is treated as one unit: either all of its intended database changes commit, or none of them do. If a transfer debits one account and credits another, an error between those operations should not leave only the debit committed.
Consistency means a committed transaction must leave the database satisfying its defined integrity rules. Constraints, validation, and application logic can participate in maintaining that valid state; ACID does not mean the database automatically understands every business rule.
Isolation controls how concurrent transactions interact. Stronger isolation can prevent anomalies such as dirty reads, while weaker isolation can permit more concurrency. The exact behavior depends on the database and isolation level.
Durability means that once a transaction has successfully committed, its changes are intended to survive failures such as a process crash or restart. Databases commonly use mechanisms such as transaction logs and persistent storage to provide this guarantee.
In distributed transactions, ACID becomes harder because related work may span multiple resources or services. A single local database transaction cannot automatically make independent databases commit or roll back together; distributed protocols such as two-phase commit address coordination, while distributed architectures may instead use patterns such as sagas.
Key points
- Atomicity gives all-or-nothing transaction behavior.
- Consistency preserves database integrity rules across commits.
- Isolation determines how concurrent transactions can observe each other.
- Durability makes committed changes survive failures.
- ACID is not the same as distributed transaction coordination; multiple resources require additional mechanisms.
Interview questions
What is the difference between consistency and isolation?
Consistency concerns whether a committed transaction leaves the database satisfying its integrity requirements. Isolation concerns what concurrent transactions are allowed to see while they execute. For example, a database constraint can protect consistency, while an isolation level can determine whether one transaction can observe another transaction's uncommitted data.
What does ACID stand for?
ACID stands for Atomicity, Consistency, Isolation, and Durability. Atomicity means a transaction succeeds completely or is rolled back. Consistency means committed changes preserve the database's integrity rules. Isolation controls interaction between concurrent transactions, and durability means committed changes survive failures.
What happens if the second operation in a transaction fails?
The application should roll back the transaction if the operations are intended to succeed as one unit. The rollback removes the transaction's uncommitted changes. This preserves atomicity by preventing the first successful operation from becoming a partial committed result.
Why are distributed transactions more difficult than local transactions?
A local transaction can usually rely on one transaction manager and one resource to coordinate its changes. With multiple databases or services, there is no single local commit operation that automatically controls every participant. Protocols such as two-phase commit can coordinate participants, but they introduce additional coordination and failure-handling concerns. Service-oriented systems may instead use patterns such as sagas with compensating actions.