← Lessons

quiz vs the machine

Gold1450

Databases

The Copy On Write B Tree

A copy on write B tree never overwrites a page in place, instead writing new versions and swapping a single root pointer to commit atomically.

5 min read · core · beat Gold to climb

Avoiding In Place Writes

A traditional B tree edits pages in place, which risks a half written page after a crash. A copy on write B tree, also called a shadow paging tree, never modifies an existing page. Instead it writes a fresh copy of any page it changes.

How an Update Works

To change a leaf, the engine copies that leaf to a new location with the change applied. Its parent must now point to the new leaf, so the parent is copied too, and so on up to the root.

  • Every modified page is written to free space.
  • The chain of copies reaches the root.
  • The new root pointer is written last, which atomically publishes the whole change.

Benefits

  • A crash before the root swap leaves the old tree fully intact, so recovery is simple.
  • Old versions remain readable, giving cheap snapshots and consistent reads without blocking writers.

Costs

  • Writing a path of copies per update raises write amplification.
  • Reclaiming superseded pages requires tracking which old versions are still needed.

Key idea

A copy on write B tree writes new page copies up to a fresh root and swaps one pointer to commit, giving crash safety and snapshots at the cost of higher write amplification.

Check yourself

Answer to earn rating on the learn ladder.

1. What makes a copy on write update atomic?

2. Why does a copy on write B tree give cheap snapshots?

3. What is a cost of the copy on write approach?