Part 5 of 10
B-trees
The B-tree is the standard structure for reading and writing records by key, and nearly every relational database relies on it. Like SSTables, it keeps keys sorted, so single-key lookups and range queries are both efficient. Beyond that, the design is very different.
| LSM-tree | B-tree | |
|---|---|---|
| Unit of storage | Segment files of several MB, any size | Fixed-size pages, also called blocks |
| Changing data | Write new files; never edit old ones | Overwrite a page in place |
Pages were traditionally 4 KiB, but PostgreSQL uses 8 KiB and MySQL 16 KiB by default.
Pages and lookups
- Each page has a number, and a page can refer to another page by its number, like a pointer that lives on disk.
- Page number times page size gives the page's byte position in the file.
- Every lookup starts at one page called the root.
- A page holds a few keys and the numbers of its child pages.
- Each child is responsible for one unbroken range of keys, and the keys stored between the child numbers mark where one range ends and the next begins.
- A lookup follows the child whose range holds the key, level by level, until it reaches a leaf page.
- A leaf page holds the values themselves, or references to where the values are stored.
The number of child references per page is called the branching factor, and it is usually several hundred.
Updates, inserts, and splits
Guess first
A leaf page is already full, and a new key belongs in it. What does the B-tree do?
Finding and splitting pages
Step 1 of 5
Each box is a page. The root's keys divide the key space: under 20 goes left, 20 to 39 goes to the middle, and 40 and up goes right. Here a page holds at most 3 keys.
- Update: find the leaf page holding the key and overwrite that page with the new value.
- Insert: find the page whose range covers the new key and add the key there.
- Split: if that page is full, split it into two half-full pages and add a new boundary key to the parent page.
- Growing taller: a split can make the parent full too, so splits can ripple upward, and if the root splits, a new root is created on top.
The tree always stays balanced, and its depth grows as O(log n). Most databases need only 3 or 4 levels. For example, 4 levels of 4 KiB pages with 500 children per page can hold about 250 TB.
Making B-trees reliable
A basic B-tree write overwrites a page in the same spot, so every reference to that page stays valid. That is the opposite of an LSM-tree, which only adds files and deletes whole old files. Overwriting is risky:
- A split changes several pages at once, and a crash halfway can leave a broken tree, such as a page that no parent points to.
- A torn page is a page that the hardware only partly wrote when the power went out.
The fix is a write-ahead log (WAL): an append-only file where every change is recorded before any tree page is touched. After a crash, the database replays the WAL to bring the tree back to a consistent state. For speed, changed pages are kept in memory and written to disk later, and the WAL still protects them. A write is durable once it is in the WAL and the WAL has been flushed to disk with fsync.
B-tree variants
- Copy-on-write: instead of overwriting a page and keeping a WAL, write the changed page to a new place, then write new copies of its parents that point to it.
- Copy-on-write also helps with consistent snapshots for transactions, which come later in the course.
- Shortened keys: pages inside the tree only need enough of each key to act as a boundary, so more keys fit per page.
- That raises the branching factor and lowers the number of levels.
- Sibling pointers: each leaf page links to its left and right neighbors, so a range scan can walk across leaves without climbing back up to the parents.