Indexing & Storage Engines
Understand B-Trees, LSM-Trees, WALs, and Bloom filters to explain read vs. write speed.
In plain words
An index is like the index at the back of a book: instead of reading every page to find a word, you jump straight to it. Storage engines decide how data is physically written to disk.
Think of it like…
A B-Tree is an alphabetical binder where you erase and rewrite pages in place; an LSM-Tree is a notebook where you only append new notes at the end and merge notebooks overnight.
Where you’ve seen it
Searching 100M users by email without an index scans every row. With an index on email, the database finds the match in a few milliseconds.
Words you’ll hear
- Index
- An extra structure that makes lookups on a column fast.
- B-tree
- A sorted tree index; great for reads and range queries.
- LSM tree
- A write-optimized engine that buffers writes and merges them later.
- Full table scan
- Reading every row because no useful index exists. Slow at scale.
Visual diagram
Theory & deep dive
Why indexes make queries 1,000× faster (and cost writes)
Without an index, finding WHERE email = "a@b.com" forces a full table scan across millions of rows. An index keeps a sorted lookup map pointing straight to the matching row in O(log N) - roughly 3–4 steps for 100 million rows.
The catch: every INSERT, UPDATE, or DELETE must also update every index on that table. Over-indexing slows down writes and eats extra RAM/disk.
WAL (Write-Ahead Log) and Bloom Filters
Both B-Trees and LSM-Trees write changes sequentially to an append-only Write-Ahead Log (WAL) before saying "committed." If power fails mid-operation, the database replays the WAL on reboot so nothing is lost.
LSM-Trees also use Bloom filters - tiny bit arrays in RAM that tell you instantly "this key is DEFINITELY not in this SSTable file" - skipping pointless disk reads.
How it works, step by step
Step 1 of 41. Append durability first via the Write-Ahead Log (WAL)
Before mutating complex tree structures, every write appends sequentially to an on-disk WAL so crashes can replay committed state.
- Write Pattern
- Sequential append
- Crash Recovery
- WAL replay
When to use it & trade-offs
What it is
Storage engines organize bytes on disk. B-Trees update sorted pages in place for fast reads, while LSM-Trees append sequentially to memory/SSTables for fast writes.
Why it matters: Knowing why B-Trees favor reads and LSM-Trees favor heavy writes lets you tune indexes, avoid write amplification, and pick the right DB engine.
When to reach for it
- Adding composite indexes aligned with
WHERE col1 = ? AND col2 > ?(equality before range) - Choosing LSM-backed engines for write-heavy telemetry, chat history, or ingestion
- Using Bloom filters in caches/DBs to reject non-existent keys cheaply
When to avoid or be careful
- Creating an index on every column of a write-heavy table
- Using UUIDv4 as a primary key in a clustered B-Tree (causes severe page splitting and cache thrashing - use ULID/UUIDv7)
B-Tree vs. LSM-Tree Engines
Strengths: Every key exists in one place; fast reads with zero compaction stalls.
Watch out: Random 4–16 KB page writes and write amplification on heavy random inserts.
Strengths: Sequential disk writes give huge write throughput and better compression.
Watch out: Reads may check multiple SSTables (mitigated by Bloom filters); compaction spikes.
Strengths: Leftmost prefix ordering + covering columns let queries finish in index-only scans.
Watch out: Every extra index slows down INSERT/UPDATE/DELETE and consumes RAM/disk.
Interview signals & numbers to know
When this topic comes up
- slow SQL query
- write amplification
- SSTable
- random UUID primary key
- Bloom filter
Benchmarks & rules of thumb
- 4-level B-Tree capacity
- Up to ~10⁹ keys
- Bloom filter size (1% FP)
- ~10 bits per key
- Sequential vs random HDD/SSD
- 5–50× faster
- Postgres page size
- 8 KB
Common mistakes & follow-ups
Common interview mistakes
- Indexing low-cardinality booleans or putting range columns first in a composite index
- Using random UUIDv4 as a clustered primary key in MySQL/InnoDB instead of time-ordered UUIDv7/Snowflake
- Assuming Bloom filters can delete keys easily (standard Bloom filters only support insert & lookup)
Likely interviewer follow-ups
- Why do random UUIDv4 primary keys degrade B-Tree write performance at scale?
- How do Bloom filters prevent cache penetration and speed up LSM-Tree reads?
- Explain write amplification vs. read amplification between B-Trees and LSM-Trees.
Knowledge check
Put it into practice
Design full architectures with AI feedback
Apply Indexing & Storage Engines inside real High-Level Design prompts or review Q&A flashcards.