DatabasesDeeper

Kayavak / Databases / DB·58 Storage Engine Internals / Deeper

Deeper · Databases

Storage Engine Internals

The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.

Why does this exist?

Disks reward reading large blocks and punish seeks, and that shaped storage engines for fifty years. Rudolf Bayer and Edward McCreight's B-tree (1972) made each tree node a disk page holding hundreds of keys, so finding any of billions of rows takes a handful of page reads. System R and its successors added a write-ahead log, so a change becomes durable by appending to one sequential file rather than rewriting scattered pages.

Write-heavy workloads pushed another design. The log-structured merge tree (O'Neil and others, 1996) buffers writes in memory, flushes them as sorted files and merges files in the background, turning random writes into sequential ones. Google's Bigtable (2006), then LevelDB (2011) and Facebook's RocksDB (2012) made LSM trees the engine of many modern databases. SSDs changed the costs again (random reads are cheap, rewriting flash wears it out), and engines keep adapting.

The mental model

Try it yourself first: write your own model in the Mental Model Explorer, then compare.

Every engine chooses which of three costs to pay: read amplification, write amplification or space amplification. B-trees favour reads and update in place; LSM trees favour writes and pay at read and compaction time.

Question Answer
What is it? The layer that stores rows and indexes in pages on disk and keeps them durable
Problem it solves Fast lookups, scans and writes over data much larger than memory, surviving crashes
Parts Pages, heap or clustered storage, B-tree and LSM indexes, the buffer pool, the WAL, checkpoints, compaction
How it works Log the change, apply it to pages in memory, write pages back later; on a crash, replay the log
Assumptions The log is flushed before a commit is acknowledged; the hardware honours flushes
When they change A disk or controller that lies about flushing loses "committed" data on power failure
Where it fails Random-key inserts splitting B-tree pages; LSM compaction falling behind heavy writes
Related ideas B-trees and LSM trees (Foundations, ch03); fsync (Foundations, ch08); PostgreSQL internals (ch57)

What if?

What if you used random UUIDs as a clustered primary key?

Reason it through, then open

Each insert lands on a random leaf page, so the whole index must stay in memory to avoid disk reads, pages split everywhere and fill to about half, and caches thrash. Sequential keys (or time-ordered UUIDs such as UUIDv7) append to the rightmost page instead, keeping inserts cheap and pages full.

What if a database wrote data pages directly instead of logging first?

Reason it through, then open

A crash in the middle of a transaction would leave some pages changed and others not, with no record of what was intended: a torn, inconsistent database. Write-ahead logging records the intent first, sequentially and cheaply; after a crash, redo the committed changes and undo the uncommitted ones from the log.

Where the boundary is

Aspect B-tree LSM tree
Writes In place, random page writes Appended, sorted and merged later
Reads One path down the tree May check several levels (Bloom filters help)
Space Pages partly empty after splits Old versions until compaction
Suits Read-heavy, mixed OLTP Write-heavy, ingest-heavy

Derive it

Why a B-tree is so shallow.

  1. An 8 KB page holding keys of about 16 bytes plus pointers fits roughly 400–500 entries, so each inner node has a fan-out f of about 500.
  2. A tree of height h can reach f^h leaves: 500¹ = 500, 500² = 250,000, 500³ = 125 million, 500⁴ = 62.5 billion.
  3. So four or five levels cover billions of rows, and finding any row means reading that many pages.
  4. The top levels are a tiny fraction of the tree (one root, a few hundred second-level pages) and stay in memory, so in practice a lookup costs one or two disk reads.
  5. A binary tree over the same billion rows would be about 30 levels deep, 30 random reads: the high fan-out is the whole trick.

How sure are we?

Claim How it's known Where from
B-tree height grows with log base fan-out of the size Theorem Bayer and McCreight, 1972
LSM trees trade read and space cost for write throughput Model O'Neil et al., 1996; the RUM conjecture (Athanassoulis et al., 2016) frames the trade
Random keys cause page splits and poor locality Measured The chapter's lab; widely reproduced

Still open

Designing engines for new hardware (persistent memory, very fast NVMe, disaggregated storage in the cloud) is active research and engineering, as are learned indexes and adaptive structures.

What would change your mind? If random and sequential writes cost the same on your storage, LSM trees' main advantage would shrink. On flash, rewrites still wear the device.

Recall

Why are B-trees only four or five levels deep for billions of rows?

Each node holds hundreds of keys, so capacity grows as fan-out to the power of height.

What does write-ahead logging guarantee?

Every change is recorded durably before data pages change, so crashes can be repaired from the log.

What three costs do storage engines trade?

Read, write and space amplification.

Why are random UUID primary keys slow in clustered indexes?

Inserts scatter across pages, causing splits, half-full pages and poor caching.

Reflect

Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.