Course contentsShow
← Databases & SQL
Lesson 1525 of 1,95314. High Availability and DurabilityFree lesson

Write-Ahead Logging Mechanics

Explain how database engines ensure durability by sequencing changes to persistent storage before data modification.

In this lesson, you will learn how Write-Ahead Logging (WAL) ensures database durability by enforcing a strict sequence of operations before data pages are ever modified on disk.

The Principle of "Log Before Data"

To achieve the "D" in ACID (Durability), a database cannot simply overwrite data files every time a transaction commits. Random disk I/O is notoriously slow, and a crash during a partial page write could lead to catastrophic corruption. Instead, database engines utilize a Write-Ahead Log (WAL).

The fundamental rule is simple: no data page can be written to persistent storage until the corresponding log record describing the change has been flushed to non-volatile memory.

How the Sequence Works

When a transaction performs an update, the database engine follows this precise order of operations:

  1. Prepare Log Record: The engine generates an entry representing the change (e.g., "Transaction 101 updated Row A from X to Y").
  2. Append to Log Buffer: The log record is appended to an in-memory buffer.
  3. Flush to Disk: Before the transaction returns a "Success" to the user, the database forces this log buffer to persistent storage (an fsync operation). Because logs are sequential writes, this is significantly faster than performing random I/O on the actual data files.
  4. Update Data Buffer: The in-memory data page is modified.
  5. Checkpoint: Later, the database asynchronously flushes the modified data pages to the main database file.

A Conceptual Flow

If the database crashes after Step 3 but before Step 5, no data is lost. Upon restart, the recovery manager reads the WAL and "replays" any committed transactions that were not yet present in the main data files.

-- Conceptual logic of a WAL entry
-- Even if the table update hasn't reached the disk file:
INSERT INTO wal_log (lsn, tx_id, op_type, data_diff) 
VALUES (4502, 101, 'UPDATE', 'Row_A_New_Value');

-- The COMMIT is only acknowledged AFTER this record hits persistent storage.
COMMIT; 

By decoupling the high-speed sequential log write from the expensive random data page write, the engine guarantees that the state of the database is always reconstructible, even if the system loses power mid-operation.

Key Takeaway: Write-Ahead Logging ensures durability by requiring that all modifications are recorded in a sequential, persistent log before the primary data files are updated, allowing the engine to recover from crashes by replaying the log.