Definition
Shadow paging is a technique for updating stored data so that a crash leaves either the complete old version or the complete new version. The data is divided into fixed-size blocks called pages, and a page table records where each page is stored. An update never overwrites a page in use: changed pages go to free locations, and a new page table lists them. One small write then replaces the root, the fixed location that names the current page table. That write is the commit.
The idea
A file of 4 pages is updated in place, page by page. Power fails after 2 of the 3 changed pages have been written. The file is now a mixture that no program ever wrote, part new version and part old, and nothing records which page is which. Repairing it needs extra information, such as a log of the old or the new contents.
Shadow paging avoids the mixture by never touching the old version. The new version is built beside the old one and shares every page that did not change, and the switch from old to new happens in one small write. A library catalog is a fair picture: new editions are shelved in empty places, a new catalog lists them together with the unchanged books, and one notice at the entrance says which catalog is current. The picture stops in two places. Changing the notice must be indivisible, which a storage device does not always guarantee, and the old books may be removed only when no reader still holds the old catalog.
The technique comes from Raymond Lorie’s 1977 work on the System R database [515]. In the classic description the shadow is the old page table, kept unchanged until the commit, together with the pages it lists. Some later texts call the new copies the shadow pages instead, so check which meaning a text uses.
How it works
Three structures take part.
The pages hold the data.
The page table gives, for each page number, the location where that page is stored.
The root is one small record at a fixed, known location that holds the location of the current page table. Recovery and every reader start at the root.
An update runs in seven steps.
Read the root to find the current page table .
For each page to change, take a free location and write the new contents there. Never write to a location that uses.
Write a new page table to a free location: a copy of in which the entries of the changed pages give the new locations.
Flush: wait until the new pages and are stored permanently. Only then may the root be written, because a storage device may otherwise store the root before the pages it names.
Commit: overwrite the root with the location of in one atomic write, meaning a write that after a crash is found either complete or not done at all, and flush it. From now on, recovery finds the new version.
Publish the new version to programs reading the data in memory, for example by replacing an in-memory pointer to the current page table.
Reclaim: a location used only by , such as an old copy of a changed page or itself, may be reused once the new root is durable and every reader that started from has finished.
Recovery after a crash is short: read the root and use the version it names. Pages written after the last commit are not reachable from the root, and recovery treats their locations as free. No log is replayed and nothing is undone.
When the page table is itself a tree of pages, as in a B-tree (a balanced search tree §14.10 whose nodes are pages), changing one page means writing new copies of every node on the path from that page up to the top of the tree, because each parent must point to the new copy of its child. This is copy-on-write §8.11 applied to a tree, and copy-on-write file systems and databases work this way.
Worked example
Storage has 10 locations, numbered 0 to 9, of 4 KiB each, and a separate root location R. A file has 4 pages. Page table , stored at location 4, says: page 0 at location 0, page 1 at 1, page 2 at 2, page 3 at 3. R holds 4. The update changes page 2.
Write the new page 2 at free location 5.
Write at free location 6: page 0 at 0, page 1 at 1, page 2 at 5, page 3 at 3.
Flush locations 5 and 6.
Write 6 into R and flush.
Crash cuts. If power fails before step 4 completes, R still holds 4. Recovery follows and finds the complete old version; locations 5 and 6 are unreachable and become free. If power fails after step 4, R holds 6 and recovery finds the complete new version. No crash gives a mixture, provided that the write of R is atomic.
Space. The file occupies 4 data pages and 1 page table, KiB. During the update, the old page 2 and must stay while the new page 2 and are added, so the update needs KiB of free space, a peak of KiB. An update in place needs no extra space but gives no crash safety.
Reclaiming. After step 4, locations 2 and 4 hold data that only the old version uses. A reader X read R before the commit and is still reading page 2 from location 2. If location 2 were reused now, X would read data that belongs to something else, so locations 2 and 4 may be reused only after X finishes. Had the root been switched in memory but not yet written durably, a crash would return the file to , which needs location 2; so reclaiming also waits for the durable root write.
Stated precisely
Let be the set of storage locations reachable from root value : the page table that names and every page that table lists. Write for the root value that is durably stored, for a root value about to be committed, and for the root value that a reader started from. Shadow paging keeps this invariant: every location in holds complete, durable contents, and no location in is written while is the durable root.
Commit guarantee. Suppose that (1) every location in is durable before the root write begins, and (2) the root write is atomic with respect to power loss. Then after any crash, the recovered data equals exactly the version of the old or the version of .
Visibility. Two different events define it. For recovery, the new version exists from the moment the root write is durable. For readers in memory, it exists from the moment the in-memory root pointer is replaced, which must be an atomic operation §9.8 so that no reader sees half of an old pointer and half of a new one. Publishing in memory before the durable root write lets readers act on a version that a crash can still erase; publishing after it avoids that.
Reclamation. A location may be reused only when belongs to none of , , and for every reader still running.
Limits and common mistakes
The root write must be atomic, and many storage devices do not promise that. Software for disks has traditionally assumed that one sector, 512 bytes or 4 KiB, is written all or nothing. Flash memory §6.6 can be left with a partly programmed word after a power loss, which is a torn write §8.19. The usual remedy is two root locations written alternately, each with a sequence number and a checksum §4.32; recovery takes the valid copy with the higher number. LMDB (Lightning Memory-Mapped Database), for example, keeps two such meta pages.
Write ordering §8.20 is part of correctness. Write caches and storage controllers may reorder writes; without a flush between the page writes and the root write, the root can reach storage before the pages it names.
Every commit writes a new page table, or with a tree a new copy of the whole path to the top, even for a one-byte change; this is a form of write amplification §8.18, extra physical writing for each logical change. Changed pages also land wherever space is free, so data that was stored contiguously becomes scattered, which slows sequential reading on a disk.
One root switch commits everything in the new page table at once, so two transactions §8.7 that run at the same time cannot commit separately without extra machinery. Database textbooks give this, the scattering of pages and the cost of copying page tables as the reasons general-purpose databases prefer write-ahead logging §8.12.
Locations that no root reaches any more must be found and freed, either by tracking which locations each version uses or by scanning from the root after a crash. This is a form of garbage collection §8.16.
Shadow paging protects the stored layout, not the meaning of the data. A complete new version is still wrong if the program that built it computed wrong values, and the root write does not show who produced the data it points to.
Questions answered
Which pointer update defines visibility, and when may old storage be reclaimed?
The write of the root defines visibility. In shadow paging the root is the one record, at a fixed location, that names the current page table, and the new version becomes visible when that pointer is replaced. Two separate events count. For crash recovery, the new version exists once the new root value is durably stored, because recovery starts from the stored root. For programs reading the data in memory, it exists once the in-memory root pointer has been replaced by an atomic operation. Everything the new root reaches must be durably stored before the root is written; otherwise a crash can leave a root that points at pages that were never written.
Old storage may be reclaimed only when both of these hold: the new root is durable, so that no crash can bring back the old version; and every reader that started from the old root has finished, so that no running reader still follows old pointers. A page that the new version shares with the old one is never reclaimed by this step, because the new root still reaches it.
Related entries: A/B update (§8.10), Copy-on-write (§8.11), Commit record and selector (§8.21), Merkle trees and authenticated data structures (§5.43), Torn write (§8.19), Write ordering (§8.20), Write-ahead logging (§8.12). Further reading: [515].