LESSON 08 · Storage engine

Trace a local read and write

Durable acknowledgment depends on the path inside a replica.

SYSTEM VIEW

The storage path

Diagram view
Acked writes—
Durable writes—
Files opened—

Watch one write reach a replica’s log and memtable, see where it becomes durable, then follow a read.

  1. The Coordinator sends put(order:42, v2) to this replica. It appends 42 = v2 to the Commit log at offset 4, past the fsynced line, so it sits only in the page cache. The Memtable, sorted in memory, takes it too.

    Offset
    4
    fsynced
    offsets 1–3

    What changed

    • 42 = v2 at offset 4
    • 42 = v2 in the Memtable
    • Past the fsynced line
  2. The replica calls fsync: the fsynced line moves past offset 4, and 42 = v2 is durable. Only then does it ack, and the Coordinator counts the ack toward W. A power cut can no longer lose this write.

    fsynced
    offsets 1–4
    Ack
    after fsync

    What changed

    • fsynced line past offset 4
    • Ack counted toward W
  3. Next, put(order:17, v2) lands at offset 5 and in the Memtable, but this replica acks before any fsync. Engines often fsync in batches to save time, so the boundary is a setting to check. The Coordinator counts the ack anyway: 2 writes acked, 1 durable.

    Offset
    5, past the line
    Acked
    before fsync

    What changed

    • 17 = v2 at offset 5
    • 17 = v2 in the Memtable
    • Acked before fsync
  4. The power fails and takes the page cache: 17 = v2, past the fsynced line, is lost though the Coordinator counted its ack. On restart the Memtable is rebuilt from the log. A process crash spares the page cache; a power cut does not.

    Survives
    offsets 1–4
    Lost
    17 = v2, acked

    What changed

    • 17 = v2 at offset 5
    • Memtable rebuilt from the log
    • 1 acked write lost
  5. When the Memtable fills, the replica writes it out as file 10, an immutable sorted file called an SSTable, and starts an empty Memtable. Random writes become sequential ones: a log append, then one sorted file. Cassandra, RocksDB and HBase work this way.

    Sorted files
    9 → 10
    Design
    LSM tree

    What changed

    • File 10, sorted and immutable
    • Memtable empty
  6. A get(order:55) misses the Memtable, then the 10 files, newest first. Each file’s Bloom filter never wrongly says absent; a maybe needs a real lookup. All 10 say absent, so no file opens; at 1% false positives, such lookups open about 0.1 files on average.

    On average
    about 0.1 files, not 10
    Cost
    about 10 bits a key
    Per billion keys
    1.25 GB

    What changed

    • A Bloom filter per file
    • order:55 ruled out
    • No file opened
  7. Compaction merges the Sorted files in the background and clears versions that newer ones replaced, at a cost in disk I/O. If it falls behind, reads touch more files. A read-heavy workload with few updates may do better on a B-tree engine such as InnoDB.

    Costs
    disk I/O
    Watch
    compaction, disk headroom
    Few updates
    a B-tree, such as InnoDB

    What changed

    • Files merged
    • Replaced versions

Space plays · ← → step · Home resets

THE IDEA

How it works

A log-structured replica commonly appends a write to a durable log and updates an in-memory table, the memtable. It later flushes that table to disk as an immutable sorted file, an SSTable. This design, the log-structured merge tree or LSM tree, is used by Cassandra, RocksDB, and HBase because it turns random writes into sequential ones. Acknowledging before the required log durability boundary risks losing a write on restart, even if the coordinator counted it. A process crash spares data in the operating system’s page cache; a power cut does not, unless the log was flushed with fsync. Engines often fsync in batches to save time, so treat the boundary as a setting to check.

A read checks memory and then disk tables, newest first. A Bloom filter is a compact bit array kept per table. It can say a table definitely lacks a key or might contain it; it never wrongly says absent. As an example, with ten tables and a 1% false-positive rate, a lookup for a key in none of them opens about 0.1 tables on average instead of ten. That rate takes about 10 bits per key, or 1.25 GB for a billion keys. A “maybe” still needs a real lookup, which may find nothing.

Compaction merges files and clears obsolete versions at an I/O cost; if it falls behind, reads touch more files. A read-heavy workload with few updates may do better on a B-tree engine such as InnoDB, which updates pages in place. Watch compaction pressure and disk headroom alongside API latency.

DECISION POINT

A node acknowledges when a value reaches memory but before its log is durable. Power fails immediately. What happens?

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

Keep in mind

  • Replica acknowledgments need an explicit durability boundary.
  • A Bloom filter can prove a key is absent from a table, never that it is present.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 2
ApplyQUESTION 01

A replica promises that any value it acknowledges survives its own power cut. What is the earliest point it may acknowledge?

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

Next lesson

0 of 2 checkpoints answered correctly to complete this lesson.