Storage engines: B+ tree vs LSM tree

Write and read the same keys in two engines. A B+ tree changes pages in place. An LSM tree adds writes to memory, saves sorted files, and merges them later.

Step through the same work in each engine

Both engines start with the same six keys, user:01 to user:50. Pick an engine and the work, then press Next step. Back takes you to the step before.

The engine
The work

With these settings: The B+ tree changed 2 pages in place, at 2 places on disk. Each is saved whole later: 16 KB for a few bytes of record.

App, writes and readsAppwrites and readsLog on disk, grows at its endLog on diskgrows at its endRoot page, user:20 splits it, NOWRoot pageuser:20 splits ituser:15 → leftNOWLeft leaf page, keys before user:20Left leaf pagekeys before user:20Right leaf page, user:20 and afterRight leaf pageuser:20 and after
  • App — writes and reads — healthy
  • Log on disk — grows at its end — healthy
  • Root page — user:20 splits it — healthy — NOW — user:15 → left
  • Left leaf page — keys before user:20 — healthy
  • Right leaf page — user:20 and after — healthy
  • Write
Step 1 of 7

Step 1 of 7 · Root page

Done: user:15 starts at the root page

What happens
The root says keys before user:20 go left, so user:15 goes left.
Why
A B+ tree keeps each key in one fixed place, and the root tells the write where.
What changes
The write goes to the left leaf page.
Next
The left leaf page changes in memory

Event log

6 more to go. Press a line to go back to that step.

Pages changed in memory
0
Log entries
0
Page writes to disk
0
not yet

Step 1 of 7: Root page. user:15 starts at the root page. The root says keys before user:20 go left, so user:15 goes left.

What this simplifies. Real trees have thousands of pages and engines have many files per level, but the steps are the same. Page size differs: 16 KB in MySQL's InnoDB, 8 KB in PostgreSQL. Both kinds of engine keep a log on disk.

Try it yourself

The same six keys in both engines. Write, flush, compact and read in any order.

LSM tree: writes
New files, never edited
New data reaches disk as whole sorted files, written in one go. This suits heavy write traffic.
B+ tree: writes
Pages changed in place
Each write changes the page where its key lives, and those pages sit at scattered places on disk.
Reading one key
B+ tree is usually quicker
It walks one path from the root to one page. An LSM tree may check memory and several files.

LSM tree (used by RocksDB and Cassandra)

Built for writes
1. Memtable: the newest writes, in memory2 / 4 keys
user:10: "Alice"
user:25: "Bob"
2. Level 0: saved files (SSTables) on disk, not merged yet1 files
SST-L0-1: user:05, user:30
3. Level 1: merged files on disk, one version of each key
SST-L1-1: user:01, user:50

B+ tree (used by PostgreSQL and MySQL)

Built for reads
Root page: keys before the split key go left, the rest go right
user:20
Left leaf page: keys before user:20
user:01user:05user:10
Right leaf page: user:20 and after
user:25user:30user:50
A leaf page holds the records themselves. A page has a fixed size: 16 KB in MySQL's InnoDB, 8 KB in PostgreSQL. A write changes its page in memory, and the database writes the whole page back to disk later.

Write a key

Read one key

Log of what happened

[5:35:56 AM]Both engines start with the same six keys. Write or read a key to compare them.