Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

Recovery System: Log-Based & WAL
DBMS

Recovery System: Log-Based & WAL

Understand how databases recover from crashes using Logs, Checkpoints, and Write-Ahead Logging.

Recovery System: Log-Based & WAL

What happens if the power goes out while a transaction is 50% done? The Recovery System ensures that the database returns to a consistent state.

The foundation of modern database recovery is Write-Ahead Logging (WAL) — a simple rule that guarantees atomicity and durability.


Learning Objectives

After completing this chapter, you will be able to:

  • Classify different types of database failures.
  • Explain Write-Ahead Logging (WAL) and why it’s critical.
  • Describe log record structure and Log Sequence Numbers (LSN).
  • Explain Undo and Redo operations.
  • Understand checkpoints and why they speed recovery.
  • Differentiate between log-based recovery and Shadow Paging.
  • Answer recovery interview questions.

Types of Failures

Failure TypeDescriptionEffect
Transaction FailureLogical error, constraint violation, deadlockOnly the affected transaction needs rollback
System CrashPower failure, OS crash, hardware faultRAM lost, disk intact. All active transactions affected
Disk FailurePhysical disk corruption, head crashRequires restoration from backup + replay

Write-Ahead Logging (WAL)

WAL is the most important rule in database recovery. Every modern database uses it.

The WAL Rule

A transaction cannot commit until all its log records are written to stable storage (disk).

In other words: Log first, then commit.

Why WAL Matters

  1. If the system crashes, the DBMS reads the log on disk to see what happened
  2. Transactions that committed before the crash → Redo (re-apply changes that may not have reached disk)
  3. Transactions that were active at the crash → Undo (roll back partial changes)

Log Record Structure

Each log record (also called a Log Sequence Number — LSN) contains:

FieldDescription
LSNUnique, monotonically increasing ID
Trans IDWhich transaction made this change
Page IDWhich data page was modified
Old Value (Before Image)Value before the change (for Undo)
New Value (After Image)Value after the change (for Redo)
Prev LSNPrevious LSN of the same transaction

Types of Log Records

Record TypeMeaning
<T start>Transaction T has begun
<T X, V1, V2>T wrote V2 to data item X (old value V1)
<T commit>T has committed
<T abort>T has aborted

Undo and Redo

Undo

Used for transactions that were active at the time of the crash.

Process: Scan the log backwards. For each change by the active transaction, restore the old value.

Result: The database is returned to the state before the transaction started.

Example: If T1 had executed A = A - 100 but not committed before the crash, Undo sets A back to its previous value.

Redo

Used for transactions that committed but whose changes may not have reached the disk.

Process: Scan the log forward. For each committed transaction, re-apply the new value.

Result: All committed changes are guaranteed to be on disk.

Example: T1 committed, but the page containing A was still in memory when the crash occurred. Redo writes A - 100 to disk.


Recovery After a Crash

1. Identify the last checkpoint
2. Scan the log from the checkpoint forward
3. Build two lists:
   - Undo-list: transactions that never committed
   - Redo-list: transactions that committed
4. Undo all transactions on the Undo-list (backward scan)
5. Redo all transactions on the Redo-list (forward scan)
6. Database is consistent

Checkpoints

Without checkpoints, recovery would scan the ENTIRE log from the beginning of time — impractical for databases that have been running for years.

What is a Checkpoint?

A Checkpoint is a moment where the DBMS:

  1. Flushes all memory buffers to disk
  2. Writes a checkpoint record to the log (listing active transactions)

How Checkpoints Accelerate Recovery

Without CheckpointWith Checkpoint
Scan the entire log (millions of records)Scan only log AFTER the last checkpoint
All transactions since DB was createdOnly transactions since last checkpoint (minutes/hours)
Slow recovery (hours/days)Fast recovery (seconds/minutes)

Types of Checkpoints

TypeBehavior
Begin/End CheckpointRecords start and end of checkpoint — ensures consistency
Fuzzy CheckpointAllows transactions to continue during checkpoint — used by modern databases
Online CheckpointNo database downtime needed

Shadow Paging (Alternative to Logging)

An older alternative to log-based recovery.

FeatureLog-Based (WAL)Shadow Paging
MechanismLog every changeMaintain two page tables (current + shadow)
CommitWrite log, then apply changesSwap page table pointers
RollbackUndo from logDiscard current table, revert to shadow
PerformanceGood for all workloadsFast for crashes, slow for updates
Disk usageLog grows continuouslyMore disk fragmentation

Shadow Paging is simpler conceptually (no log to manage) but results in disk fragmentation and is slower for update-heavy workloads. Log-based recovery is the modern standard.


Interview Deep Dive

Q: Why do we use Checkpoints?

A: Without checkpoints, recovery would scan the ENTIRE log from the beginning of time. A Checkpoint ensures all memory buffers are flushed to disk. After a crash, recovery only needs to scan the log AFTER the last checkpoint — dramatically reducing recovery time.

Q: What is an LSN?

A: Log Sequence Number — a unique, monotonically increasing ID for every log record. Modern databases (PostgreSQL, SQL Server) use LSNs to compare the state of a page on disk vs the state in the log. If the page LSN ≥ the log LSN, the update is already applied (skip it). If page LSN < log LSN, the update needs Redo.

Q: What is Shadow Paging (vs Log-based recovery)?

A: A simpler alternative to logging. It maintains two page tables (current and shadow). Updates are made to the current table. On COMMIT, the shadow table pointer is updated to match the current. On crash, you just discard the current and revert to the fixed shadow. It avoids logging but causes disk fragmentation.

Q: What happens if data is written to disk but the log is lost?

A: This violates WAL — and the database cannot guarantee Atomicity or Durability. If the log is lost but data was written, the DBMS cannot determine if a transaction was supposed to be committed or rolled back. This is why WAL mandates: log records must reach stable storage BEFORE the commit is acknowledged.


Key Takeaways

  • Write-Ahead Logging (WAL): Log first, commit later — guarantees atomicity and durability.
  • Undo: Roll back changes of uncommitted transactions (backward scan).
  • Redo: Re-apply changes of committed transactions that didn’t reach disk (forward scan).
  • LSN: Unique identifier for each log record — used to compare page vs log state.
  • Checkpoints: Flush buffers to disk periodically to bound the log scan during recovery.
  • Shadow Paging: Alternative to WAL — simpler but slower and causes fragmentation.
  • Recovery after crash: Find last checkpoint → build Undo/Redo lists → Undo then Redo.
  • WAL is used by every modern database (PostgreSQL, MySQL, Oracle, SQL Server).

My Private Notes

Notes are auto-saved locally to this device.