TwiceToldby Develoker
ConceptsSystem designLabsCheat sheetsAbout

Develoker makes free tools for learning to build software. TwiceTold explains each idea twice, as an everyday analogy and the real thing; CloudArch lets you design cloud systems and put load on them.

About ยท Labs ยท Cheat sheets ยท Learning paths

โ† Back to LSM trees

๐Ÿ—‚๏ธ LSM trees: cheat sheet

๐ŸŽจ The analogyA journalist's in-tray: new notes go in a sorted tray, and full trays become folders that are never edited.
๐ŸŽ“ The regular explanationA log-structured merge tree buffers writes in a sorted in-memory memtable backed by a write-ahead log, flushes it to immutable sorted SSTables, and merges those files in the background through compaction. Writes are fast and sequential; reads may have to check several files.
The write pathStep 5 of 5
Day bookzoekey zoe=Leeds: zoe0annkey ann=Paris: ann1bobkey bob=Rome: bob234๐Ÿ“žCaller๐Ÿ“ฅ Desk trayann ยท bob ยท zoe
  1. A caller says Zoe moved to Leeds. Mia first jots it at the bottom of her day book, in the order calls come in.
  2. Then she drops the note into the desk tray, in its place by name, and says "got it".
  3. More calls come in about Ann and Bob. The day book keeps them in call order, while the tray keeps them sorted by name.
  4. Someone knocks the tray off the desk and the notes scatter. The tray is empty, but the day book is safe in Mia's bag.
  5. Mia reads her day book from the top and puts every note back into the tray. Nothing is missing.
Flushing a full memtableStep 4 of 4
Fresh day book pageannkey ann=Rome: annโœฆโœฆโœฆโœฆโœฆ0123Folder 1 ยท never editedannkey ann=Paris: ann0bobkey bob=Rome: bob1dankey dan=Oslo: dan2zoekey zoe=Leeds: zoe3๐Ÿ“žCaller๐Ÿ“ฅ Desk trayann (newer)
  1. The day book already lists Zoe, Ann and Bob. A call about Dan arrives, and the tray now holds four notes, which is all it can take.
  2. Mia puts the whole stack, already sorted by name, into a new folder and files it in the cabinet.
  3. The folder keeps the notes safe now, so Mia throws away those day book pages and starts a fresh page and an empty tray.
  4. Ann moves again. Mia doesn't open the folder to fix her note. She just writes a newer note for Ann.
Reading newest firstStep 4 of 4
Folder 3 (newest) ยท card: surely notevekey eve=Nice: eve0faykey fay=Bern: fay123Folder 2 ยท card: maybe ยท tab Abobkey bob=Rome: bob0catkey cat=Lima: cat1dankey dan=Oslo: dan23Folder 1 (oldest) ยท old Bob, not readannkey ann=Paris: ann0bobkey bob=Oslo: bob1zoekey zoe=Leeds: zoe23๐Ÿ™‹Asker๐Ÿ“ฅ Desk trayno bob
  1. The cabinet holds three folders. Someone asks where Bob lives. Mia checks the desk tray first, because it has the newest notes. No Bob there.
  2. Next, the newest folder. Its check card says "surely no Bob in here", so Mia skips it without opening it.
  3. The next folder's card says "maybe Bob". Mia opens it at the A tab, finds Bob's note right away and answers Rome.
  4. The oldest folder also has a Bob note, saying Oslo. Mia never opens it. The first Bob she found was the newest, so it's the right one.
Tombstones and mergingStep 5 of 5
Merged folder ยท no Zoe, no stickyannkey ann=Paris: ann0bobkey bob=Rome: bob1catkey cat=Lima: cat2๐Ÿ“žCaller๐Ÿ™‹Asker๐Ÿ“ฅ Desk trayempty
  1. Folder 1 has a note for Zoe. Zoe asks to be removed, but folders are never edited, so Mia adds a "Zoe deleted" sticky note to the tray, next to a new note about Cat.
  2. The tray is filed as Folder 2, sticky note and all. The old Zoe note still sits in Folder 1, taking up room.
  3. Someone asks about Zoe. The tray is empty, and the newer folder has the sticky note, so Mia stops there and answers "no note for Zoe".
  4. Tidy-up time. Mia merges both folders into one new folder, keeping the newest note for each person and dropping the sticky note with the old Zoe note.
  5. The two old folders go in the bin. One tidy folder is left, and Zoe's note and its sticky are gone for good.
Size-tiered and leveled compactionStep 5 of 5
New A to M folder ยท only place for Bobannkey ann=Rome: ann0bobkey bob=Lyon: bob1catkey cat=Bern: cat2New N to Z foldernedkey ned=Riga: ned0patkey pat=Kyiv: pat1samkey sam=Doha: sam2
  1. The pile habit. Four thin folders of about the same size have piled up, and Bob is in two of them.
  2. Mia merges all four into one thick folder, keeping only the newest note for each person.
  3. The thin folders go in the bin and the thick folder is filed away. Now the shelf habit. A fresh folder on the top shelf holds any letters, and the next shelf splits letters between two folders.
  4. To move the top folder down, Mia merges it with every shelf 2 folder that shares its letters. Ann, Cat, Ned and Sam get copied again just to fit Bob and Pat in.
  5. The old folders go in the bin. On shelf 2 only one folder can hold Bob, so finding him means opening at most one folder per shelf.
LSM tree versus B-treeStep 4 of 4
๐Ÿ“žCaller๐Ÿ“’ Ring binderone place๐Ÿ“ฅ Desk trayno Ann here๐Ÿ—‚๏ธ Filed folders
  1. With the ring binder, every call means flipping to Bob's page and rewriting his note right there.
  2. With the in-tray, the same call is just a note dropped into the tray. No flipping, so a flood of calls is no problem.
  3. Finding Bob in the binder is easy. He is in exactly one place.
  4. Finding Ann with the in-tray means checking the tray first, then a folder or two. Quick to write, a bit more work to read.

๐Ÿงฉ Key questions

Mia said "got it" for Zoe's note, then the tray was knocked off the desk before it was filed. What happens to Zoe's note?
Mia rebuilds the tray from her day book, so the note comes back. Every note goes into the day book before Mia says "got it", and the day book is safe.
Bob has a note in the tray saying Rome and an older one in a folder saying Oslo. What does Mia answer, and why?
Rome, because she checks the newest place first and stops there. The tray always has the newest notes, so the first Bob she finds is the current one.
Why does Mia add a "deleted" sticky note instead of just throwing Zoe's note away?
Folders are never edited, so without the sticky the old note in a folder would still answer. The sticky note is newer, so it's found first, and it's dropped together with the old note when folders are merged.
Mia's notes change all the time and people ask about them constantly. Which tidying habit fits best?
The shelf habit, because only one folder per shelf can hold each person. Searches open fewer folders and less room goes to old notes, at the price of more copying.
Compared with the ring binder, what does the in-tray give up?
Quick finding: a search may check the tray and several folders. The binder keeps each person in one place but needs flipping for every call. The tray takes calls quickly and pays when searching.