Skip to the content.

πŸ”Ή What is Write-Ahead Logging (WAL)?

Write-Ahead Log (WAL) is a technique used in databases to ensure durability and atomicity (part of ACID properties).

This ensures that even if the system crashes, the database can recover to a consistent state.


πŸ”Ή How WAL Works (Step by Step)

  1. Client issues a write (e.g., insert/update).
  2. Database writes the change to the WAL (append-only log).
  3. Database applies the change to the actual database file (data pages).
  4. On crash/restart:

    • The database reads the WAL.
    • Re-applies any committed changes that were not persisted to the main database.

πŸ”Ή Example

Suppose you insert a new order:

Step 1: Append to WAL
[WAL] INSERT INTO orders VALUES (123, 'Laptop', 1)

Step 2: Update database file
[DB FILE] orders table updated

Crash happens here? Recovery reads WAL:
Re-applies INSERT statement -> ensures order is not lost

πŸ”Ή Key Benefits

  1. Durability (part of ACID) βœ…

    • Once WAL is written, the transaction is considered durable even if DB crashes before writing to main storage.
  2. Crash Recovery πŸ”„

    • WAL allows the database to replay committed transactions after a crash.
  3. Sequential Writes πŸ“„

    • Writing to a log is sequential, which is faster than random writes to data files.
  4. Atomicity

    • Ensures partial transactions don’t corrupt databaseβ€”only fully logged transactions are replayed.

πŸ”Ή Usage in Databases


πŸ”Ή Summary

Write-Ahead Log = log of all changes written before updating the database itself.


WAL vs Redo/Undo logs β€” the way professional DB engines implement them.

WAL (Write-Ahead Logging) is a strategy / principle.

Redo & Undo logs are the actual log files used by many databases to implement WAL.

So:

WAL = the rule Redo/Undo logs = the files used to follow the rule

But depending on the database, WAL may contain only redo info or both.


πŸš€ 1. WAL (Write-Ahead Logging) β€” The Principle

WAL means:

Before modifying the actual data file, write the change to a log on disk.

Purpose:

WAL is append-only, very fast, and persisted immediately.


πŸš€ 2. Redo Log β€” β€œWhat should be reapplied after crash”

Redo log stores how to reapply committed changes.

Contains:

Used in roll-forward recovery:

πŸ’‘ If DB crashes:

Redo = reapply the work that was finished.


πŸš€ 3. Undo Log β€” β€œHow to reverse uncommitted changes”

Undo log stores how to undo/rollback partial work.

Contains:

Used in two situations:

Case 1: Transaction does ROLLBACK

Undo log restores old values.

Case 2: DB crashes, but transaction was not committed

Undo log is used during restart to undo incomplete operations.

Undo = cancel the work that wasn’t finished.


⭐ So what’s the difference?

Feature WAL Redo Log Undo Log
Meaning Principle: write log before data Log of new values Log of old values
Purpose Ensure durability Recover committed txns Rollback uncommitted txns
Used for Crash recovery, durability Redo (roll forward) Undo (roll back)
Contains Depends on DB New changes Old changes
Required for crash recovery? Yes Yes (for committed) Yes (for uncommitted)

⭐ How different DB engines use WAL/Redo/Undo

PostgreSQL

MySQL InnoDB

Oracle & SQL Server

MongoDB / Cassandra


⭐ Final Summary (Best Version)

βœ” WAL = The rule

β€œWrite changes to log before writing to main data file.”

βœ” Redo Log = New data for crash recovery

(Used to reapply committed changes after crash)

βœ” Undo Log = Old data for rollback

(Used to undo failed or uncommitted transactions)

βœ” Many DBs combine these but the idea is the same:


Whether WAL (Write-Ahead Logging) needs to be enabled manually or is on by default depends on the database:


1. PostgreSQL


2. SQLite

PRAGMA journal_mode=WAL;

3. MySQL (InnoDB)

innodb_log_file_size
innodb_flush_log_at_trx_commit

4. MongoDB


πŸ”Ή Summary

Database WAL / Journal Enabled by Default? Notes
PostgreSQL WAL βœ… Yes Fully integrated
SQLite WAL mode ❌ No Must enable manually
MySQL (InnoDB) Redo log βœ… Yes Configurable
MongoDB Journal (WAL) βœ… Yes Configurable

πŸ’‘ Rule of Thumb: