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.
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 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 · 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.
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.