ACID
A transaction is a sequence of database operations that are treated as a single unit.
ACID (Atomicity, Consistency, Isolation, Durability) is a set of properties that ensure data integrity and consistency in database transactions.
- Atomicity: A transaction is either executed completely or not at all.
- Consistency: The database must remain in a consistent state before and after a transaction.
- Isolation: Transactions must be isolated from each other to prevent interference.
- Durability: The results of a committed transaction must be persistent and recoverable in the event of a system failure.
How is ACID achieved?
Atomicity: The database maintains a transaction log that records all operations performed within a transaction. If a transaction fails, the database can use the log to roll back changes, restoring the database to its previous consistent state. Periodic checkpoints mark points of consistency; if a failure occurs, the database can recover to the last checkpoint and replay the logged transactions from that point.
Consistency: The database enforces data integrity constraints, such as primary keys, foreign keys, and data types, ensuring that data remains consistent and valid throughout transactions.
Isolation: The database employs locking mechanisms to control concurrent access to data. Some databases also use timestamping to order transactions and prevent conflicts, ensuring that the operations of one transaction do not interfere with another.
Durability: The database implements a technique called Write-Ahead Logging (WAL). It writes log records before making changes to the database, ensuring that even in the event of a system crash, changes can be recovered from the log.