LESSON 01 · The log

Start with an append-only log

A file you only append to, plus an in-memory index, is a working database and the basis of crash recovery.

SYSTEM VIEW

The write path

Diagram view
Records—
Keys in memory—
Unsynced—

Follow parcel 8841 into a file that only grows, then see what the index, compaction and fsync each add.

  1. The App writes parcel 8841 as packed. The record goes on the end of the log file at offset 0, a sequential write, the cheapest kind on most storage. The hash index notes that 8841 lives at offset 0, so a read needn’t scan the file.

    Record
    8841 packed
    Offset
    0

    What changed

    • 8841 packed at offset 0
    • Index: 8841 → 0
  2. Parcel 8841 ships. Nothing in the file is edited: the update is a newer record at offset 4096, and 8841 packed stays where it was, now stale. The index moves 8841 to 4096, its newest version.

    Offset
    4096
    Index
    8841 → 4096

    What changed

    • 8841 shipped at offset 4096
    • Index: 8841 → 4096
    • 8841 packed is stale
  3. To read 8841, the App asks the hash index, which answers 4096 from memory, and one disk read at that offset returns 8841 shipped. Riak’s Bitcask works this way. A hash map has no order, though, so a range of parcels means scanning the file.

    Point read
    1 lookup, 1 disk read
    Range read
    scan the file

    What changed

    • Lookup: 8841 → 4096
    • 1 disk read at offset 4096
  4. The hash index holds every key in memory, so all the keys must fit, and a restart empties it. The App rebuilds the map by reading the whole file from the start: 8841 maps to 0, then to 4096, as the later record wins.

    In memory
    every key
    After a restart
    read the whole file

    What changed

    • Map emptied by a restart
    • Rebuilt by reading the file
    • 8841 → 4096 again
  5. Every update leaves its old version behind, so the file only grows, and the rebuild read 8841 packed for nothing. A background job rewrites the log keeping each key’s newest record: 8841 packed goes, one record per key remains.

    Keeps
    newest per key
    Drops
    8841 packed

    What changed

    • 8841 packed
    • 1 record per key
  6. The App writes parcels 1203 and 5170, and write() returns for each. They may only have reached the operating system’s cache, past the synced line; only fsync makes them safe. Larger engines fsync a write-ahead log the same way before changing their main files, so recovery can replay it.

    Synced
    8841 shipped
    Not synced
    1203, 5170

    What changed

    • 1203 new, 5170 new
    • Both past the synced line
    • Index: 3 keys
  7. Try both. A power cut empties the map and loses the tail past the line, 1203 and 5170; 8841 shipped survives, since earlier records are never rewritten. With fsync first, one sync makes both durable before either is confirmed: engines often group commits, as a sync per commit is slow.

    Safe
    what is synced
    fsync calls
    1 for 2 commits

    What changed

    • Power cut: the unsynced tail
    • fsync: both durable at once
    First comes

Space plays · ← → step · Home resets

THE IDEA

How it works

A database has two jobs: keep what you give it and find it again. The smallest design that does both is a file you only append to. Each write adds a key and value to the end, and an update is a newer record for the same key. Sequential writes are the cheapest pattern on most storage, and a crash can damage only the tail, because earlier records are never rewritten.

Finding data is the weak spot, since a read would otherwise scan the whole file. So keep an index, a structure that maps a key to where its data lives. An in-memory hash map from key to byte offset is enough. Say parcel 8841 was written as “packed” at offset 0 and “shipped” at offset 4096. The map holds 8841 at 4096, so a read costs one hash lookup and one disk read. Riak’s Bitcask engine works this way. The costs: every key must fit in memory, range queries are out because a hash map has no order, old versions pile up until a job rewrites the file, and the map must be rebuilt after a restart.

The same idea protects larger engines. A write-ahead log (WAL) makes each change durable before the engine’s main on-disk files are altered, so recovery can replay whatever had not reached them. A returned write call may only have reached the operating system’s cache; the data is safe only after a sync call such as fsync. Syncing on every commit is slow, so engines often group commits into one sync.

DECISION POINT

A team needs to store 200 million device records. With overhead, each index entry takes about 40 bytes (example figures), and the server has 16 GB of memory. They also want a list of devices seen in the last hour. Would you approve an append-only log with an in-memory hash index?

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

Keep in mind

  • Appending is fast and crash-friendly; finding data later is what needs an index.
  • A write is durable once the log is synced to the device, not when the write call returns.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
DiagnoseQUESTION 01

A service gets success back from write(), then acknowledges the client. Power fails a moment later and the record is gone. What was missing?

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.