Full answer
Write-ahead log (WAL)
Changing data pages directly is risky: a crash halfway through writing a page leaves it corrupt. Instead, the engine first appends a record ("set row 42 balance to 900") to a log and forces it to disk (fsync). Only then is the change acknowledged. Pages are updated later, in memory, and written lazily. After a crash, the engine replays log records after the last checkpoint. Sequential appends are fast even on spinning disks, and group commit (one fsync for many transactions) raises throughput.
Both B-tree and LSM engines use a WAL. It also feeds replication (PostgreSQL streaming replication ships WAL records) and change data capture.
B-trees
Data lives in pages (often 8 to 16 KB) arranged as a balanced tree. A lookup reads about 3 to 4 pages for billions of keys, and the upper levels are usually cached. An update finds the leaf page and changes it in place; a full page splits.
- Reads: predictable, one path down the tree; excellent range scans.
- Writes: each small update may rewrite a whole page (write amplification), and random updates mean random I/O.
- Used by PostgreSQL, MySQL InnoDB, SQL Server, SQLite.
LSM trees
- Writes go to the WAL and to an in-memory sorted table (memtable).
- When the memtable is full, it is written to disk as an immutable sorted file (SSTable), sequentially.
- Background compaction merges SSTables, dropping overwritten values and tombstones.
- Reads check the memtable, then SSTables from newest to oldest; Bloom filters skip files that cannot contain the key, and a sparse index finds the block.
- Writes: sequential and batched, so very high throughput.
- Reads: may touch several files; range scans merge several sorted runs.
- Space and I/O: compaction rewrites data repeatedly; leveled compaction lowers read and space cost, tiered compaction lowers write cost.
- Used by RocksDB, Cassandra, ScyllaDB, LevelDB, and many time-series stores.
Comparing
| Aspect | B-tree | LSM tree |
|---|---|---|
| Write throughput | Moderate, random I/O | High, sequential I/O |
| Point reads | Fast and predictable | Good with Bloom filters, more variable |
| Range scans | Excellent | Good, merges runs |
| Space | Some fragmentation | Temporary duplicates until compaction |
| Background work | Little | Compaction can spike latency |
The three amplifications summarise it: B-trees have lower read amplification, LSM trees lower write amplification, and compaction strategy moves LSM costs between write, read and space amplification.
By level
Fresher: Explain that the database writes changes to a log first so it can recover after a crash. A fresher should describe a B-tree lookup as a few page reads.
Mid: Describe LSM writes, flushes, compaction and reads with Bloom filters, and compare with B-tree updates. Name example systems.
Senior: Discuss read, write and space amplification, compaction strategies, group commit, latency spikes and workload-based choice. Expect careful reasoning about crash recovery.
Key points
- WAL: append and fsync before acknowledging; replay after a crash
- B-trees update pages in place; fast reads and range scans
- LSM trees buffer in memory, flush sorted files, compact later
- Bloom filters cut LSM read cost
- Read, write and space amplification frame the trade-off
Common mistakes
- Thinking LSM trees do not need a WAL
- Claiming B-trees are always slower for writes regardless of workload
- Ignoring compaction cost and latency spikes
- Forgetting that deletes in LSM trees are tombstones
Follow-ups
- How does leveled compaction differ from tiered compaction?
- Why does PostgreSQL need VACUUM?
- How does group commit improve throughput?
Tips
- Explain the crash scenario that motivates the WAL
- Walk through an LSM write and read
- Use amplification terms in the comparison
Evaluation rubric
- Correct WAL purpose and recovery
- Accurate B-tree and LSM mechanics
- Clear trade-off comparison
- Knows real systems and compaction strategies