Sub topic 6
Durability: Committed Data Survives Failure
Durability is the ACID property that makes a committed transaction survive a crash. After the database reports success for COMMIT, the committed changes should still exist after a process failure, operating-system restart, or power loss.
This chapter assumes you know basic SQL, tables, constraints, and how concurrent users or processes can access a database.
What a commit promises
A transaction has two broad outcomes:
COMMITmakes its changes permanent from the database’s point of view.ROLLBACKabandons its uncommitted changes.
Durability does not mean that data is indestructible. Disk hardware can fail, backups can be missing, and an entire data center can be lost. It means the database has completed the storage work required by its configured durability guarantee before confirming the commit.
For example:
CREATE TABLE payments (
id integer PRIMARY KEY,
amount numeric(10, 2) NOT NULL
);
BEGIN;
INSERT INTO payments (id, amount)
VALUES (1, 25.00);
COMMIT;
-- After a normal database restart:
SELECT * FROM payments WHERE id = 1;
-- The row should still be present.Write-ahead logging
Most relational databases use a write-ahead log (WAL). A WAL is a sequential record of changes made by transactions. “Write-ahead” means the log record must reach durable storage before the corresponding changed data page is written there.
The database can therefore recover after a crash:
- It reads the log during startup.
- It finds transactions whose commits were recorded.
- It reapplies committed changes that had not reached the data files.
- It discards or reverses incomplete transactions.
A checkpoint later copies changed pages from memory to the main data files. Checkpoints improve recovery time, but they do not replace the WAL. The WAL protects changes made after the last checkpoint.
Commit acknowledgment and storage settings
Durability depends on configuration and hardware. A database may acknowledge a commit after writing the WAL to the operating-system cache, or only after requesting that storage flush it to stable media. Faster settings can lose the most recent acknowledged transactions during a power failure.
| Choice | Benefit | Risk |
|---|---|---|
| Flush the log before acknowledging commit | Stronger crash protection | Higher commit latency |
| Delay or avoid synchronous flushes | Lower latency | Recent commits may be lost |
Replication adds another decision. A primary database may confirm a commit after its own WAL is durable, or it may wait until one or more replicas have received and stored the log. Synchronous replication can reduce data loss after primary failure, but it increases latency and depends on replica availability.
Practical design rules
- Keep backups separate from the live database.
- Test restoring backups, rather than only checking that backup jobs report success.
- Choose synchronous durability when losing acknowledged writes is unacceptable.
- Monitor disk space, WAL growth, replication lag, and recovery failures.
- Keep transactions short so their logs and locks do not grow unnecessarily.
Exercise
Why can a database recover a committed change that was not yet written to its main data file?
Show answer
The write-ahead log records the change first. During startup, recovery replays the log records for committed transactions.