We use analytics cookies.We use cookies to analyze site traffic and improve your experience.
Loading...
System Design Masterclass · Data Replication Distribution
Free preview: the first 2 parts of this 9-part lesson.
Already bought the course? Sign in to read the whole lesson.
this lesson is part of
System Design Masterclass
Unlock this lesson and the 751 other paid lessons
$25 once, by PayPal
See the System Design courseread the rest of this lesson in
System Design Masterclass
Unlock this lesson and the 751 other paid lessons
$25 once, by PayPal
See the System Design coursein Data Replication Distribution
This lesson is part of the full System Design Masterclass. Unlock it along with 769 other lessons for a one-time payment.
One-time payment by card or PayPal
Get Lifetime AccessOne-time payment: UPI, card or netbanking
Get Lifetime Access18 Foundation lessons are free, no payment needed. Try them now
It is easy and good
“It is really nice and good Please join the course”
Your database is updating a row. It is halfway through modifying a B-tree index page and the data page when the server's power supply dies. The machine goes dark instantly. No graceful shutdown, no flushing buffers, just an immediate halt.
When the server reboots, the database's files are in an inconsistent state. The index page was modified but the data page was not. Or maybe both were partially modified. Reading them would return garbage. Your B-tree index now points to a row that does not exist, or worse, to a row that belongs to a different table.
The Write-Ahead Log (WAL) prevents this disaster. Before modifying any data page, Postgres first writes a description of the change to the WAL, a sequential, append-only log on disk. Only after the WAL record is safely on disk does Postgres modify the actual data pages. This principle was formalized by C. Mohan at IBM Research in the ARIES paper (1992), and every modern database implements some variant of it.
If the server crashes, Postgres replays the WAL on startup. Changes written to the WAL but not yet applied to data pages get replayed. Changes not in the WAL were never committed, so they are discarded. The database is always recoverable to a consistent state. Always.
This same WAL is what powers Postgres . The primary streams its WAL to replicas, and replicas replay the same records against their own data files. Crash recovery and replication are literally the same mechanism. That is a clean design, and it is the reason Postgres replication is so reliable.
The WAL is a sequence of records, each describing a low-level modification to a database page. It operates at the physical level. Not "UPDATE users SET name = 'Alice'" but "write these 42 bytes at offset 2048 in file base/16384/16389."
WAL record structure:
WAL records are written to segment files, each 16MB by default (configurable at initdb time). Files are named with their LSN range: 000000010000000000000001, 000000010000000000000002, etc. The pg_wal directory on a busy Postgres instance might have hundreds of these files.
The WAL guarantee: A transaction is considered committed when its COMMIT record is flushed to the WAL on disk via fsync. At that point, even if the server loses power one nanosecond later, the transaction's changes can be recovered by replaying the WAL.
How WAL interacts with data pages:
This "write the log first, data later" pattern is why it is called Write-AHEAD Log. The log is always ahead of the data.