Topic 067 / Advanced
Storage engine fundamentals: pages, compaction, checkpoints
Chapter 5.4 · Engines, Durability, Transactions, and Concurrency
What and why
Every database stores data on disk as fixed-size pages and must periodically reconcile what's in memory with what's safely on disk. Two storage-engine shapes dominate: B-tree engines, which update pages in place, and LSM engines, which never update in place at all. The shape chosen explains most of a database's observed read and write behavior under load. Builds on: → 64 (indexing: B-tree, hash and LSM-tree).
Diagram
| B-tree engine | LSM engine | |
|---|---|---|
| Write | update the page in place | append to an in-memory buffer |
| Flush | periodic checkpoint: dirty pages to disk | buffer flushed as a new sorted file |
| Cleanup | none needed (in place) | background compaction merges files |
| Read cost | one path down the tree | may check several files until compacted |
How it works
- Data is read and written in fixed-size pages, not individual bytes.
- B-tree engines modify the relevant page in place and rely on the write-ahead log (→ 68) for crash safety between checkpoints.
- A checkpoint flushes modified ("dirty") pages to disk and lets older log entries be discarded — it bounds how much log must be replayed on crash recovery.
- LSM engines instead buffer writes in memory (a memtable), flush the buffer as an immutable sorted file when full, and never modify a file after it's written.
- Compaction merges these sorted files in the background, discarding entries that were later overwritten or deleted and bounding how many files a read must check.
- The trade-off: B-tree favors read latency and pays a write-amplification cost on in-place updates and page splits; LSM favors write throughput (sequential appends) and pays its cost later, in background compaction I/O.
Practical example
(illustrative, not a stated vendor default). A burst of random writes against a B-tree engine causes page splits and scattered disk writes; the same burst against an LSM engine is a sequential append to the current file — cheap immediately, with the merge cost deferred to compaction running later in the background.
When to use it
- Use when: diagnosing why a system behaves a certain way under load — periodic checkpoint I/O spikes (B-tree) or compaction-driven write stalls (LSM) are explained by this, not a mystery.
- Avoid when: picking an engine from internals alone without testing the actual workload — theory about write amplification doesn't replace a load test.
- Failure scenarios: a compaction backlog during a sustained write burst causing read latency to climb as more files must be checked per read; a checkpoint storm causing a periodic latency bump on an otherwise idle system.
- Simpler alternative: for most application teams, treat the storage engine as a black box and only study internals when a specific, measured pathology needs explaining.
Practice
(15–20 min, lab Postgres — reason, don't rebuild LSM). Insert a large batch
of rows into a lab table, then run VACUUM and note what it does conceptually (reclaiming
space from updated/deleted rows — not identical to LSM compaction, but the same family of
background cleanup). Then write, in your own words, which engine shape (B-tree-like or
LSM-like) would show better write latency for a workload that is 95% inserts with almost
no updates, and why. Done when your prediction names the structural reason (in-place page
writes vs. sequential appends), not just the engine label.
Key takeaway
B-tree engines pay their cost on write (in place, with checkpoints); LSM engines defer it to background compaction. Done when you can: explain a checkpoint I/O spike or a compaction-driven write stall from the engine's shape alone. Used next in → 68.
References
Kleppmann and Riccomini, Designing Data-Intensive Applications — see
references.md.