Write-optimised vs read-optimised storage
Choosing an engine for a heavy write load vs a heavy read load.
Basic solution
LSM trees batch writes in memory and flush sorted runs (fast writes, slower reads, compaction). B-trees update in place (fast point reads, more random writes).
In-place pages versus append, flush and compact
The B-tree modifies the page that holds the key, so writes are random and reads are one descent. The LSM appends to a memtable, flushes sorted files, and compacts them in the background, so writes are sequential and reads may check several files.
How it works
- B-tree write: find the leaf page that holds the key's position, record the change in the write-ahead log, modify the page in memory and mark it dirty. The page is written to disk later. Because keys arrive in random order, each write dirties a different page somewhere in the file, so writes are random I/O.
- B-tree read: descend from the root page through the internal pages to the leaf, three or four page reads. The upper levels are almost always cached, so a point read usually costs one disk read, and a range read walks sideways along the linked leaves.
- LSM write: append the change to the write-ahead log, then insert it into the memtable, an in-memory sorted table. When the memtable reaches its size limit, it is written to disk in one sequential pass as an immutable sorted file (an SSTable) at level 0. Old data on disk is never modified.
- LSM read: check the memtable, then level 0's SSTables from newest to oldest, then each deeper level. Before opening a file, consult its Bloom filter, a small bit array that says whether the key might be in the file. A point read may still touch several files when the key is old.
- Compaction runs in the background: it reads overlapping SSTables, merges them into larger sorted files in the next level, drops versions of a key that were overwritten and keys marked deleted, and deletes the input files. Each byte is rewritten several times as it moves down the levels.
- The trade: LSM pays write amplification in compaction and read amplification in file lookups to get sequential, batched writes; B-trees pay random writes to get one-place reads.
Rule of thumb
- Write-heavy, append-like, SSD or spinning disk, large datasets: LSM (RocksDB, Cassandra, HBase).
- Read-heavy point and range lookups with in-place updates and transactions: B-tree (InnoDB, Postgres).
Worked example
Chat message store, 70K writes/s at peak
- Each message 200 B. B-tree: each insert dirties a leaf page (8 KB) somewhere in a 20 TB index. At 70K/s that is 70K random 8 KB page writes per second, which exceeds what a few NVMe drives sustain once the working set outgrows the buffer pool, and the WAL plus page writes mean roughly 2× write amplification before counting secondary indexes.
- LSM: 70K × 200 B = 14 MB/s of sequential appends to the memtable and WAL; flushes write sorted 64 MB files. Compaction with a levelled strategy rewrites each byte about 10× over its life, so total disk write is ~140 MB/s sequential, which one NVMe handles with room to spare.
- Reads: "last 50 messages in conversation 42" is a range on a key that was written recently, so it lives in the memtable or level 0 and 1: one or two file reads. Old history reads walk more levels but are rare.
- Space: levelled compaction keeps about 10% extra space; size-tiered can need 2× during a merge. Budget 1.3× for levelled, so 20 TB of messages needs about 26 TB of disk.
- Where a B-tree wins instead: the ride platform's trip table, 15K updates/s to existing rows (state changes) and point reads of the current state. Updates in place and a single-seek read beat merging across levels, because every read wants the latest version of one row.
Variants and when each wins
Levelled compaction (RocksDB default, Cassandra LCS)
- Each level holds 10× the data of the one above, and files within a level never overlap in key range, so a read checks at most one file per level. It wins for read-sensitive or space-sensitive workloads, at the cost of about 10× write amplification.
Size-tiered compaction (Cassandra STCS, HBase)
- Files of similar size are merged once enough of them accumulate, so a key may live in many overlapping files until the merge. It wins for write-heavy, append-mostly tables, with about 4× write amplification, but reads may hit many files and space can double during a merge.
B-tree with a large buffer pool (InnoDB, Postgres)
- Pages are updated in place and the hot ones stay in memory. It wins when the working set fits memory and updates to existing rows are common, because reads are predictable and transactions and secondary indexes are natural.
Fractal or Bε-trees (TokuDB, some newer engines)
- A B-tree where each node has a buffer that collects updates and pushes them down in batches. It wins as a middle ground on write-heavy B-tree workloads, but is less common in practice.
Pick levelled LSM for large read-and-write stores, size-tiered for append-heavy logs and time series, a B-tree for transactional tables with updates and a working set that fits in RAM.
Failure modes
Write stalls from compaction debt
- You notice writes pausing for seconds and the level 0 file count climbing. It happens because data is arriving faster than compaction can merge it down, and the engine stalls writes to stop level 0 growing without bound. Add compaction threads and disk bandwidth, use bigger memtables so flushes are rarer, switch the hottest table to size-tiered compaction, or throttle bulk loads.
Read amplification on a cold key
- You notice a point read of old data taking many disk reads. It happens because the key sits in a deep level, and Bloom filters only let the read skip files, not levels, so it still checks one file per level on the way down. Enlarge the block cache, use partitioned filters, reduce the number of levels with a larger size ratio, or put a cache in front for the hot set.
Space blow-up during merges
- You notice disk filling to twice the data size whenever compaction runs. It happens because size-tiered merges keep both the old input files and the new output file until the merge completes. Switch to levelled compaction, or leave 50% headroom on the disk.
B-tree page splits and fragmentation
- You notice inserts with random keys (UUIDv4) getting slower and the index growing larger than the data. It happens because every insert lands on a random leaf, filling pages unevenly and splitting them, which leaves half-empty pages everywhere. Use time-ordered keys (UUIDv7, Snowflake) so inserts append to the right edge of the tree.
Tombstones that never go away
- You notice a range read over data that was deleted days ago still being slow. It happens because LSM deletes are markers (tombstones) that must be read and skipped until compaction carries them to the deepest level and drops them. Set TTLs, delete whole partitions instead of rows where possible, and tune compaction so tombstones are purged sooner.
Interviewer follow-ups
Answer out loud before you open each one.
What is write amplification and how do the two engines compare?
Bytes actually written to disk per logical byte written. A B-tree writes the WAL plus a full page per changed row, so small updates on big pages amplify, plus index pages. An LSM writes the WAL, the flush, then rewrites during each compaction: roughly 10× for levelled and 4× for size-tiered, but all sequential.
Why do Bloom filters matter so much for LSM reads?
A point read must consider every SSTable that could hold the key. A Bloom filter per file says "definitely not here" with a 1% false positive rate, so a read checks a handful of filters in memory and opens only one or two files instead of dozens.
Which engine do Postgres, MySQL, Cassandra, RocksDB and DynamoDB use?
Postgres and MySQL InnoDB are B-trees; Cassandra, HBase, LevelDB, RocksDB and most key-value stores are LSM; DynamoDB does not say but behaves like a partitioned LSM. MongoDB WiredTiger is a B-tree with an LSM option. Many SQL-on-LSM systems exist, like CockroachDB on Pebble.
How would you decide for a new service?
Estimate the write rate and the ratio of updates to inserts, then the read pattern. Inserts at tens of thousands per second with range reads of recent data says LSM; updates to existing rows with point reads and transactions says B-tree. If the working set fits memory, the B-tree is simpler and usually wins.