Course contentsShow
← Computer Science
Lesson 1407 of 2,87232. File SystemsFree lesson

Write-Ahead Logging and Recovery

Learn the protocol of writing log entries before modifying file system structures for safe recovery.

Write-Ahead Logging and Recovery

What you'll learn: How file systems use write-ahead logging to guarantee safe recovery after crashes by writing changes to a log before updating actual structures.


The Core Problem

When a file system modifies data—creating files, updating inodes, or changing directory entries—these operations often involve multiple disk writes. If the system crashes mid-operation, you're left with inconsistent structures. How do we ensure we can recover to a consistent state?

The Write-Ahead Logging Protocol

Write-Ahead Logging (WAL) follows a simple but powerful rule: always write what you're about to do to a log before doing it.

Here's the protocol:

  1. Log the change: Before modifying any file system structure (inode, data block, free space bitmap), write a descriptive log entry to a dedicated log area on disk
  2. Wait for log commit: Ensure the log entry is safely on disk (the write completes)
  3. Perform the actual operation: Now modify the real file system structures
  4. Mark complete: Optionally note in the log that the operation finished

Recovery After a Crash

On reboot after a crash, the file system scans the log:

  • Logged but not completed? Replay the operation (redo)
  • Not logged? The operation never started; nothing to fix
  • Logged and marked complete? Skip it; already done

This is like a chef writing down "add salt to pot 3" on a clipboard before actually doing it. If the kitchen catches fire mid-shift, the next chef reads the clipboard and knows exactly what was in progress.

Why It Works

The log becomes your single source of truth during recovery. Because disk writes are atomic at the sector level, either the log entry makes it to disk completely or not at all—no partial confusion.

Key Takeaway:
Write-ahead logging ensures recoverability by committing planned changes to a durable log before modifying file system structures, enabling safe replay or rollback after crashes.