Skip to content
derpx06.dev
WritingExplainersNotesAbout
works on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisationworks on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisation

Learning in public.

A student's notebook on system design, distributed systems and databases. Shared as I learn.

ReadWritingExplainersNotesAbout
ElsewhereGitHubLinkedInHugging FaceLeetCode
Say hellomanaskhairnar1511@gmail.com
derpx06
NORMALmain© 2026 Derpx06Pune, Indiatop ↑
Blog/Databases

B-Trees vs LSM-Trees: how databases store your data

Every database picks a side: sort now or sort later. Most performance surprises you've hit come from which side it picked.

Derpx0615 Oct · 4 min read
Contents
Why is there even a choice?The neat shelf: B-treesThe pile: LSM-treesCompaction is where the surprises liveSide by sideSo what do I actually do?
0% read · 4 min leftBack to top ↑
On this page0%
Contents
Why is there even a choice?The neat shelf: B-treesThe pile: LSM-treesCompaction is where the surprises liveSide by sideSo what do I actually do?

Think about your phone gallery. You could sort every photo into neat folders the moment you take it. Finding one later is quick, but every new photo means stopping to file it. Or you could dump everything into one big "Recent" pile. Saving is instant, but finding the photo from last month means scrolling forever.

You can't have both. Sorting is work, and the only question is when you do it. Every database makes this choice, and most of its weird behavior comes from it.

Why is there even a choice?

Disks are much faster at one long, continuous read or write than at jumping between scattered spots. Think of reading a chapter in order versus flipping to random pages. SSDs (the newer kind of storage) shrank that gap but didn't close it, because flash memory has to be erased in big blocks before it can be reused. Meanwhile data shows up in random order, so somebody has to sort it. That is just how it is.

The neat shelf: B-trees

A B-tree keeps everything sorted on disk in fixed-size chunks called pages, and updates them in place. To save a record, you find its page, read it, change it, and write it back. If a page gets full, it splits in two.

Top signposts (in memory)
Middle signposts (in memory)
Leaf pages with your records
Even a billion records sit only three or four levels deep.

Reads are predictable. A lookup costs one or two real trips to disk, even in the worst case. And a range like "March to June" just walks through neighboring pages.

Writes cost more. Pages are usually 4 or 8 KB, so saving 100 bytes means writing thousands. Most databases also write the change to a log first, in case the power goes out, so those bytes land twice. This is called write amplification, and it's often ten times or more for a B-tree.

You already use one

Postgres, MySQL and MongoDB all use B-trees, and so do most filesystems. For apps that read a lot and write a moderate amount, this is the right default.

The pile: LSM-trees

A log-structured merge-tree never reshuffles the shelf. New data goes into a sorted list in memory, with a log on disk as a crash backup. That's the whole write path.

When the list fills up, it gets written out in one long stretch as a file that is never changed again. An update is just a newer entry in a newer file. A delete writes a small marker called a tombstone (basically "ignore the old one").

Reads are where you pay. A record could be in memory or in any file, so you check newest first. Two things help.

  • A quick check per file. A small in-memory structure (a Bloom filter) can say "definitely not here". It may wrongly say "maybe", but it never wrongly answers "no". That lets reads skip almost every file.
  • Background tidying. Compaction merges files, keeps the newest version of each record, and drops the tombstoned ones.

Compaction is where the surprises live

Compaction is background work that competes with your normal work. Under steady heavy writes it can fall behind. Files pile up, reads slow down, and some databases deliberately slow your writes to let it catch up. So you get a database that was fast for six hours and then suddenly isn't.

Also, deleted data doesn't free space until compaction runs. A big delete can raise disk use for a while, because tombstones are records too.

Ranges are the weak spot

The per-file check only answers questions about one specific record. A range scan has to open every file in the range and merge them, so a workload that's mostly ranges fights the design.

LevelDB, RocksDB, Cassandra and HBase are LSM-trees. RocksDB sits quietly under many systems that never mention it.

Side by side

B-treeLSM-tree
Writingscattered, in placecontinuous, appended
Bytes written per byte savedhighlower now, paid later by tidying
Finding one record1 to 2 disk trips, predictabledepends on file count
Reading a rangeexcellentweaker; must merge files
Wasted spacehalf-empty pagesold data until tidying runs
Speed over timesteadymostly fast, with tidying wobbles

There's a formal version of this, the RUM conjecture: you can optimise reads, updates, or small size, and improving two costs you the third. Every "revolutionary" database is just a different point on that triangle, not a way out.

So what do I actually do?

Usually nothing. Take the default. Three cases are worth a closer look.

  1. Very heavy writing (logs, events, sensors, metrics). LSM-trees were built for this.
  2. A mysterious slowdown under steady writes. On an LSM-tree, check the tidying backlog before you blame your queries.
  3. Deleting makes things worse. That's tombstones. The space comes back later.
Key takeaways
  • Sorting is work. A B-tree does it when you write. An LSM-tree does it later, in the background.
  • B-trees give steady, predictable reads and good ranges, but each write costs a lot of disk traffic.
  • LSM-trees write very fast, but reads check many files, and compaction can cause slowdowns and odd space use.
  • Deletes in an LSM-tree add tombstones, so disk use can go up before it goes down.
  • Use the default unless you have heavy writes, mystery slowdowns, or deletes that backfire.
ShareXLinkedInEmail
Written by Derpx06A developer who builds AI search tools and the code around them. I explain what I got wrong first, so you can skip it. Spotted a mistake? Tell me — corrections get credited.
Databases · 5 minWhat is a Bloom filter? A tiny "definitely not" checkerDatabases · 4 minWhat really happens when you delete a file or a database row?Databases · 5 minHow does a database keep your data when the power goes off?
Start here
01Why is 0.1 + 0.2 not 0.3 in code?02How does Git work inside? (Snapshots, not changes)03How does your computer know the time? (And why it can be wrong)