← Build Your Own Database
Stage 2: The Write-Ahead Log
stage 2 of 4 · ~25 min · runs in your browser
RAM dies with the process. Before a real engine touches the memtable, it appends the operation to a write-ahead log (WAL) — an append-only record that survives crashes. On restart, replaying the log rebuilds the memtable exactly. This is the durability contract of every serious database.
Extend your store: createStore(existingLog) where:
- every
setappends{ op: "set", key, value }to astore.logarray, everydelappends{ op: "del", key } store.logis the array itself (the harness inspects it)- when
createStore(existingLog)receives a previous run's log, it replays it so the rebuilt store answersgets identically
The crash test below is literal: it builds a store, "crashes" it, and hands your constructor the old log. Your engine either remembers or it doesn't.
tests (4)
○ still a working store
○ mutations are appended to the log in order
○ survives a crash via log replay
○ replay preserves overwrite order
createStore.js