Menu
Medium #system-design·September 9, 2026

Database Write-Ahead Log (WAL) for Data Durability

This article delves into the Write-Ahead Log (WAL), a fundamental mechanism databases use to ensure data durability and atomicity. It explains how WAL guarantees that committed transactions are preserved even during system crashes, outlining its role in transaction processing and recovery, which is critical for robust system design.

Read original on Medium #system-design

Understanding Write-Ahead Logging (WAL)

The Write-Ahead Log (WAL) is a core component in most relational and NoSQL databases, crucial for maintaining ACID properties, specifically Atomicity and Durability. It ensures that any changes intended for the database are first recorded in a persistent log before being applied to the actual data files. This sequential logging strategy is key to preventing data loss and enabling reliable recovery mechanisms in the event of system failures.

How WAL Ensures Durability and Atomicity

  • Atomicity: If a transaction fails mid-way, the database can use the WAL to undo (rollback) any changes that were partially applied, restoring the database to its state before the transaction began.
  • Durability: Once a transaction's changes are written to the WAL and flushed to persistent storage, the transaction is considered committed. Even if the database crashes before the actual data files are updated, the WAL contains all necessary information to redo these changes upon recovery.
💡

WAL's Performance Implications

While WAL enhances data integrity, it also impacts performance. Writing sequentially to a log file is generally faster than random writes to data pages. Databases often buffer WAL entries and flush them in batches, balancing durability guarantees with write throughput. Understanding this trade-off is vital for designing high-performance, durable systems.

WAL in Database Recovery

During database startup after a crash, the recovery process typically involves two main phases using the WAL:

  1. Redo Phase: The database scans the WAL for committed transactions that might not have been fully applied to data files. It re-applies these changes to ensure all committed data is present.
  2. Undo Phase: For transactions that were in progress but not committed at the time of the crash, the database uses the WAL to roll back any partial changes, ensuring atomicity.
sql
-- Example of a transaction and its WAL implications
BEGIN TRANSACTION;
  UPDATE accounts SET balance = balance - 100 WHERE id = 'userA';
  INSERT INTO transaction_log (user_id, amount, type) VALUES ('userA', -100, 'withdrawal');
COMMIT;
-- Each operation generates a WAL record. The COMMIT flushes these records to disk, guaranteeing durability.
WALWrite-Ahead LogDatabase InternalsDurabilityAtomicityACIDData RecoveryTransaction Management

Comments

Loading comments...