B-tree
A balanced search tree whose nodes are whole disk pages holding hundreds of sorted keys each. It exists because disks and SSDs are read in pages, so a tree three or four levels deep can find any one of a billion keys in three or four page reads.
What it costs
| Operation | Cost | Note |
|---|---|---|
| Point lookup | O(log_B n) ≈ 3–4 page reads | Usually 1 real disk read: the upper levels stay in the buffer pool. |
| Range scan | O(log_B n + k) | Find the start, then walk sibling leaves (B+tree leaves are linked). |
| Insert / update | O(log_B n) | Modify the leaf page in place; split it when full, which can cascade upward. |
| Delete | O(log_B n) | Remove from the leaf; merge with a sibling when it gets too empty. |
Memory: Keys plus pointers, roughly 1.5× the raw key size because pages are kept 50–90% full. The top two levels of a billion-row index fit in a few MB of RAM.
Three levels, one page per node, linked leaves
A lookup descends root → internal → leaf, binary-searching each page. Leaves are linked so a range scan walks sideways. A full leaf splits and pushes its middle key up.
How it works
- Every node is one page (8 KB in Postgres, 16 KB in InnoDB). An internal node holds sorted separator keys and child pointers; a leaf holds sorted keys with the row location or the row itself.
- A lookup starts at the root, binary-searches the separators to pick a child, and repeats until it reaches a leaf. With 300 keys per page, 4 levels cover 300^4 = 8 billion keys.
- An insert finds the leaf the same way and writes the key into it. If the leaf is full it splits in two and the middle key is pushed into the parent; if the parent is full it splits too, and so on up. Only a split at the root makes the tree taller.
- In a B+tree (what databases actually use) all values live in leaves and leaves are linked left to right, so a range scan finds the first key and then follows leaf pointers without going back up.
- Each modified page is written to the write-ahead log first, then the page itself is written back later. Random keys touch random leaves, which is why B-tree writes are random I/O.
Why the fan-out matters
- Height grows with the logarithm base B (keys per page). Doubling the data adds a level only every few hundred times, so a tree is effectively 3–4 levels for anything you will index.
- The root and second level are read on every lookup, so the buffer pool keeps them in memory; most lookups cost one leaf read.
Worked example
The alias index in the URL shortener
- 6 billion aliases of 7 bytes, each with a 6-byte row pointer: about 13 bytes per entry. An 8 KB leaf page at 70% full holds about 440 entries, so leaves alone are 6B / 440 = 13.6M pages = 110 GB.
- Internal nodes hold 7-byte separators plus 4-byte child pointers: about 500 per page. Level above the leaves: 13.6M / 500 = 27K pages. Above that: 55 pages. Above that: the root. Four levels.
- A redirect lookup for alias aB3xZ9: read root (cached), read a level-2 page (cached), read one of the 27K level-3 pages (likely cached, 220 MB total), read one leaf (a real disk read on a miss). One SSD read of about 100 µs.
- Inserting 40 new aliases per second with random base62 keys means 40 random leaf writes per second plus WAL. Trivial. The same insert rate with 40,000 per second (a chat message store) would be 40K random page writes per second, which is where an LSM tree starts to win.
When it is the wrong choice
- Write rates in the tens of thousands per second with random keys: every insert is a random page write, and SSD write amplification climbs. An LSM tree turns those into sequential flushes.
- Keys that only ever append (time-ordered ids) are fine; keys that are random (UUIDv4) scatter inserts across every leaf and keep the whole index hot. Use time-ordered ids.
- Pure key-value lookups with no range queries: a hash index is one read instead of three or four, if the engine offers one.
Interviewer follow-ups
Answer out loud before you open each one.
Why is a B-tree so shallow compared with a binary tree?
Each node holds hundreds of keys, not one, so each level divides the search space by hundreds. A binary tree over a billion keys is 30 levels; a B-tree with 300 keys per page is 4. Fewer levels means fewer page reads, which is the cost that matters on disk.
What happens on an insert when the leaf page is full?
The leaf splits into two half-full pages and the middle key is inserted into the parent as a new separator. If the parent is also full it splits the same way. Splits are why pages sit at 50–90% full and why inserting random keys into a huge index is more expensive than appending to the end.
Why does the choice of primary key affect B-tree performance?
Sequential keys (auto-increment, Snowflake, UUIDv7) always insert at the rightmost leaf, so only one page is hot and splits are cheap. Random keys (UUIDv4) land on a random leaf every time, touching the whole index and causing splits everywhere. That is the main reason time-ordered ids are preferred.
How does a covering index save a page read?
Normally the leaf holds the key and a pointer, so after finding the key the database reads the table page too. If the index also stores the columns the query needs, the answer comes straight from the leaf and the table read is skipped.
Where it shows up
- Building blocks
- Index the columns you filter on · Index selective columns first · Read the query plan · Default to a relational database · Write-optimised vs read-optimised storage · Page results with a cursor · Know your write amplification
- Problems
- URL Shortener (TinyURL / bit.ly) · Ride Matching (Uber / Lyft) · Notification System