Write-ahead logs, B-trees and LSM-trees
How databases make writes durable and fast: the log first, then two ways of organising data on disk and the trade-offs between them.
On the roadmap:Advanced · step 3: Storage engines
Lesson 2 of 10 · 13 min read · quiz · exercise
Durability needs a log
A database holds data in memory pages for speed, but memory is lost on a crash. To be durable it must put a record of every change on disk before it tells the client "committed". That record is the write-ahead log (WAL): an append-only file of changes.
Figure SDA-ADS-02-1 · Write-ahead log flow · © forcsguy.com
The same idea appears everywhere: databases, file systems (journaling), message brokers (the log is the data) and replication (followers replay the primary's log).
Two ways to organise data on disk
B-trees: update in place
A B-tree keeps sorted keys in fixed-size pages arranged as a shallow tree. Finding a key takes a few page reads; changing a key updates a page in place (plus a WAL record for safety).
- Strengths: predictable reads, efficient range scans, mature, used by most relational databases.
- Costs: writes touch random pages (random I/O); page splits and fragmentation; every write is written twice (WAL and page).
LSM-trees: append and merge
A log-structured merge tree buffers writes in a sorted in-memory table (the memtable), also logged to a WAL. When full, it is flushed as an immutable sorted file (an SSTable). Background compaction merges files and discards overwritten and deleted values.
Figure SDA-ADS-02-2 · LSM-tree write and read path · © forcsguy.com
- Strengths: very fast sequential writes, good compression, high write throughput.
- Costs: a read may check several files (helped by bloom filters and caches); background compaction creates write amplification and I/O spikes; deletes leave tombstones until compaction.
Choosing
| Question | B-tree | LSM-tree |
|---|---|---|
| Write-heavy ingestion | Slower | Faster |
| Point reads and range scans | Steady | Good, depends on compaction |
| Predictable latency | Easier | Compaction can add tail latency |
| Space usage | Some fragmentation | Temporary extra during compaction |
| Typical users | PostgreSQL, MySQL (InnoDB) | Cassandra, RocksDB, HBase, many time-series stores |
There is no winner: choose by workload, and measure. This lesson explains why a store behaves the way it does under load, which helps when diagnosing slow writes or sudden I/O spikes.
Related ideas
- Checkpoints and snapshots bound recovery time.
- Replication often ships the WAL to replicas.
- Fsync policy (flush every write, or every few milliseconds) is a durability versus speed knob.
- Bloom filters appear in the probabilistic-structures lesson of the core-concepts course.
Real-world reading
Related reading
- The Log-Structured Merge-Tree (paper)O'Neil et al. · UMass BostonPapers
- Amazon Aurora: Design Considerations for High Throughput Cloud-Native Relational Databases (paper)Verbitski et al. · Amazon Web ServicesPapers
- RocksDBMeta · GitHubGitHub
- ClickHouse BlogClickHouse engineers · ClickHouseBlogs
Show 6 moreShow fewer
- The Google File System (paper)Ghemawat, Gobioff and Leung · GooglePapers
- Inside the Magic PocketDropboxCase Studies
- Database InternalsAlex Petrov · O'ReillyBooks
- How SQLite WorksSQLite team · SQLiteDocumentation
- Bigtable: A Distributed Storage System for Structured Data (paper)Chang et al. · GooglePapers
- Tumblr Engineering BlogTumblr engineers · TumblrBlogs
Check your understanding
Question 1 of 3
0 correct so far
What is the purpose of a write-ahead log (WAL)?
Go deeper
- Designing Data-Intensive Applications, chapter 3 by Martin Kleppmann: storage engines in depth
Topics covered in this lesson
Practise it: Sequential writes win
Everything below is already set up for this lesson: change the design, run the traffic, and see whether you meet the goal.
- 1The task
- 2Build and test
A burst rising to 1,600 requests per second, mostly writes, hits an engine that must apply each one durably.
Goal: Under 1% errors, queue clears in 30 s.
Append-only logging lets you accept writes fast and apply them at the engine's own pace.
Try it yourself
A service writes 50,000 small events per second and reads recent ones by key. Which engine would you start with, and what would you watch?
Click what a strong design here would need:
More in Distributed systems
© 2026 forcsguy.com. Original lesson text and diagrams. Books and sites linked above belong to their authors.