How Does the Page Cache Accelerate File I/O?

The page cache is a region of main memory managed by the operating system kernel that stores copies of recently accessed file data from secondary storage. By serving repeated read requests directly from RAM and batching write operations in the background, it bridges the speed gap between fast processors and much slower disk drives. This mechanism reduces physical disk accesses, lowers application latency, and improves overall system throughput.

Why Disk I/O Needs Caching

In classical computing architectures, accessing data in dynamic random-access memory (DRAM) takes tens of nanoseconds, whereas accessing a solid-state drive (SSD) takes tens to hundreds of microseconds and a spinning hard disk drive (HDD) takes milliseconds. If every read() or write() system call waited for a physical storage device, the CPU would spend most of its time stalled on I/O.

The page cache exploits temporal locality (data accessed once is likely to be accessed again soon) and spatial locality (data near recently accessed bytes is likely to be needed next). When the kernel keeps file blocks in RAM, subsequent accesses bypass the storage bus entirely.

How the Page Cache Buffers Disk Reads

When a process issues a read request for a file, the kernel first checks whether the corresponding 4 KiB memory pages are already resident in the page cache:

  • Cache Hit: If the requested pages are present in memory, the kernel copies the data directly from the page cache into the process's user-space buffer. No disk I/O occurs.
  • Cache Miss: If the pages are not in the cache, the kernel allocates free page frames, schedules a disk read to populate them, and blocks the requesting process until the transfer completes. Once loaded, the data remains in the page cache for future requests.

To further reduce misses, operating systems perform read-ahead (or prefetching). When the kernel detects sequential read patterns, it proactively loads subsequent contiguous pages from disk into the page cache before the application explicitly requests them.

How Delayed Writes (Write-Back) Work

Most modern operating systems use a write-back policy rather than writing every modification immediately to disk:

  1. Marking Pages Dirty: When an application writes to a file, the kernel modifies the corresponding pages in the page cache and marks them as dirty. The write() system call returns immediately once the data is in RAM, allowing the application to continue executing without waiting for the storage device.
  2. Coalescing and Reordering: Multiple small writes to the same page are absorbed in memory, turning several logical writes into a single physical write.
  3. Background Flushing: Kernel background threads periodically wake up to flush dirty pages to disk. Flushing is triggered when dirty pages exceed a memory threshold, when pages remain dirty longer than a configured age limit, or when an application explicitly requests durability via fsync() or fdatasync().

Memory Pressure and Page Eviction

Because main memory is finite, the page cache cannot grow indefinitely. When the system needs free RAM for running applications or new file data, the kernel reclaims space from the page cache:

  • Clean pages can be discarded immediately because an identical copy already exists on disk.
  • Dirty pages must be written back to storage before their page frames can be reused.

Kernels typically use variants of the Least Recently Used (LRU) algorithm—such as active and inactive lists—to decide which pages to evict first, keeping frequently accessed ("hot") file data in memory while reclaiming "cold" pages.

Summary

By caching disk reads in RAM, prefetching sequential data, and coalescing writes through write-back buffering, the page cache transforms slow, frequent storage operations into fast memory accesses and batched background transfers.