System design paper · 1996

LSM-tree

O’Neil, Cheng, Gawlick and O’Neil · Acta Informatica 1996

How a log-structured merge-tree turns slow random disk writes into fast batched ones, and how LevelDB, RocksDB and Cassandra use the idea.

New entries go to a log and a small tree in memory. A cursor then sweeps them into the big tree on disk in batches. In one of the paper’s worked examples, 20 MB in memory fronts 9.2 GB on disk.

You’ll learn to

  • Explain why an index on a heavily written table costs so much with a classic design, and how batching changes the bill.
  • Walk through a rolling merge between a small in-memory tree and a large on-disk one, and say why nothing is overwritten.
  • Use the paper’s worked numbers to compare two and three trees, and say what limits adding more.
  • Compare leveled and tiered compaction by write, read and space cost, and name the systems that use each.

The idea in short

A disk is slow when it must jump to a different place for every small write, and fast when it writes a long run of data in one go. A busy database index, the lookup structure that finds rows quickly, makes the first kind of write all day. The authors’ answer is to hold new index entries in memory, write a safety copy to the log the database already keeps, and merge them into the big on-disk index in large batches.

The merge sweeps through the big index in order, so one trip to disk carries many new entries instead of one. For a workload that mostly inserts, the paper’s cost sums come out at nearly two orders of magnitude fewer random disk operations per insert. With the paper’s typical values, our own arithmetic gives about 75 times fewer. Reads get a little slower, because a lookup may have to check more than one place.

The problem

Start with a bank. Every time a customer pays, the bank updates an account table of 100 million rows at 100 bytes each, 10 GB in all, and adds a row to a history table. The paper’s example runs at 1,000 transactions a second, and the bank works eight hours a day. Now the bank wants to look up all the recent payments for one account. That needs an index: a second structure that maps an account number and time to the place where the row is stored, so the database does not have to scan the whole table.

The usual index is a . It is a tree of fixed-size blocks on disk, called pages (4K in the paper, so 4,096 bytes), where each page points to the next level down. To add an entry, the database walks down from the root, finds the right leaf page, changes it and writes it back. The writes land wherever that key belongs, which is all over the disk.

That is the expensive part. A disk has a moving arm that must travel to the right spot before it can read or write. A write that needs the arm to move is a (an I/O is one read or write to disk). The paper counts an arm at about 40 random I/Os a second, the 1995 figure; its first example uses an older 25. A write that carries on from where the arm already is, a sequential write, is far cheaper per byte.

The paper puts numbers on this with two examples.

The account table

1,000 transactions × 2 I/Os = 2,000 I/Os a second

Each transaction costs about two I/Os: a read of the page, and a write of a page it dirtied earlier. So 1,000 transactions a second need about 2,000 random I/Os a second.

Disk arms for the table

2,000 ÷ 40 = 50 arms

At 40 random I/Os per arm per second, that is 50 disk arms, bought for speed and not for space. (At the older 25 a second, the paper’s first example, it is 80.)

The history index

1,000 × 3,600 × 8 × 20 = 576,000,000 entries

The index keeps 20 days of entries, 16 bytes each: a 4-byte account number, an 8-byte time and a 4-byte row pointer. That is 9.2 GB, about 2.3 million pages.

Why caching does not help

2.3 million pages ÷ 1,000 inserts a second ≈ 2,300 s

New entries spread across all those pages, so any one page is touched about once every 2,300 seconds. It is not worth keeping in memory between visits. Each insert pays full price: read the leaf and write it back, another 2,000 random I/Os a second and about 50 more arms.

So the index costs as much in disk arms as the table does, for data that is mostly written and rarely read. The question the paper answers is: can the index be kept up to date without one random I/O per insert?

The design choices

The paper gives a name to its answer: the Log-Structured Merge-tree, or . The “log-structured” half comes from an earlier idea, the log-structured file system, where changes are written to the end of a log instead of in place. The “merge” half is what the authors add: a way to keep an ordered, searchable index while writing in big batches.

Our biggest sale ever · Black Friday

Flat 50% Off

Pro

Continue reading

Unlock every course, now and later

  • System Design, live
  • AI Engineering, live
  • Data Engineering, live
  • Machine Learning Engineering, live
  • Inference Engineering, coming soon
  • AI Research, coming soon
  • Performance Engineering, coming soon
  • Agent & Harness Engineering, coming soon
  • LLM Fine-tuning, coming soon
  • LLM Pre-training & Post-training, coming soon
  • Reinforcement Learning, coming soon
  • AI Infrastructure & Platform Engineering, coming soon
  • AI Evaluation & Safety, coming soon
  • Forward Deployed Engineering, coming soon
  • Cloud Computing, coming soon
  • DevOps, coming soon

50% off · Annual and Lifetime

BLACKFRIDAY50Applied at checkout

Choose a plan

About the paper

The Log-Structured Merge-Tree (LSM-Tree) by O’Neil, Cheng, Gawlick and O’Neil, Acta Informatica 1996.

Read the original paper

More System design papers

All papers