Unit 3 of 12 · about 4 hours · explain, measure
Buffered, flushed and durable write states and the fsync contract
Replication cannot repair an undefined local ack. A client that hears success must know whether the data survives a process crash, an OS crash or a power loss.
You'll be able to
- Distinguish buffered, flushed and durable write states and name which failures each survives.
- State an acknowledgement contract that names the exact sync step behind every ack.
- Measure sync latency on your machine and explain group commit from the numbers.
Covers
Read
In this order, only the parts listed
0 of 4 done
- PostgreSQL WAL configuration, internals and reliabilityPostgreSQL 18 Documentation - WAL Configuration and Internals
Section 28.5, WAL Configuration: the checkpoint paragraphs including the 5-minute
checkpoint_timeoutand 1 GBmax_wal_sizedefaults and the redo-record definition; then Section 28.6, WAL Internals, and Section 28.1, ReliabilitySection 28.1 also documents CRC-32C per WAL record and checksummed data pages with full-page images; read it as the contract statement, not as a tutorial.
- OSTEP journaling write orderingOperating Systems: Three Easy Pieces - Crash Consistency and Data Integrity
Chapter 42, Section 42.3: the checkpoint sequence and the side note on forcing writes to disk with barriers, plus the checksum-in-journal fix for batched transaction writes
- fsync contract and error semanticsLinux man-pages: fsync
DESCRIPTION for
fsyncversusfdatasyncand the directory-sync requirement for new file names; ERRORS: anEIOfrom write-back may concern data written through any descriptor of the file - PostgreSQL error handling for a failed flushPostgreSQL 18 Documentation - WAL Configuration and Internals
Section 19.14, Error Handling:
data_sync_retry, why PostgreSQL panics on a failed flush, and why a retried flush can report success after the kernel dropped the data
Focus on
- Buffered means visible to the process. Flushed means handed to the OS. Durable means confirmed on stable storage after
fsyncorfdatasyncreturns success. - Acknowledge only after the sync the contract names. An ack after the bytes reach the OS but before sync survives a process crash, not an OS crash or power loss; an ack while they still sit in the process's buffer survives neither. A renamed or newly created file needs its directory synced too.
- The Systems unit on files, page cache, fsync, atomic rename and durability taught the calls. Here the new material is the ack contract, group commit and measured sync latency.
- A failed
fsyncmay leave clean page cache over lost data, so retryingfsyncis not proof the data landed. Treat the write as uncertain and reconcile from the log.
Common mistakes · 3
- Reading
writereturn as durability. It confirms the OS accepted the bytes, not that any disk holds them. - Syncing once and assuming later writes are covered. Every byte needs its own completed sync before its ack.
- Calling a retry after a failed
fsynca fix. PostgreSQL'sdata_sync_retrydocumentation explains why: after a write-back failure the kernel may have dropped the data, so a secondfsynccan report success over lost bytes. Recovery must decide from the log, not from the retry.
Try it yourself
Closed-book, before checking anything
os.fsync and report the median and the spread over one run.Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.A server acks a write after
writebut beforefsync, then the OS crashes. Is the data durable?AnswerNo. The data was buffered and possibly flushed to the OS cache, but never confirmed on stable storage. Only post-sync acks survive an OS crash or power loss.
2.A file is synced but its directory entry is not, then power fails. What can be lost?Answer
The name. The file contents may sit on disk while the directory never records the rename or creation, so the file is unreachable after reboot.
3.Why does PostgreSQL batch several commits behind one flush with
commit_delay?AnswerEach flush pays one sync latency. Letting concurrent commits share the leader's sync amortizes that cost across transactions at the price of a small added delay.
4.What does the checkpoint redo record tell crash recovery?Answer
The WAL position from which REDO must start. All data-file changes before it are already on disk, so earlier WAL segments can be recycled.
You're done when
You can label any ack in your log as buffered, flushed or durable and point at the sync call that justifies it.
Only when you can do this without notes.
Leads to
Notes
Resources · 3 for this unit, 10 more in the section
- 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.
- 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.
- Linux man-pages: fsyncLinux man-pages project · support
Read the DESCRIPTION for what
fsyncandfdatasyncguarantee and the directory-sync requirement for new file names, and the ERRORS section for what anEIOfrom write-back can refer to.
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. - Hypothesis - Stateful TestingHypothesis · support
Use
RuleBasedStateMachinewith 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. - 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.
- 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.
- 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.
- 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.