Unit 12 of 12 · about 6 hours · implement

A crash-safe key-value store with crash and corruption injection and model-based tests

This is the section artifact. Every earlier unit lands here: ordered records, sync states, torn tails, scans, indexes, compaction and measured amplification.

You'll be able to

  • Implement a checksummed append-only key-value store with a declared acknowledgement contract.
  • Inject crashes at every persistence boundary and corrupted bytes at every read boundary.
  • Test random operation sequences against a dictionary model and fail loudly on divergence.

Covers

Crash-safe append-only key-value storeCrash injection at every persistence boundaryCorruption injection and terminal failureRandomized model-based operation tests
1

Read

In this order, only the parts listed

0 of 4 done

  1. Hypothesis stateful testingHypothesis - Stateful Testing

    Rule-based state machines with rule, invariant, Bundle and consumes, including the DatabaseComparison example that tests a store against a dict model

    Record the Hypothesis version with every saved failure. The machine proves only the explored interleavings; plant a bug to show it detects the fault class before trusting a pass.

  2. OSTEP data integrity as the corruption specOperating Systems: Three Easy Pieces - Crash Consistency and Data Integrity

    Chapter 45, Sections 45.3 to 45.7: checksums, misdirected writes, lost writes and scrubbing

    Turn each failure mode into one injection point: torn tail, flipped payload bit, misdirected record and lost write.

  3. Build Your Own Redis key-value server as a warm-upBuild Your Own Redis

    Chapter 7, Key-Value Server, with Chapters 8 and 9 for hashtable and serialization behavior

    Use this only for request parsing and in-memory behavior. It defines no acknowledgement or crash contract, so it must not replace your crash-tested store.

  4. Replicated log storage slice as the artifact specReplicated Log and Stream Processor - Storage Slice

    Core requirements on checksummed WAL segments with recovery and atomic metadata publication, plus Milestone A on the local append log with torn and corrupt tail recovery

    Implement the local slice only: segments, indexes, ack states, recovery and atomic metadata. Replication and consensus arrive in later sections.

2

Focus on

  • Name the ack contract first: which sync backs every success reply. Buffered, flushed and durable acks are separate code paths with separate tests. The Programming units on property, metamorphic and differential testing with shrinking and on regression tests, failure injection and flaky tests taught the tools; here they test a crash contract.
  • Inject at every boundary: before and after each append, checksum, flush, index update, manifest swap and ack. A boundary without a crash test is an untested promise.
  • Corruption has two outcomes: truncate a corrupt tail that was never acknowledged and continue, or halt on corrupt middle state. Never skip a bad record and keep reading as if the log were whole.
  • The dict model decides truth for every random sequence. Identical puts, deletes and reads run against both sides; any divergence fails the run with the shrunken sequence kept.
Common mistakes · 3
  • Trusting a green run as proof. The machine covers explored interleavings against your model; an incomplete model or short run hides whole fault classes.
  • Fsyncing once per batch but acking per write. Either ack after the shared sync completes or track per-write sync state; mixed accounting loses acknowledged writes.
  • Recovering past corruption by skipping. A corrupt tail truncates; corrupt middle state is terminal until an operator or snapshot intervenes.
3

Try it yourself

Closed-book, before checking anything

From a blank file, build a length-prefixed CRC log with put, get and delete plus an in-memory index, then drive it with a RuleBasedStateMachine using Bundle keys against a dict model. Plant a no-op delete bug and show the machine fails; fix it and show the run passes with the seed retained.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.What must a crash test cover that a normal operation test does not?Answer

    Every persistence boundary in both orders: crash before and after each append, checksum write, flush, index update, manifest swap and ack, then recovery to the contract state.

  2. 2.The model says a key is deleted but the store returns its value. What failed?Answer

    The implementation diverged from the model on the delete path. Keep the shrunken failing sequence as the regression test and fix the tombstone or index update.

  3. 3.Recovery finds a corrupt record with valid records after it. What is the correct behavior?Answer

    Halt with a terminal corruption error, preserving the evidence. Only a corrupt tail may truncate; skipping middle corruption invents history.

  4. 4.Why retain the random seed with every failing sequence?Answer

    The seed plus the shrunken steps make the failure replayable on any machine. Without them the fault report is an anecdote, not a regression test.

You're done when

Random sequences pass against the model, every injected crash recovers to the ack contract, and corruption either truncates the tail or halts with evidence.

Only when you can do this without notes.

Leads to

Notes

