DatabasesAdvancedguide

B-Trees vs. LSM-Trees: The Battle of Storage Engine Internals

Why is MongoDB fast for reads but Cassandra better for writes? Deep dive into the trade-offs between B-Trees and Log-Structured Merge Trees.

Sachin SarawgiApril 20, 20262 min read2 minute lesson
Recommended Prerequisites
Database Indexing Deep Dive

B-Trees vs. LSM-Trees: Choosing Your Storage Engine

At the heart of every database is a storage engine that decides how data is laid out on disk. The two most dominant architectures are B-Trees and LSM-Trees. Understanding the difference is key to picking the right database for your workload.

1. B-Trees: The Read-Optimized Classic

Used by: PostgreSQL, MySQL, MongoDB, Oracle.

  • The Structure: A B-Tree is a self-balancing tree that stores data in fixed-size blocks (pages).
  • The Process: When you update a record, the B-Tree finds the specific page and overwrites it in place.
  • Pros: Fast, predictable read performance ((\log N)$). Great for range scans and applications where data is read more often than written.
  • Cons: Write amplification. Every small update requires reading and rewriting an entire page (usually 4KB or 8KB).

2. LSM-Trees: The Write-Optimized Modernist

Used by: Cassandra, RocksDB (used by Meta), LevelDB, DynamoDB.

  • The Structure: A Log-Structured Merge Tree doesn't update data in place. Instead, it turns all writes into sequential appends.
  • The Process:
    1. Writes go to an in-memory Memtable.
    2. When the Memtable is full, it's flushed to disk as an immutable SSTable.
    3. In the background, a Compaction process merges these files.
  • Pros: Incredible write throughput. Writes are sequential, making them extremely fast on both HDD and SSD.
  • Cons: Read amplification. To find a key, the engine may have to check the Memtable and multiple SSTables. (Though Bloom Filters help mitigate this).

3. The Trade-off: Read vs. Write Amplification

  • Read Amplification: One logical read requires multiple physical disk reads. High in LSM-Trees.
  • Write Amplification: One logical write requires multiple physical disk writes. High in B-Trees (due to page fragmentation and WAL overhead).

Summary

  • Choose B-Trees (Postgres/Mongo) for standard CRUD apps, CMS systems, and relational data where query latency for reads is the primary concern.
  • Choose LSM-Trees (Cassandra/RocksDB) for logging, telemetry, IoT data, and high-frequency messaging systems where you need to ingest millions of events per second.

By matching your data access patterns to the underlying storage structure, you can avoid costly performance bottlenecks as your system scales.

Learning Path: Databases Track

Keep the momentum going

Step 37 of 54: Your next milestone in this track.

Next Article

NEXT UP

SQL vs NoSQL: Which One for Your Next Production MVP?

2 min readAdvanced

📚

Recommended Resources

Designing Data-Intensive ApplicationsBest Seller

The definitive guide to building scalable, reliable distributed systems by Martin Kleppmann.

View on Amazon
Kafka: The Definitive GuideEditor's Pick

Real-time data and stream processing by Confluent engineers.

View on Amazon
Apache Kafka Series on Udemy

Hands-on Kafka course covering producers, consumers, Kafka Streams, and Connect.

View Course

Practical engineering notes

Get the next backend guide in your inbox

One useful note when a new deep dive is published: system design tradeoffs, Java production lessons, Kafka debugging, database patterns, and AI infrastructure.

No spam. Just practical notes you can use at work.

Sachin Sarawgi

Written by

Sachin Sarawgi

Engineering Manager and backend engineer with 10+ years building distributed systems across fintech, enterprise SaaS, and startups. CodeSprintPro is where I write practical guides on system design, Java, Kafka, databases, AI infrastructure, and production reliability.

Keep Learning

Move through the archive without losing the thread.

Related Articles

More deep dives chosen from shared tags, category overlap, and reading difficulty.

More in Databases

Category-based suggestions if you want to stay in the same domain.