Skip to the guide

How a database stores data: pages, rows and logs

IntermediateConcept3 pages

Before choosing a database, open one up: where a row sits on disk, why every database keeps a log, and what "committed" promises.

A filing cabinet and a notebook by the door

Picture a records office. Every record is a sheet of paper in a folder, and every folder holds the same number of sheets. When a caller phones in a change, the clerk fetches the whole folder to her desk and corrects the sheet there. Before she tells the caller "done", she writes one line at the end of a notebook by the door: "folder 812, sheet 4: address is now 12 Hill Road". That line takes seconds, and once it's written the change can't be lost, even if the desk is knocked over. In a quiet moment she refiles the changed folders and marks in the notebook how far she got.

  • the clerk → the database engine, and the caller → the order service sending a change
  • a sheet → a row
  • a folder → a page: the fixed-size block a database reads and writes, 8 KB in PostgreSQL and 16 KB in MySQL's InnoDB by default
  • the desk → the buffer pool: pages kept in memory
  • the notebook → the write-ahead log (WAL): every change, appended in order and flushed before the engine says "committed"
  • refiling in a quiet moment → a checkpoint