5-step crash studio
Pull the plug on a database
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.
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?
Start Lesson 1 · Saved, Then Lost →
Local test & source checkout
git clone https://github.com/itsHabib/free-to-learn.git
cd free-to-learn
git switch --detach 7dba0d743224c10a707307769782b0db3f9a2a1a
cd courses/btree-wal-lab
node --test tests/btree-wal.test.mjsGive feedback on this course · Usage & privacy
Three Ways In · Layer 3 of 3 · Node.js 22+ · Zero Dependencies
Build With Your Coding Agent — 5 Rungs + Planted Traps
Build a crash-safe storage engine with your coding agent across five rungs: checksummed 4 KB slotted pages, B+Tree leaf/internal splits, steal/no-force write-ahead logging, ARIES undo with compensation log records, and fuzzy checkpoints. Each rung gives you an architect prompt for Claude Code, Codex, or Cursor, a planted bug that unguided agents write, and a deterministic test suite that catches it.
- 4 KB slotted page: Slotted page layout with free-space compaction and torn-write checksum — Planted trap: Trusting header length or missing mid-page torn writes without full-page checksum verification.
- B+Tree splits: Leaf copy-up splits and internal push-up splits — Planted trap: Using leaf copy-up logic for internal nodes so separator keys duplicate across levels.
- WAL & buffer pool: Steal/no-force buffer eviction enforcing WAL-before-data — Planted trap: Evicting a dirty page to disk before flushing its log record up to pageLSN.
- Crash-safe recovery: ARIES redo and undo with Compensation Log Records (CLRs) — Planted trap: Undoing in place without writing CLRs so a second crash during recovery corrupts state.
- Fuzzy checkpoints: Active Transaction Table, Dirty Page Table, and min-recLSN redo scan — Planted trap: Starting redo at the checkpoint LSN instead of the oldest dirty page's recLSN.
unzip agent-track.zip && cd btree-wal-lab/agent-track
node --test test/*.test.mjs
mkdir -p work
IMPL=work node --test test/rung-01.test.mjs
Download 5-Rung Agent Track (agent-track.zip) Read AGENT-TRACK.md on GitHub Open agent-track/README.md on GitHub