Try
↑ ↓ move↵ openesc close
In this lesson · topic 1 of 7
  1. 1. Random writes are the expensive kind
  2. 2. Memtable, SSTables and compaction
  3. 3. Run leveled and tiered on the same writes
  4. 4. What each policy costs you
  5. 5. Why reads cost more here
  6. 6. When compaction cannot keep up
  7. 7. Memtables, runs and amplification

Topic 1

Random writes are the expensive kind

3 min read

The cost of writing in place

A B-tree changes pages where they live. Each insert can dirty a different page, so the disk sees a stream of small writes in random places. Sequential writes are far cheaper on spinning disks and still help on flash, where they wear the device less and keep its internal housekeeping simple.

A log-structured design takes the opposite view. Never change a file. Put new writes in memory, and when memory fills, write it out in one sequential pass as a sorted, immutable file. A write is then one entry in a memory table plus one append to a log.

What you owe afterwards

This is a log-structured merge tree, an LSM tree. It has two debts.

The first is reads. The newest value of a key may be in memory, or in any of the files, so a read may check several places.

The second is the files themselves. The same key can sit in many files, the oldest copies are stale, and deleted keys still occupy space. A background job called compaction merges files to fix this, and every merge rewrites data that was already on disk.

In the lab, 40,000 writes make 198measured flushes of a 200measured-entry memtable. Compaction then rewrites entries so that, with leveled compaction, the disk sees 7.7xmeasured as many entry writes as the application made.

Predict first

An LSM tree writes new data in one sequential pass and never edits a file. Does it write fewer bytes to disk in total than a B-tree?

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms