Transaction
In short
A transaction is a group of database operations that succeed or fail as a single unit, so the data is never left in a half-finished, inconsistent state.
What is a database transaction?
A transaction bundles several database operations together so they are treated as one all-or-nothing action. If every step succeeds, the changes are saved with a COMMIT; if anything goes wrong, a ROLLBACK undoes all of them, as if the transaction never happened.
The classic example is a bank transfer: subtracting money from one account and adding it to another must both happen, or neither. Without a transaction, a crash between the two steps could make money disappear. With a transaction, the database guarantees that the two updates are applied together.
Reliable transactions follow the ACID properties. Atomicity means all or nothing; consistency means the data always moves from one valid state to another; isolation means concurrent transactions don't see each other's unfinished work; and durability means committed changes survive crashes and power loss. Databases offer different isolation levels, such as Read Committed and Serializable, which trade strictness for performance.
A database transaction is different from a business transaction like a purchase, although one is often used to record the other. Transactions also get harder in distributed systems such as microservices, where one operation spans several databases; there, teams often use patterns like sagas, a series of local transactions with compensating steps, instead of a single ACID transaction.
At a glance
Key takeaways
- A transaction groups operations into one all-or-nothing unit.
COMMITsaves the changes;ROLLBACKundoes them.- ACID stands for atomicity, consistency, isolation, and durability.
- Isolation levels control how concurrent transactions affect each other.
Example
-- Transfer $100 from account 1 to account 2
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
-- Save both changes together
COMMIT;
-- If something had failed, you would run ROLLBACK instead,
-- and neither update would be appliedReaders ask
What does ACID mean in databases?
ACID stands for atomicity, consistency, isolation, and durability, the four guarantees that keep database transactions reliable even when errors, crashes, or many simultaneous users occur.
What is the difference between COMMIT and ROLLBACK?
COMMIT permanently saves all the changes made in the current transaction, while ROLLBACK discards them and restores the data to how it was before the transaction started.
Do NoSQL databases support transactions?
Many do. MongoDB, for example, supports multi-document ACID transactions, but guarantees and performance vary by database, so check the documentation for your specific system.
See also
- DatabaseDatabases, p. 6A database is an organized collection of data stored on a computer, managed by software that lets applications save, search, and update it efficiently.
- SQLDatabases, p. 40SQL is the standard language for working with relational databases, used to create tables and to insert, query, update, and delete the data stored in them.
- NoSQLDatabases, p. 29NoSQL is a family of databases that store data in models other than relational tables, such as documents, key-value pairs, wide columns, or graphs.
- Database IndexDatabases, p. 7A database index is a data structure that helps a database find rows quickly without scanning a whole table, much like the index at the back of a book.
- MicroservicesSoftware Architecture, p. 27Microservices are an architectural style where an application is split into small, independently deployable services that communicate over a network.
- OLTPDatabases, p. 31OLTP (online transaction processing) describes databases built for many small, fast reads and writes from everyday operations, such as placing orders.
- Saga PatternSoftware Architecture, p. 35The saga pattern runs a transaction spanning several services as a sequence of local steps, undoing completed steps with compensating actions if one fails.
Sources
Spotted a mistake or something missing on this page?Suggest an edit