IIT Madras Zanzibar · Teaching

Module 8: Storage & Indexing · Lecture 34 · Week 13

Disk Storage, Pages & the Buffer Pool

How databases physically read and write data; why the disk hierarchy defines everything

Learning Objectives

Disk storage structure
Figure 34.1: A hard disk drive: platters, tracks, sectors, and the read/write head assembly

1. Why Physical Storage Matters

Every abstraction in a DBMS (relations, SQL queries, transactions) bottoms out in reads and writes to physical storage. The performance of almost every database operation is dominated by I/O cost, not CPU cost. This means that anyone who designs a database, writes queries, or tunes a system must understand how data is stored on disk.

The central tension in database storage is simple: processors operate at nanosecond scale; disks operate at millisecond scale. That is a six-order-of-magnitude gap. The buffer pool manager is the primary layer that bridges this gap by keeping frequently accessed pages in RAM.

2. The Storage Hierarchy

LevelTypical SizeLatencyBandwidthVolatile?
L1 Cache32–512 KB~1 ns>100 GB/sYes
L3 Cache8–64 MB~10 ns~50 GB/sYes
DRAM (RAM)4–512 GB~100 ns~10–50 GB/sYes
NVMe SSD256 GB–8 TB~100 µs3–7 GB/sNo
SATA SSD256 GB–4 TB~200–500 µs~500 MB/sNo
HDD1–20 TB5–15 ms100–200 MB/sNo

The 10,000× Rule

A random HDD read (~10 ms) takes roughly 10,000× longer than a DRAM read (~100 ns). Even on modern NVMe SSDs, random reads are 1,000× slower than DRAM. This means that reducing the number of page I/Os is the most powerful optimization available to a DBMS.

2.1 HDD Internals

A spinning disk consists of one or more platters coated in a magnetic material. Each platter surface is divided into concentric tracks, and each track is divided into sectors (typically 512 bytes or 4 KB). The OS and DBMS read/write in units called blocks or pages, which are multiples of the sector size.

The time to read a random block on HDD has three components:

T_total = T_seek + T_rotational_latency + T_transfer ≈ 5 ms + 4 ms + 0.1 ms ≈ 9–10 ms (typical random HDD access)

Sequential reads are much faster because seek time is paid only once for a long stream of contiguous blocks.

2.2 SSD Characteristics

SSDs have no moving parts. Random reads are much faster (~100–500 µs vs 10 ms on HDD). However, SSDs have asymmetric read/write performance: reads are fast, writes involve erasing an entire flash block (often 128–512 KB) before writing, making random writes slower. This asymmetry influences B+ tree and log-structured storage designs.

3. Pages: The Unit of I/O

A DBMS never reads or writes individual tuples directly from disk. It always reads and writes pages (also called blocks). A page is the unit of transfer between disk and the buffer pool.

Page Size

Typical page sizes are 4 KB, 8 KB, or 16 KB. PostgreSQL uses 8 KB by default; SQL Server uses 8 KB; MySQL InnoDB uses 16 KB. Larger pages mean fewer I/Os for sequential scans but more wasted I/O if you only need a few tuples from a page.

3.1 Page Layout

Each page contains a page header (page ID, free space pointer, number of records, checksum) and the actual record data. Records within a page are typically stored using a slotted page structure:

┌────────────────────────────────────────────┐ │ Header: pageId, numSlots, freeSpacePtr, ... │ ├────────────────────────────────────────────┤ │ Slot 1 → offset, length │ │ Slot 2 → offset, length │ │ ... │ │ (free space) │ │ ... Record 2 │ │ ... Record 1 │ └────────────────────────────────────────────┘

Slots grow from the top of the page; records are packed from the bottom. This allows records to be moved within the page (for compaction) without changing their record ID (RID = pageId + slotNum).

3.2 Record IDs (RIDs)

A record is uniquely identified by its RID = (pageId, slotNum). Index entries in B+ trees and hash indexes store RIDs to point to actual tuples. When a record moves between pages (e.g., due to an UPDATE that grows the record), the RID must be updated or a forwarding pointer left behind.

4. The Buffer Pool

The buffer pool is a region of DRAM managed by the DBMS that caches recently accessed pages. It is divided into fixed-size frames, each the same size as a disk page. The buffer pool manager is responsible for:

Buffer pool diagram
Figure 34.2: The buffer pool in memory holds frames; the disk holds pages. The page table maps pageId → frameId.

4.1 The Pin Count and Dirty Bit

Each frame in the buffer pool has two metadata fields:

4.2 Replacement Policies

PolicyEvictGood ForBad For
LRULeast Recently Used frameGeneral workloads, hot pagesSequential scans (pollutes cache)
MRUMost Recently Used frameRepeated sequential scansRandom access patterns
ClockApproximate LRU, uses a reference bitLow overhead LRU approximationSlightly less optimal than LRU
LRU-KPage whose Kth most recent access is oldestMix of sequential and random accessHigher bookkeeping overhead

Sequential Flooding Problem

LRU performs poorly for full table scans. A sequential scan reads every page once; LRU will evict previously cached hot pages to make room for scan pages that will never be needed again. Many systems use "MRU for sequential scans" or ring-buffer techniques to avoid this: scan pages are marked as "use once" and evicted immediately after use, preserving the hot working set.

4.3 The Clock Algorithm

The Clock algorithm approximates LRU without maintaining a full access-time list. Each frame has a reference bit. When a page is accessed, its reference bit is set to 1. The "clock hand" sweeps around frames:

  1. If reference bit = 1 → set it to 0, advance hand (give the page a second chance)
  2. If reference bit = 0 → evict this frame (it hasn't been accessed since the last sweep)

5. I/O Cost Model

We estimate query cost in units of page I/Os (ignoring CPU). The key parameters are:

B = number of pages in a relation P = number of buffer frames available
OperationI/O CostNotes
Full table scan (heap)BSequential, one pass
Index lookup (B+ tree)log_d(B) + 1d = fill factor, log depth + leaf
Sort (external merge sort)2B · ⌈log_{B-1}(B/P)⌉ + BMultiple passes over data
Simple nested loop joinB_R + B_R · B_SWorst case, no index

6. Pre-fetching and Write-Ahead

Pre-fetching: When the DBMS detects a sequential scan, it issues prefetch I/Os: reading the next N pages before they are requested. This hides disk latency by overlapping I/O with processing.

Write-ahead logging (WAL): Before a dirty page can be written back to disk, its log record must be written to the log on disk first. This ensures that crashes during a write can be recovered. We cover WAL in detail in L41.

7. Summary