1.5 The Buffer Pool

It is inefficient to read a page and then discard it from memory right away, because the pages of frequently referenced records will soon be needed again. So a database keeps pages read from disk in memory for a while. This memory space is called the buffer pool, or the buffer cache depending on the database.

When a query requests a page, the database checks the buffer pool first. If the needed page is already there, it references memory directly with no disk read. If the page is not in the buffer pool, it is read from disk. Finding the page in memory is a cache hit, and the opposite is a cache miss. The share of all requests served from the buffer pool is the cache hit ratio, and for business systems around 99% is usually considered healthy. 99% looks sufficient, but remembering that disk access is thousands of times slower than memory, the 1% of misses can account for most of the total time.

Figure. Flow of the buffer pool handling a page request

flowchart TB
  Q["Query requests a page"] --> F["Look for the page in the buffer pool"]
  F -->|"Found: cache hit"| M["Use the page in memory directly"]
  F -->|"Not found: cache miss"| S["Check whether the buffer pool has a free slot"]
  S -->|"Free slot available"| R["Read the page from disk and load it"]
  S -->|"Full"| V["Pick the least recently used page (LRU)"]
  V --> DT["Check whether the picked page is a dirty page"]
  DT -->|"Dirty page"| W["Write its changes to disk first"]
  DT -->|"Clean page"| X["Free the slot"]
  W --> X
  X --> R
  R --> M

The figure draws, from top to bottom, the order in which the buffer pool handles a request for one page.

  1. When a query requests a page, the database first looks for it in the buffer pool.
  2. If the page is there, it is a cache hit. The page in memory is used directly with no disk I/O, and that is the end (left path).
  3. If the page is not there, it is a cache miss. The page must be read from disk, so the database first checks whether the buffer pool has a free slot. If it does, the page is read from disk and loaded right away.
  4. If the buffer pool is full, one page is picked for eviction. The basic principle is to pick the page that has gone unused the longest (LRU).
  5. If the picked page is a dirty page, its changes are written to disk first and then the slot is freed. If it is a clean page, its content matches the original on disk, so the slot is freed immediately.
  6. The page read from disk is loaded into the freed slot, and the query uses it.

The key point is that disk I/O occurs only on the path where a cache miss happened. The higher the hit ratio, the more requests end at step 2.

The buffer pool has limited size. When it is full, one page must be removed to make room for a new one. Which one? It makes sense to evict the page nobody has looked for the longest. This principle is called LRU (Least Recently Used), and each database designs its own page retention policy in detail to optimize performance. These rules are covered in more detail in Section 1.6, which follows.

There is one more thing to consider when evicting a page from the buffer pool. A page in which data was added or deleted in the buffer pool no longer matches the original on disk. So the modified content must be applied to disk before eviction. A page modified in the buffer pool (memory) but not yet reflected on disk is called a dirty page. A dirty page must have its changes written to disk before it is removed from the buffer pool. There is a reason, though, for not writing to disk on every modification and instead accumulating changes in the buffer pool for a while: even if the same page is changed many times, the final version needs to be written to disk only once. This approach does carry the risk that, if the server suddenly goes down, modifications that existed only in the buffer pool are lost. The mechanism that guards against this risk is WAL (Chapter 5), covered in Part 2.

When a Large Scan Overflows the Buffer Pool

If one full scan pushes out all the frequently used pages, then even after the scan ends, every read of an evicted page must go to disk for a while.

Figure 1-5. How one full scan exhausts the buffer pool

block-beta
  columns 13
  space:2 dir<["MRU (most recently used)  ->  LRU (evicted next)"]>(right):8 space:3
  l0["(1) normal"]:2 c0_0["O1"] c0_1["C1"] c0_2["P1"] c0_3["O2"] c0_4["C2"] c0_5["O3"] c0_6["P2"] c0_7["C3"] space:3
  l1["(2) scanning"]:2 c1_0["S3"] c1_1["S2"] c1_2["S1"] c1_3["O1"] c1_4["C1"] c1_5["P1"] c1_6["O2"] c1_7["C2"] n1["O3 P2 C3 evicted"]:3
  l2["(3) scan ends"]:2 c2_0["S20"] c2_1["S19"] c2_2["S18"] c2_3["S17"] c2_4["S16"] c2_5["S15"] c2_6["S14"] c2_7["S13"] n2["all hot pages gone"]:3
  l3["(4) next query"]:2 c3_0["O1"] c3_1["S20"] c3_2["S19"] c3_3["S18"] c3_4["S17"] c3_5["S16"] c3_6["S15"] c3_7["S14"] n3["O1 miss, read from disk"]:3
  legend["O = order page · C = customer page · P = product page · S = statistics table page (S1~S20)"]:13
  class dir,l0,l1,l2,l3,legend plain
  class c0_0,c0_3,c0_5,c1_3,c1_6,c3_0 slot
  class c0_1,c0_4,c0_7,c1_4,c1_7 data
  class c0_2,c0_6,c1_5 meta
  class c1_0,c1_1,c1_2,c2_0,c2_1,c2_2,c2_3,c2_4,c2_5,c2_6,c2_7,c3_1,c3_2,c3_3,c3_4,c3_5,c3_6,c3_7 hot
  class n1,n2,n3 warn

The figure assumes a small buffer pool that holds 8 pages and has only the simple rule of evicting the page unused the longest. Each row is the 8 slots of the buffer pool at one point in time; the left end (MRU) is the most recently used page and the right end (LRU) is the next to be evicted. A newly loaded page enters at the left end, and when space runs short, the page at the right end is pushed out. The statistics table has more pages (S1 to S20, 20 pages) than the buffer pool has slots (8).

  • (1) Normal. Frequently used pages such as order (O), customer (C), and product (P) pages fill the buffer pool, so most lookups finish in the buffer pool.
  • (2) Full scan in progress. When someone full-scans a large statistics table, each time a statistics page (S1, S2, S3, ...) comes in at the left end, the popular page that was unused the longest at the right end is pushed out, one at a time. In this row, O3, P2, and C3 were pushed out while three pages were read.
  • (3) Scan ends. By the end of the scan, only the last pages of the statistics table (S13 to S20) remain in the buffer pool. These pages are almost never reused, yet they occupy the slots because they are treated as the most recently used.
  • (4) Next query. If orders are then queried as usual, every lookup misses. The order and customer pages must be read back from disk one by one, for example reading O1 from disk again, placing it at the left end, and evicting S13.

This is why, even after one full scan finishes, the whole service stays slow until the popular pages are loaded again.

Each database has countermeasures to prevent this. Some let large scans use only a separate area, and others first insert a newly loaded page around the middle of the buffer pool and promote it toward the front only if it is read several times.