Storage Engines
How databases store data on disk — pages and heap files, the buffer pool, write-ahead logging, B-tree vs LSM-tree engines, and row vs column storage.
Underneath SQL, a storage engine decides how rows are laid out on disk, how they're found, and how changes survive crashes. Its design determines whether a database is fast at reads, writes, point lookups or big scans.
Pages and the buffer pool
Databases read and write disk in fixed-size pages (8 KB in PostgreSQL, 16 KB in InnoDB). A page holds a header, a slot directory and the rows (tuples).
The slot directory grows from the front and rows from the back, so rows can move within a page without changing their address (page id, slot number).
The buffer pool caches pages in RAM. Queries work on pages in the pool; modified ("dirty") pages are written back later. Eviction uses LRU-like policies (PostgreSQL uses clock sweep). A well-sized buffer pool holding the working set is the single biggest performance factor for most databases.
Write-ahead logging (WAL)
How can a database promise durability without writing every modified page to disk at commit (which would be very slow and random)?
- Every change is first appended to the write-ahead log — a sequential file — describing what changed.
- At commit, only the log is fsynced. Sequential appends are fast.
- Dirty pages are written to their real locations later, in the background (checkpoints).
- After a crash, redo the log from the last checkpoint to recover committed changes (and undo uncommitted ones, as ARIES-style recovery does).
The WAL does more than crash recovery: it drives replication (ship the log to replicas), point-in-time recovery, and change data capture (Debezium reads the log to stream changes to Kafka).
B-tree engines
The classic design (PostgreSQL, MySQL InnoDB, SQL Server, Oracle, SQLite). Data and indexes are B+ trees: balanced trees with high fan-out (hundreds of keys per page), so even billions of rows are only 3–4 levels deep.
- Updates modify pages in place (logged in the WAL first).
- Reads are fast and predictable: O(log n) page reads, usually with the upper levels cached.
- Writes cause random I/O and page splits; each change may rewrite a whole page (write amplification).
InnoDB stores the table itself as a B+ tree ordered by the primary key (a clustered index); PostgreSQL stores rows in an unordered heap and every index points into it.
LSM-tree engines
Log-structured merge trees (RocksDB, LevelDB, Cassandra, ScyllaDB, HBase, the storage of CockroachDB and TiDB) are optimised for writes:
- Writes go to an in-memory sorted memtable (and the WAL). No random disk writes at all.
- A full memtable is flushed as an immutable sorted file, an SSTable.
- Background compaction merges SSTables into larger sorted files, dropping overwritten and deleted (tombstoned) entries.
- Reads check the memtable, then SSTables from newest to oldest. Bloom filters skip files that can't contain the key, and sparse indexes find the block within a file.
| B-tree | LSM tree | |
|---|---|---|
| Writes | Random, in place | Sequential, batched — much higher write throughput |
| Reads | Fast, predictable | May check several files (Bloom filters help) |
| Space | Fragmentation in pages | Temporary duplicates until compaction; compresses well |
| Background work | Vacuum (Postgres), page splits | Compaction (can cause latency spikes) |
| Best for | Read-heavy OLTP, range queries | Write-heavy workloads: logs, time series, messaging, IoT |
Row vs column storage
All of a row's columns stored together.
- Fetching or updating whole rows is efficient
- Great for OLTP: "get order 42"
- A query reading 2 of 50 columns still reads all 50
Each column stored separately (in large chunks).
- Read only the columns a query needs
- Similar values compress extremely well (run-length, dictionary encoding)
- Vectorised execution processes thousands of values per instruction
- Great for analytics: "average price per region over 5 billion rows"
Column stores and formats: ClickHouse, DuckDB, Snowflake, BigQuery, Redshift, and the open file formats Parquet and ORC used in data lakes. Many databases are now hybrid (HTAP), keeping both forms.
MVCC storage
To let readers and writers run without blocking each other, most engines keep multiple versions of rows (Concurrency control). PostgreSQL writes a new tuple version for every update and later vacuums dead ones; InnoDB updates in place and keeps old versions in undo logs. This is why long-running transactions cause bloat in Postgres and growing undo history in MySQL.
- Data lives in fixed-size pages, cached in the buffer pool.
- WAL: log first and fsync the log at commit; write pages lazily; replay after crashes.
- B-trees update in place and excel at reads and range scans.
- LSM trees buffer writes in memory, flush sorted files and compact — excellent for write-heavy loads.
- Row stores suit OLTP; column stores (and Parquet) suit analytics.
QWhy is Cassandra (or RocksDB) fast at writes?medium
It uses an LSM tree: writes are appended to a commit log and inserted into an in-memory memtable, with no random disk I/O or read-before-write. Memtables are flushed as sorted immutable files, and compaction merges them in the background with large sequential I/O.