A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Write-Ahead Logging and Database Recovery

College Depth 82 in the knowledge graph ☐ I know this ☆ Set as goal
2topics build on this
360prerequisites beneath it
See this on the map →
ACID PropertiesDatabase Transactions+1 more→→Checkpointing: Sharp and Fuzzy CheckpointsCrash Recovery: Undo and Redo Logging
WAL write-ahead logging recovery durability REDO UNDO checkpointing

Core Idea

Write-Ahead Logging (WAL) ensures atomicity and durability by requiring that every modification be recorded in a sequential log file before being applied to data pages. On crash recovery, the database REDOs all changes from committed transactions not yet reflected in data files, and UNDOs all changes from transactions that were in-flight at crash time, restoring a consistent state. Checkpoints periodically flush dirty buffer pages to disk and record a safe restart point, bounding the amount of log that must be replayed during recovery.

How It's Best Learned

Trace the WAL protocol through a simulated crash at various points (before commit, after commit but before flush, mid-write) to understand what REDO and UNDO must handle. Study the ARIES recovery algorithm at a conceptual level.

Common Misconceptions

Explainer

You already understand that ACID transactions guarantee atomicity (all or nothing) and durability (committed data survives crashes). But how does a database actually deliver these guarantees when it can crash at any moment — mid-write, after updating some pages but not others, or right after acknowledging a commit? The answer is Write-Ahead Logging (WAL): a deceptively simple protocol that underpins crash recovery in virtually every modern database.

The core rule is: before any change is applied to a data page on disk, the description of that change must first be written to a sequential log file and that log record must be flushed to stable storage. This is the "write-ahead" part — the log always leads the data. When you execute an UPDATE, the database writes a log record describing the change (old value, new value, transaction ID, page number), forces that record to disk, and only then is it safe to modify the actual data page. The data page itself might linger in the buffer pool in memory for a while before being written to disk — and that is perfectly fine, because the log already contains everything needed to reconstruct the change.

When a crash occurs, recovery proceeds in two phases. The REDO phase replays the log forward from the last checkpoint, reapplying all changes from committed transactions that may not have been flushed to data pages yet. This restores the effects of completed work. The UNDO phase reverses changes from transactions that were in progress but never committed at crash time — these partial changes may have reached data pages (since the buffer pool can flush dirty pages at any time), so they must be rolled back to maintain atomicity. After both phases complete, the database is in a consistent state: every committed transaction's effects are present, and every uncommitted transaction's effects are gone.

Checkpoints are the mechanism that keeps recovery time bounded. Without checkpoints, recovery would have to replay the entire log from the beginning of time. A checkpoint flushes all dirty buffer pages to disk and records a log sequence number marking a known-good state. On recovery, the database only needs to replay log records after the last checkpoint. In PostgreSQL, you can observe this directly: the `checkpoint_timeout` and `max_wal_size` settings control how often checkpoints occur, balancing recovery speed against I/O overhead during normal operation. More frequent checkpoints mean faster recovery but more background I/O; less frequent checkpoints mean longer recovery windows but smoother performance during steady-state operation.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Conditional Statements → Defining and Calling Functions → Functions: Decomposing Problems → Function Parameters and Argument Passing → Return Values → Error Handling and Exceptions → File I/O Basics → File System Concepts → Database Transactions → ACID Properties → Write-Ahead Logging and Database Recovery

Longest path: 83 steps · 360 total prerequisite topics

Prerequisites (3)

Leads To (2)