Imagine transferring $100 from Account A to Account B.
Two operations must happen:
Subtract $100 from A Add $100 to B
Enter fullscreen mode
Exit fullscreen mode
What happens if the first succeeds but the second fails?
Money disappears.
This is where database transactions become important.
What Is a Transaction?
A transaction groups multiple database operations into a logical unit.
Conceptually:
BEGIN
Subtract money from A Add money to B
COMMIT
Enter fullscreen mode
Exit fullscreen mode
If something goes wrong:
ROLLBACK
Enter fullscreen mode
Exit fullscreen mode
The database attempts to return to the previous consistent state.
ACID
Transactions are commonly discussed using ACID:
Atomicity
All operations succeed, or none do.
Consistency
The database moves from one valid state to another.
Isolation
Concurrent transactions shouldn't incorrectly interfere with one another.
Durability
Committed changes should survive failures according to the database's guarantees.
Why Transactions…