Module 4 · Dive 4.1
The append-only file.
CubDB writes by appending and never by overwriting. Each commit adds its data and ends with a header; the last complete header is, by definition, the current state. That makes a commit a single atomic act and crash recovery a non-event — a half-written tail is just ignored. Append a few commits, then crash one mid-write.
The last header wins
interactiveAtomicity for free
store.design.mdA database that overwrites in place has to work to stay consistent through a crash — journals, double-writes, fsync dances — because a torn overwrite corrupts existing data. Append-only sidesteps all of it: existing bytes are never at risk, so the only question on recovery is “where does the good data end,” answered by scanning to the last valid header. The commit becomes visible exactly when its header lands, and not a moment before. This is the property the native engine leans on to call a page durable, and it is why the durable tier could be a single embedded file rather than a server.
References & sources
- External
- CubDB · how it worksappend-only file, headers, recovery
- lucaong/cubdbthe implementation
- Write-ahead loggingthe contrast: journal vs append-only
- Echo records
- store.design.mdwhy a page is durable on CubDB
- Designing Data-Intensive Applications, Kleppmann 2017log-structured storage, crash recovery (Ch. 3)