free to learn.world runs in this page · zero background needed · nothing sent anywhere

Storage engines · Zero background needed · Visual guide + 9 interactive lessons · Free

B+Tree Storage Engine & Write-Ahead Log Crash Lab

A B+Tree is an even-height guide tree of linked data pages where every search takes the same few steps; a Write-Ahead Log (WAL) is a receipt journal that only grows at the end. Together they let databases like Postgres and SQLite keep rows sorted on disk and survive a pulled power cable.

Try the 5-step studio: five power cuts, one guess each →

Watch & Poke Studio · 5 steps · one per Short

Pull the plug on a database

A page holds rows on disk. A receipt records a change so the database can finish or undo it after a crash. Keep or remove a safety line, guess what survives, then pull the plug.

Step 1 · Split Crash

Pairs with Short 1

A full page holds eight rows numbered by tens (10, 20, 30, 40, 50, 60, 70, 80). The last four move to a new page. Searches start at the parent page, which needs a link to the new page. The power dies before the link reaches disk. Saving a split receipt in the receipt journal first lets restart finish that link.

journal.save(splitReceipt)
write(rightPage)   // 50..80
write(leftPage)    // 10..40
write(parentPointer) // power dies

Predict: after restart, which rows can a search reach?

Step 2 · Journal First

Pairs with Short 2

The balance changes from 100 to 150. The page write tears halfway through, leaving an unreadable balance on disk. Calling journal.flush() forces the receipt out of fast memory onto physical disk.

journal.append("set balance 150")
journal.flush()
page.write("balance 150") // tears

Predict: what does the page say after recovery?

Step 3 · Numbered Slots

Pairs with Short 3

Rows A, B, C and D start in places 1, 2, 3 and 4 at the bottom of a page. Numbered slots at the top point to those places. Deleting B leaves a hole, so the page packs A, C and D together. Each rowNumber stays fixed (1, 2, 3 and 4 for A through D), while bottomSpot counts the new places from 1.

removeRow(2)
for (let rowNumber of [1, 3, 4]) {
  move(rowNumber, bottomSpot)
  slot[rowNumber] = bottomSpot
  bottomSpot = bottomSpot + 1
}

Predict: what does reading row C return?

Step 4 · Replay & Erase

Pairs with Short 4

A and B both start at 100. A got +10 from a transfer that finished. B got +5 from a transfer that never finished, so recovery must erase it. Recovery first replays every receipt, then erases B's +5. The power dies again just after it erases.

replayEveryReceipt()
for (const step of unfinished) {
  step.reverse()
  journal.append(undone(step))
}

Predict: what is B after the second recovery?

Step 5 · Quick Checkpoint

Pairs with Short 5

Pages change in fast memory before reaching disk. Recovery replays receipts from a start point onward and skips everything before it. A background checkpoint writes a note, without pausing work: the oldest unsaved page's receipt, where the oldest unsaved memory page was first changed. Page P changed at receipt 3 and never reached disk. The checkpoint ran at receipt 5.

const note = lastCheckpoint()
let start = note.checkpointReceipt
start =
  note.oldestUnsavedPageReceipt
replayFrom(start)

Predict: does page P keep its change?