Skip to content

Lesson 1 of 9 · 24m

Storage Engines: Pages, Buffer Pools, and the Write-Ahead Log

The storage engine's real job is surviving crashes while making disk I/O predictable. Pages, buffer pools, and WALs are the pieces you must understand first.

The first thing a senior database engineer learns is that SQL is a polite fiction. Underneath it, the database is moving fixed-size pages between disk and memory, replaying logs after crashes, and trying very hard not to fsync on every row. Once you see that machinery, mysterious production behavior stops being mysterious.

Databases think in pages, not rows

InnoDB normally uses 16 KiB pages. PostgreSQL uses 8 KiB blocks. Rows live inside those pages, and the engine reads or writes whole pages even when a query touches one row.

logical row:  id=4821, email='sam@example.org'
physical reality:
  page 193822
    header | row pointers | free space | tuples | index metadata

This explains a classic confusion: updating one small row can dirty an entire page, and dirty pages are what eventually get written back. Compression, fill factor, row width, and hot-row update patterns are all page-layout problems.

The buffer pool is the real working set

Databases cache pages in memory. PostgreSQL calls the main cache shared_buffers; InnoDB calls it the buffer pool. If your hot pages fit there, reads are memory-speed. If they do not, every miss becomes storage latency.

-- PostgreSQL: see what is cached and how often blocks are hit
SELECT datname, blks_hit, blks_read,
       round(100.0 * blks_hit / nullif(blks_hit + blks_read, 0), 2) AS hit_pct
FROM pg_stat_database;

A 99% hit ratio is not automatically healthy. A tiny cache can have a great hit ratio while the remaining 1% represents your most important queries. Measure latency and page reads, not one vanity ratio.

The write-ahead log exists because disks lie

You cannot safely update a data page first. A crash halfway through can leave a torn or inconsistent page. So engines append a description of the change to a write-ahead log before the data page is considered durable.

transaction commit:
1. append redo/WAL records
2. flush WAL to durable storage
3. acknowledge commit
4. write dirty data pages later, in the background

PostgreSQL has WAL files under pg_wal; InnoDB has redo logs. This ordering is why commits are fast even when table files are not constantly rewritten: durability comes from a sequential log append, not random page writes.

Crash recovery is replay, not magic

After a crash, the engine reads the log from a checkpoint and redoes committed changes, then rolls back transactions that never committed. Large checkpoints and huge backlogs of dirty pages make recovery slower. This is why endless write bursts, tiny WAL settings, and abrupt power loss are a nasty combination.

Field notes

  • shared_buffers bigger is not always better; the operating system page cache is also doing useful work.
  • Full-page writes, checksums, and doublewrite buffers exist because torn pages are real hardware behavior.
  • Sequential WAL writes are the reason rotational disks survived in databases longer than people expected.
  • If a table grows faster than its live data, suspect page fragmentation, dead tuples, or wide-row updates before blaming the disks.

Was this lesson helpful?

Sign in to rate