Home›Learn›B-tree and LSM tree
Course · Inside one serverLesson 8 of 330 read

B-tree and LSM tree, side by side

Why are some databases fast at writes and others at reads?

Two animated write paths and a comparison table.

Press Play, or step with the arrows.

Under every database is a storage engine, and almost all of them are one of two designs. A B-tree keeps data sorted in pages and updates them where they are. A log-structured merge tree (LSM) writes everything new into memory and sorted files, and merges them later.

When each one wins

Go deeper
B-treeLSM tree
Write pathFind the page, change it, write it back laterAppend to memory, flush whole files
Random keysScattered page reads and splits (Notebook 10: 10 times slower in MySQL)No difference at write time
Point readsOne path, a few pagesMemtable, then bloom filters, then possibly several files
Range scansLeaves are in key orderMerged across files on the fly
DeletesRemove the entry; space reused laterA tombstone, removed only by compaction
Background workCheckpoints, VACUUM or purgeCompaction, which can use a lot of I/O
Used byPostgreSQL, InnoDB, WiredTiger, SQL Server, OracleCassandra, ScyllaDB, RocksDB, HBase

Neither is better in general. A B-tree pays at write time to keep reads simple. An LSM tree makes writes cheap and pays later, in compaction and in reads that may look in several places. If writes dominate and keys arrive in any order, LSM fits. If reads dominate and you need ranges and joins, a B-tree fits.

See the difference yourself

Go deeper
-- PostgreSQL: how full are the B-tree's leaf pages?
CREATE EXTENSION IF NOT EXISTS pgstattuple;
SELECT leaf_pages, avg_leaf_density FROM pgstatindex('orders_pkey');
# Cassandra: how many SSTables does a read touch?
nodetool tablehistograms shop orders
Connected

Where this shows up

The same mechanism, indexes and plans, old row versions, vacuum and long transactions, elsewhere on the site: in real incidents, in measurements, and in designs.

Check yourself · 3 questions

Check yourself

Pick an answer. You will see whether it is right, and why.

1. Where does an LSM tree put a new write first, after the commit log?

2. What does an LSM tree do with a delete?

3. Which engine family suffers most from random primary keys at insert time?

One database decision a weekA short email each week: one real decision, the trade-off, and the command to check it. Unsubscribe in one click.

Part 1 of 4 · Learn · 56 pagesAll pages

How do databases actually work? From one row on disk to transactions: the words you need, PostgreSQL, MySQL and MongoDB from the files up, then Redis, Cassandra and vector search, side by side, and the query and concurrency traps.

Start 1: the basicsStart 2: ten conceptsStart 3: how a query travelsStart 4: napkin mathThe courseShort videosPostgreSQL 1: why PostgreSQLPostgreSQL 2: how it stores your dataPostgreSQL 3: what happens on every writePostgreSQL 4: high availability with PatroniPostgreSQL 5: running it as a DBREPostgreSQL 6: when it breaksPostgreSQL on Kubernetes: CloudNativePGMySQL 1: why MySQLMySQL 2: how it stores your dataMySQL 3: what happens on every writeMySQL 4: high availability with InnoDB ClusterMySQL 5: running it as a DBREMySQL 6: when it breaksMySQL failover with OrchestratorMySQL on Kubernetes: the Percona operatorsMongoDB 1: why MongoDBMongoDB 2: how it stores your dataMongoDB 3: what happens on every writeMongoDB 4: high availability with replica setsMongoDB 5: running it as a DBREMongoDB 6: when it breaksMongoDB on Kubernetes: the Percona operatorB-tree and LSM treeRedis 1: why RedisRedis 2: how it stores your dataRedis 3: high availabilityRedis 4: running it as a DBRERedis 5: when it breaksCassandra 1: why CassandraCassandra 2: how it stores your dataCassandra 3: high availabilityCassandra 4: running it as a DBRECassandra 5: when it breaksVector databases 1: why vector searchVector databases 2: how it stores your dataVector databases 3: high availabilityVector databases 4: running it as a DBREVector databases 5: when it breaksDatabase familiesEngines side by sideQueries that look fine and are notLost updates and write skewMany servers 1: keep a copy runningMany servers 2: who is the leaderMany servers 3: CAP as settingsMany servers 4: unique IDsMany servers 5: split the dataMany servers 6: across servicesMany servers 7: a thousand databasesStaff path, zero to architect