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.

Advanced 4 min readLesson 6 of 14 Interview Q&A

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).

headerslot 1slot 2free spacerow 2row 1

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)?

  1. Every change is first appended to the write-ahead log — a sequential file — describing what changed.
  2. At commit, only the log is fsynced. Sequential appends are fast.
  3. Dirty pages are written to their real locations later, in the background (checkpoints).
  4. 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.

5,812,1522,25[10 | 20]33,3742,4752,55[40 | 50]62,6672,7585,90[70 | 80][30 | 60]
B+ tree: internal nodes route, leaves hold the keys and are linked for range scans.
  • 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:

flush when fullcompactionwritememtable (RAM)sorted; + WAL for durabilityL0 SSTablesL1 (larger, sorted, non-overlapping)L2 … (10× bigger each level)
  1. Writes go to an in-memory sorted memtable (and the WAL). No random disk writes at all.
  2. A full memtable is flushed as an immutable sorted file, an SSTable.
  3. Background compaction merges SSTables into larger sorted files, dropping overwritten and deleted (tombstoned) entries.
  4. 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-treeLSM tree
WritesRandom, in placeSequential, batched — much higher write throughput
ReadsFast, predictableMay check several files (Bloom filters help)
SpaceFragmentation in pagesTemporary duplicates until compaction; compresses well
Background workVacuum (Postgres), page splitsCompaction (can cause latency spikes)
Best forRead-heavy OLTP, range queriesWrite-heavy workloads: logs, time series, messaging, IoT

Row vs column storage

Row store

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
Column store

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.

Key takeaways
  • 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.
Interview questions
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.