Why are some databases fast at writes and others at reads?
Under the hood, most databases are either a B-tree or an LSM tree. A B-tree updates data where it lives and pays for random writes; an LSM tree appends and sorts later, and pays at read time. Measured with the engines under etcd and CockroachDB: 54× vs 7× bytes written per byte stored, and reads…
The verdict, which the numbers below will prove: the culprit is mostly the storage engine.
A B-tree keeps data sorted in fixed-size pages and changes a key where it lives. Reads are a short walk down the tree, but every random write dirties a whole page somewhere on disk.
An LSM tree (log-structured merge tree) does the opposite. It appends writes to a log and an in-memory table, writes them out as sorted, immutable files, and merges those files in the background. Writes become sequential and cheap. A read may have to look in several places, and the background merging costs I/O of its own.
PostgreSQL, MySQL (InnoDB), SQL Server and etcd use B-trees. RocksDB, Cassandra, ScyllaDB, CockroachDB, TiKV, and many time-series and log stores use LSM trees.
The case usually opens with a puzzle: one database swallows writes all day without a murmur, another answers lookups in a blink, and nobody on the team can say why. The usual suspects are the network, the hardware, the query. I find it fascinating that the real answer sits deeper, in two ideas: Rudolf Bayer and Edward McCreight’s B-tree from around 1970 and the LSM tree described by Patrick O’Neil and colleagues in 1996, and both still sit underneath so much of what we run. Each makes the opposite bet about where to pay, and you can see both bets clearly in the numbers.
So I ran the same workload on bbolt (the B+tree under etcd) and Pebble (the LSM tree under CockroachDB), both embedded in one Go process. The code is in database-tradeoffs-lab (make engines).
Case file
The case One database swallows writes, another answers lookups in a blink Suspects The network, the hardware, the query Culprit The storage engine: B-tree or LSM tree The fix Pick the engine’s bet to match the workload Evidence bbolt vs Pebble, same Go process, make engines, three runs
The two suspects at a glance #
| B-tree (B+tree) | LSM tree | |
|---|---|---|
| A write | Find the page, change it in place (or copy it, in bbolt), log it | Append to a write-ahead log and an in-memory table; done |
| Later | Pages written back at checkpoints | Memory table flushed as a sorted file; files merged (“compacted”) level by level |
| A point read | One walk from root to leaf | Memory table, then each level’s files (filters can skip some) |
| A range scan | Walk along adjacent leaves | Merge sorted streams from every level |
| Hurts at | Random writes: one page dirtied per key | Reads, and compaction’s background I/O |
| Used by | PostgreSQL, InnoDB, SQL Server, SQLite, etcd (bbolt), LMDB | RocksDB, Cassandra, ScyllaDB, CockroachDB (Pebble), TiKV, InfluxDB 1.x and 2.x (TSM) |
Diagram source
flowchart TD
W["A write"] --> B["B-tree: find the page"]
B --> P["Change it where it lives: a whole page dirtied"]
W --> L["LSM: append to log and memory table"]
L --> F["Flush as a sorted, immutable file"]
F --> C["Compact level by level in the background"]
The evidence: one workload, both engines #
Let’s put both bets on the same table and watch what happens.
The workload:
- 1,000,000 keys of 16 bytes, in random order, with 100-byte random values;
- writes in batches of 1,000, each batch durable (one fsync);
- then every key overwritten once;
- then 100,000 point reads and 2,000 scans of 100 keys.
“Written to disk” comes from the kernel’s own counter of bytes the process wrote (/proc/self/io), so it includes everything: pages, logs, flushes and compactions. That counter is our most reliable witness, because nothing can hide from it.
| B+tree (bbolt) | LSM tree (Pebble) | |
|---|---|---|
| Insert 1M keys, writes/s | 48-56K | 190-240K |
| Written to disk per byte stored, insert | 45× | 5.4-5.6× |
| Written to disk per byte stored, overwrite | 54× | 7.4-7.7× |
| Point read, key exists (p50) | 1.2-1.4 µs | 14-23 µs |
| Point read, key doesn’t exist (p50) | 1.2 µs | 21-23 µs |
| Range scan of 100 keys (p50) | 2.8-5.7 µs | 48-56 µs |
| Size on disk / live data | 1.89× | 1.54-1.59× |
(Ranges across three runs; the B+tree’s write amplification was identical every time.)
Diagram source
xychart-beta
title "Bytes written per byte stored, overwrite (low end of range)"
x-axis ["B+tree (bbolt)", "LSM (Pebble)"]
y-axis "bytes written per byte" 0 --> 54
bar [54 , 7.4]
Clue one: why the B+tree wrote 54 bytes per byte #
Each batch of 1,000 random keys lands in hundreds of different leaf pages. bbolt is copy-on-write: a changed page is written as a new 4 KB page, along with its parents up to the root. So a batch that logically wrote 116 KB physically wrote megabytes. PostgreSQL and InnoDB update pages in place and log the changes instead, so the exact factor differs. But the shape is the same, for the same reason: random writes dirty whole pages. That’s why bulk-loading in key order, or batching updates to the same page, makes B-trees so much faster.
Clue two: why the LSM tree wrote 5-8 bytes per byte #
Each write goes to the log (once) and the memory table. Full memory tables become sorted files, and compaction merges files level by level, so every byte is rewritten a few times as it moves down. That’s the LSM’s write amplification: a handful of times rather than dozens, and all sequential I/O, which every device does best. Here’s the bit worth admiring: postpone the sorting, and turn scattered writes into long, tidy streams. That’s why it inserted about 4× faster here.
Clue three: why reads go the other way #
Of course, nothing is free, and here’s where the LSM pays its bill.
A B-tree point read walks a few pages, which here were all in memory-mapped cache: about a microsecond. An LSM read checks the memory table, then the files at each level, each with its own index and block lookup: 14-23 µs here.
A missing key was the LSM’s slowest case (23 µs), because it has to check every level before it can say “not here”. A range scan must merge sorted streams from every level instead of walking adjacent leaves, so it was about 10-20× slower.
A false lead: Bloom filters #
LSM engines put a Bloom filter on each file, so a read can skip files that can’t contain the key. You’d expect them to rescue the reads. In my runs, with everything in memory, the filters trimmed missing-key reads by about 10% and made existing-key reads slightly slower (an extra check). I like this result because it’s a reminder that every optimisation has a target. That’s not the filters failing: they save disk reads, and with the data in RAM there were none to save. On a dataset bigger than memory, skipping a file means skipping a random disk read, and that’s where they pay for themselves. The Bloom filter post covers how they work.
With both designs understood, choosing becomes a question about your workload rather than about fashion.
Case 1: time-series and event ingest #
Situation: metrics, logs, IoT readings or clickstream data. Huge write volume, rarely updated, mostly read as recent ranges.
What I’d use: an LSM-based or log-structured store (Cassandra/ScyllaDB, a time-series database, ClickHouse for analytics). Writes are sequential, compaction works well when data arrives roughly in time order, and old data drops off whole files at a time. If you stay on PostgreSQL, insert in time order, use BRIN indexes for time columns, and partition by time (the partitioning post measures the difference).
Case 2: OLTP (orders, accounts, users) #
Situation: a mix of reads and updates, point lookups by ID, short ranges, secondary indexes, transactions.
What I’d use: a B-tree database (PostgreSQL, MySQL). Reads dominate most OLTP workloads, and fast point reads and range scans matter more than raw write throughput. A few thousand writes per second is well within a B-tree’s comfort zone on modern SSDs, especially with group commit (the log post measures that).
Case 3: a key-value store or cache that’s mostly writes #
Situation: sessions, counters, a write-heavy store of user events, or the storage layer under a distributed database.
What I’d use: an LSM (RocksDB, Pebble, or a database built on one). Write amplification of 5-8× instead of 50× also means less SSD wear, which matters when a fleet of drives absorbs writes all day. Tune compaction for the workload: levelled compaction gives lower space use and faster reads; tiered (size-tiered) compaction gives lower write amplification and more space.
The details that trip people up #
- Compaction is background work you still pay for. It competes with your queries for disk and CPU, and if it falls behind, reads slow down (more files to check) and writes eventually stall. Monitor compaction debt the way you’d monitor replica lag.
- Deletes in an LSM are writes. A delete adds a “tombstone” that hides older versions until compaction removes them. Lots of deletes plus range scans means scanning past tombstones: the classic Cassandra queue anti-pattern.
- B-trees fragment. Pages split and leave free space; this run ended with 1.9× the live data on disk. PostgreSQL adds MVCC bloat on top (the long-transaction post).
- “Fast at writes” depends on the write pattern. Writing keys in order (timestamps, sequential IDs) is cheap for a B-tree too, because consecutive writes hit the same page.
Remember this
- A B-tree pays at write time: every random write dirties a whole page.
- An LSM tree pays at read time: a read may check every level.
- 54× vs 7× bytes written, and reads about 10× faster the other way.
What to do on Monday #
- Pick the database for the workload, not the engine for its own sake: PostgreSQL or MySQL for general OLTP; an LSM-based store when sustained write volume or SSD wear is the real constraint.
- On a B-tree database:
- insert in key order where you can;
- batch writes;
- prefer time-ordered keys over random UUIDs for heavily inserted tables (UUIDv7, or a sequence; the unique IDs post measures the difference);
- partition big time-series tables.
- On an LSM store:
- design range scans around the partition key;
- avoid delete-heavy queue patterns;
- turn on Bloom filters for datasets bigger than memory;
- watch compaction.
- Measure write amplification on your own data if SSD wear or disk bandwidth matters. The kernel’s per-process I/O counters make it easy.
So the case closes without a villain. These two structures split the problem cleanly. Neither is better; each knows exactly what it gave up, and once you know too, picking between them stops being a guess.
Measured Nov 2026 with Go 1.27, bbolt 1.5.0 and Pebble 2.1.7, embedded, on a laptop NVMe SSD inside Docker; three runs. Everything fit in the page cache, so read numbers are CPU and data-structure cost, not disk seeks. Code and full results: database-tradeoffs-lab/12-storage-engines.
Comments
No comments yet.
Sign in to comment. We'll email you a link; no password needed.