← Back

Shadow Paging

5 min read

The Problem

What happens when a large block of data is being written to a database — and the writer crashes halfway through?

Say a transaction must update 3 pages: it successfully writes page 1, and then the process dies mid-way through page 2. The database is now in a partial state: page 1 holds the new data, pages 2 and 3 hold the old. No version of the data — old or new — fully exists on disk. If a reader arrives now, it sees a database that never logically existed: half-new, half-old, and internally inconsistent. A crash has effectively corrupted the database, even though no hardware failed.

This is precisely why atomicity is needed — the A in ACID (see Concurrency Control Theory in Databases for how the four guarantees divide up). A transaction must execute all-or-nothing: either every one of its changes lands, or none of them do. There is no acceptable middle ground where a transaction is "half-applied." The database needs a mechanism that guarantees a transaction's writes either fully take effect or vanish entirely — even if the process dies at the worst possible moment. Shadow paging is one of the classic techniques built to deliver exactly that guarantee.

How Can This Be Achieved?

So how can all-or-nothing writes be achieved — with the power to die at any instant and still leave a clean database behind? One of the classic techniques is shadow paging.

What is a Database Page?

In a database system, a page is a fixed-size block of storage (typically 4 KB, 8 KB, or 16 KB). Because disks and RAM process data in blocks rather than individual bytes, the database divides its storage into pages. A single page contains actual database records — table rows (tuples), index entries, or header metadata — and whenever the database reads or updates data, it moves entire pages between disk and RAM (the buffer pool).

How Shadow Paging Works

Shadow paging achieves atomicity through a copy-on-write mechanism built around a page directory — a master index pointing to where each active page lives on disk:

  1. Creating the "shadow" copy: when a transaction needs to modify a record on Page AA, the database does not overwrite Page AA directly. It allocates a brand-new page on disk (the shadow page), copies Page AA's contents into it, and makes all modifications on that copy.
  2. Maintaining the master directory: while the transaction runs, the page directory still points to the original, untouched Page AA. Any other query reading the database during this time sees only the original data.
  3. The atomic pointer flip (commit): when the transaction completes, the database performs a single atomic operation — it flips the pointer in the page directory to the new shadow page. In an instant, all updates become live.
  4. Aborts and crashes: if the transaction aborts or the system crashes before the flip, the directory still points to the old untouched pages. The database simply discards the uncommitted shadow pages and opens in a completely uncorrupted state.
Note

Copy-on-Write (CoW) is an optimization strategy with a simple rule: don't make a copy of data until someone actually tries to edit it. As long as everyone only reads, they all share the same original version; the system creates a new copy the moment a process attempts to write to it.

Picture a teacher handing out a master paper copy of class notes to 20 students. All 20 read the same master document — nobody needs a personal copy, because nobody is changing it. Then Student A wants to highlight text and scribble in the margins: instead of defacing the master copy, the system makes a photocopy of that specific page for Student A. Student A writes on the private copy while the other 19 keep reading the untouched original, undisrupted.

In shadow paging, CoW provides the two safety benefits that make atomicity work: easy rollback (a crashed or aborted transaction just throws away its private copy — the original stays pristine) and isolation (readers see only the original while updates happen on the copy — no one ever sees incomplete work).

Pros and Cons

Pros:

Cons:

Historically, IBM invented shadow paging in the 1970s for System R, but modern production databases (PostgreSQL, MySQL, Oracle) abandoned it in favor of Write-Ahead Logging to support massive multi-core parallelism. Specialized and embedded systems — SQLite (in certain modes), LMDB, CouchDB — still use shadow paging or copy-on-write trees today for fast, crash-resilient storage.