Skip to the guide

B-trees vs LSM trees

AdvancedConcept3 pages

Under every database sits a storage engine, and almost all of them use one of two designs. A B-tree keeps data in sorted pages and changes them in place, which makes reads predictable. An LSM tree turns every write into an append and sorts things out later, which makes writes cheap. Interviewers raise it when a design writes a lot, like metrics, logs or messages, and want to hear what each choice costs.

A post room

Picture the post room of a large office. One way to handle letters: a wall of pigeonholes, one per person, in alphabetical order. Each letter goes straight into its slot, so finding someone's post is one look. But every letter means walking to a different part of the wall, and when a slot overflows you have to shuffle the slots along.

The other way: as each letter arrives, you write one line in a day book. Then you slip the letter into a sorting tray by the door, in name order, which takes a second. When the tray is full, you tie its letters up as a bundle, already sorted and labelled with today's date. Every so often you merge several bundles into one bigger sorted bundle and throw out letters for people who've left. To find someone's post you check the tray, then the bundles from newest to oldest. A sticky note on each bundle saying who definitely has nothing inside lets you skip most of them.