Resources · 4 for this unit, 9 more in the section
  • Hypothesis - Stateful TestingHypothesis · support

    Use RuleBasedStateMachine with rules, bundles and invariants to drive your store and a dictionary model with the same random operation sequences, and check the machine catches a planted bug before trusting a pass.

  • Operating Systems: Three Easy Pieces - Crash Consistency and Data IntegrityRemzi and Andrea Arpaci-Dusseau · required

    Read Chapter 42 (FSCK and journaling: the crash scenarios, data and metadata journaling and their write order) and Chapter 45 (data integrity: checksums, misdirected and lost writes, scrubbing; https://pages.cs.wisc.edu/~remzi/OSTEP/file-integrity.pdf). Turn each crash scenario into an injection point in your store.

  • Build Your Own RedisBuild Your Own · optional

    Use the persistence and data-structure portions only if a second implementation is needed to cement parsing, indexing or append-log behavior. Do not replace the crash-tested stage artifact with tutorial completion.

  • Replicated Log and Stream Processor - Storage SliceRoadmap-Final · support

    Implement checksummed segments, indexes, acknowledgement states, recovery, retention and atomic metadata changes. Preserve every crash schedule and corrupted artifact needed to reproduce a failure.

Also in this section

  • CMU 15-445 - Storage, Index and Recovery SequenceCarnegie Mellon Database Group · primary

    Follow Fall 2025 lectures 3 to 10 and 21 to 22. Do the BusTub projects from tag v20251119-2025fall (https://github.com/cmu-db/bustub/tree/v20251119-2025fall): P1 (ARC replacer, disk scheduler and buffer pool manager) and P2 (B+ tree with a leaf tombstone buffer), with P0 (Count-Min Sketch) as the C++ warm-up. Tests ship with a DISABLED_ prefix to remove, and GCC 15 needs -DCMAKE_CXX_FLAGS="-include cstdint" at configure time. Use the storage and recovery homework for WAL reasoning, and keep C++ scope bounded to storage and recovery.

  • DDIA 2e - Chapter 4 Storage and RetrievalMartin Kleppmann and Chris Riccomini · required

    Read "Storage and Indexing for OLTP" (log-structured storage, B-trees, their comparison and secondary indexes) and "Data Storage for Analytics" (column-oriented storage). Its Bloom filter, tombstone and write-ahead log coverage is brief, so take those from OSTEP and The Internals of PostgreSQL. Build a workload decision table, then connect each claim to a measurement from your small store.

  • Building Distributed Systems - The Logger and IndexesBuild Distributed Systems · optional

    Only the first three tasks are free (a write-ahead log, its recovery and log segments). Tasks are graded in the browser after login, so treat a pass as a drill and keep your local harness as the evidence.

  • HLD Handbook - Storage Engines and Data StructuresHLD Handbook · required

    Read Part 0 data structures plus Part 4 storage engines and OLTP versus OLAP. Use its exercises for comparison, while the CMU project and local fault tests remain the implementation evidence.

  • The Internals of PostgreSQL - Buffer Manager, WAL and RecoveryHironobu Suzuki · support

    Read Chapter 1 Sections 1.2 to 1.4 (heap pages and tuples), Chapter 8 (buffer manager and dirty-page flushing) and Chapter 9 Sections 9.1 to 9.8 (WAL segments, XLOG records, checkpoints and recovery). Compare each mechanism with your simplified store. The site is revised often, so note the date you read it.

  • PostgreSQL 18 Documentation - WAL Configuration and InternalsPostgreSQL Global Development Group · support

    Read "WAL Configuration" (checkpoints and the redo point crash recovery starts from), "WAL Internals" and "Reliability" for the official, versioned statement of the write-ahead and durability contract.

  • ARIES: A Transaction Recovery Method (Mohan et al., 1992)ACM TODS (Stanford course copy) · support

    Read the introduction, the overview and restart processing (analysis, redo by repeating history, undo) and the page LSN rule. The rest is 1992 storage detail beyond this section.

  • Apache Parquet File FormatApache Software Foundation · support

    Read the File Format layout (row groups, column chunks, footer metadata) and the Page Index page (ColumnIndex and OffsetIndex with per-page min and max values for page skipping). Use it only for file layout and skipping; it does not cover table metadata or catalogs.

  • Linux man-pages: fsyncLinux man-pages project · support

    Read the DESCRIPTION for what fsync and fdatasync guarantee and the directory-sync requirement for new file names, and the ERRORS section for what an EIO from write-back can refer to.