LESSON 04 · Choosing an engine

Compare engines by what they amplify

Three ratios describe what an engine costs, and row size and access pattern can flip the winner.

SYSTEM VIEW

The write path

Diagram view
Row size—
B-tree writes—
LSM writes—

Send the same row to a B-tree and an LSM tree, and compare what each one writes, reads and stores for it.

  1. “Faster” means little without a workload. The App sends the same 100-byte row to a B-tree and an LSM tree. Compare bytes written per byte sent, storage reads per query, and bytes on disk per live byte. No design minimizes all three.

    Write amp
    bytes written per byte
    Read amp
    reads per query
    Space amp
    disk per live byte

    What changed

    • One 100 B row to each engine
  2. Updates land all over a table far larger than memory, one changed row per flushed page. So the B-tree writes a whole 16 KB page for the 100-byte row: a write amplification of about 160, plus the log. That eats disk bandwidth and wears SSDs.

    Row
    100 B
    Page written
    16 KB
    Write amp
    about 160×, plus log

    What changed

    • 16 KB page write for a 100 B row
    • B-tree writes about 160×
  3. The LSM tree writes the row to a sorted file, then compaction merges it down four levels, each ten times larger than the last. A byte is rewritten up to about 10 times per level: up to about 40 times in all, about 4 KB for this row, plus the log.

    Levels
    4, each 10× larger
    Per level
    up to 10 rewrites
    Write amp
    up to 40×, plus log

    What changed

    • Sorted file, then merges down 4 levels
    • LSM tree writes up to about 40×
  4. Make the rows 4 KB. The B-tree still writes one 16 KB page per row, now about 4×; the LSM tree pays per byte, up to 160 KB, so it stays near 40×. The winner flips: pick 100 B rows to flip it back.

    What changed

    • 4 KB rows: B-tree about 4×
    • LSM tree about 40× at either size
    Row size
  5. A B-tree lookup follows one path, root to leaf. An LSM lookup checks files newest first until it finds the key, here in the third. Bloom filters narrow that gap for point reads, not for range scans, which merge every level.

    B-tree
    one path
    LSM tree
    several files
    Bloom filters
    point reads only

    What changed

    • One lookup on each engine
    • B-tree reads 1 path, LSM tree 3 files
  6. Space amplification is bytes on disk per byte of live data. Randomly filled B-tree pages are often about two-thirds full, so the B-tree takes near 1.5 times its live data. This LSM tree can run near 1.1 times.

    B-tree
    about 1.5× live data
    LSM tree
    about 1.1×

    What changed

    • B-tree pages about two-thirds full
    • B-tree 1.5×, LSM tree 1.1× on disk
  7. No engine wins all three. At 100 B rows the LSM tree writes about 40× to the B-tree’s 160×; key-order inserts help a B-tree, filling each page before it flushes. Favor an LSM tree for write-heavy small records and a B-tree for read-heavy range queries, then benchmark with real data.

    Many small writes
    LSM tree
    Many range reads
    B-tree
    Then
    benchmark

    What changed

    • 100 B rows: LSM tree 40×, B-tree 160×
    • LSM tree for write-heavy small records
    • B-tree for read-heavy range queries

Space plays · ← → step · Home resets

THE IDEA

How it works

“Faster” means little without a workload. Three ratios say more. Write amplification is bytes written to storage per byte the application wrote. Read amplification is storage reads per query. Space amplification is bytes on disk per byte of live data. High write amplification eats disk bandwidth and wears SSDs, and no design minimizes all three.

Assume updates land all over a table far larger than memory, on 16 KB B-tree pages, with one changed row per flushed page. A 100-byte row then costs 16 KB of page writes, a write amplification of about 160, plus the log. An LSM tree with four levels, each ten times larger than the last, rewrites a byte up to about 10 times per level, so up to about 40, plus the log. Now make the rows 4 KB. The B-tree drops to about 4, while the LSM tree’s figure barely moves, because its cost is per byte rather than per page. The winner flipped. Inserting in key order also helps a B-tree, since each page fills before it is flushed.

Reads lean the other way. A B-tree lookup follows one path, while an LSM lookup may check several files. Bloom filters narrow that gap for point reads but not for range scans, which merge every level. On space, this LSM tree can run near 1.1 times live data, while randomly filled B-tree pages are often about two-thirds full, or near 1.5 times. Start by favoring an LSM tree for write-heavy small records and a B-tree for read-heavy range queries, then benchmark with real data.

DECISION POINT

You are choosing an engine for a fleet-sensor service: 50,000 writes per second of 200-byte readings, with a primary key of (device, time). Dashboards read one device’s last hour. Storage is your largest cost. Which engine would you start with, and what would you test?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • Compare engines on three ratios: bytes written, reads per query, and disk used per live byte.
  • Row size and access pattern, not an engine’s label, decide which one writes less.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
ApplyQUESTION 01

A 16 KB page is flushed for each single changed 200-byte row. About what write amplification do the page writes alone cause?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.