Core Concepts
B-Trees vs LSM-Trees
Follow the same key through a page update and a sorted-file merge, then choose which read and write costs fit the workload.
B-trees maintain an ordered tree of pages as keys change. LSM trees, short for log-structured merge trees, collect writes in memory and flush them to sorted files. The layout affects how much work the database does for each write, how many places a lookup checks, and how much data gets rewritten later.
Follow a key through an ordered tree
The URL Shortener uses a PostgreSQL index to look up a code such as notes. The LSM example uses the same key and value so we can follow one lookup and update through both layouts.
A B-tree groups keys into fixed-size pages. An internal page holds separator keys and points to several child pages. Starting at the root, compare the search key with the separators and follow the child for its range. Eventually we reach a leaf, a page at the bottom with ordered entries. In this example the separator m sends keys at or after m to the right branch. A lookup for notes takes that branch.
A leaf can point to a row or hold the value itself, depending on the engine. A normal PostgreSQL B-tree index entry points to the row in the heap, the main storage area for the table. PostgreSQL can use an index-only scan when the index contains the values the query needs. The visibility map records which heap pages are visible to all transactions. When the map has that information for a page, the scan can skip fetching the heap row. Once a range query finds its first key, it can continue along the ordered leaf pages. PostgreSQL B-tree implementation · PostgreSQL index-only scans
Suppose a leaf holding notes, pine, and reed is full. Inserting oak can split it into two pages. One has notes and oak, the other pine and reed. The parent adds pine as a separator and points to the new right leaf. A full parent can split too. A storage engine can also reclaim space before deciding to split. PostgreSQL records changes in its write-ahead log so an acknowledged durable commit does not require flushing every modified index page individually. PostgreSQL B-tree page splits · PostgreSQL write-ahead logging
Collect new writes into sorted files
An LSM-tree accumulates writes in an in-memory structure called a memtable. RocksDB's default memtable keeps keys in order. The engine can also append changes to a write-ahead log (WAL) and replay that log after a crash to recover changes that had yet to reach a sorted file. The write options determine which failures an acknowledged write can survive.
When the table fills, the engine freezes it and starts a new writable table. A flush writes the frozen table to an immutable sorted file, often called an SSTable. A later update goes into a newer table and can leave the old value intact in another file. RocksDB storage overview
The engine assigns sequence numbers to put updates in order. In this example, notes originally points to Spring at sequence 1. We change it to Summer at sequence 2. A current read after sequence 2 sees Summer, even if Spring remains in another file. A snapshot preserves an earlier read view. A snapshot taken at sequence 1 would still see Spring.
Memory and on-disk files can hold different versions of the same key. Per-file indexes help locate the right block. A Bloom filter can tell the engine that a particular key is absent from a file the filter covers. A possible match still requires a lookup. A range scan may need to merge several sorted inputs and skip versions the reader cannot see. RocksDB block-based tables
Merge files and reclaim old data
Compaction reads and merges existing sorted files into new ones. It can reduce overlap and remove versions that readers no longer need. It consumes disk reads, writes, and CPU. An update can appear in the WAL, a flushed file, and several compaction outputs. The amount of physical writing relative to the logical writes is write amplification.
Deleting notes can create a tombstone, a versioned delete record at sequence 3. It hides Summer at sequence 2 and Spring at sequence 1 from a current read. A snapshot taken at sequence 2 still sees Summer, and compaction must preserve that version while the snapshot needs it.
Compaction also keeps the tombstone while an older value elsewhere could become visible again. It can discard the tombstone once it can remove the covered older versions and preserve every reader's result. Reads can stop showing a key long before the engine reclaims its disk space. RocksDB snapshots and compaction · RocksDB range deletion
Different compaction policies trade read, write, and space costs. RocksDB's leveled approach organizes lower levels to reduce overlapping files in a level. Repeatedly merging overlap can increase disk writes. Its universal compaction can leave more sorted runs to merge later, trading more reads and temporary space for less rewrite work in some workloads. If background compaction cannot keep up, unfinished work grows and writes can slow or stall. RocksDB compaction styles · RocksDB write stalls
Compare the costs on your workload
For redirect lookups and occasional alias inserts, a PostgreSQL B-tree also provides the uniqueness boundary the app needs. Grouping small writes into sorted runs can help an engine sustain a higher write rate. That advantage depends on what the engine later rewrites during compaction. A B-tree can also amplify writes through page updates, splits, and logging.
Run both with representative keys, values, and a realistic cache. Let the workload run long enough for files and background work to build up. Measure the slow end of read latency, such as the 99th percentile. Track physical bytes written and disk space used during large updates or deletions. If the app writes 10 MB of new values and the engine writes 30 MB to storage, the write amplification is 3×. Count logging and background rewrites over the whole test window. RocksDB tuning guide
A storage layout does not determine the whole database contract. Engine options determine crash recovery, what reads and snapshots see, and whether a related operation can be transactional. Choose those guarantees before comparing steady-state throughput.
See also
- Database Indexes - use a B-tree for point, range, and uniqueness queries.
- Write-Ahead Log - distinguish durable logging from flushing data files.
- Isolation Levels and MVCC - follow the versions still visible to a snapshot.
- Probabilistic Data Structures - understand when a Bloom filter can rule out a key.
- Flink - its EmbeddedRocksDB state backend is a concrete place to choose an LSM engine for local operator state.
Used in
- URL Shortener - PostgreSQL's unique short-code index supports the point lookup and settles concurrent alias claims.
Done reading? Mark it so it sticks in your dashboard.