5 min read
The page cache is a performance optimization used by modern operating systems to speed up disk I/O. It acts as a buffer between your applications and the storage device by caching frequently accessed disk pages in RAM. This reduces latency and improves throughput for both reads and writes, often without the application even knowing.
Let’s walk through what happens behind the scenes during read and write operations when the page cache is involved.
When you read a file from disk, the OS first determines which disk pages are needed. It then checks whether those pages are already in the page cache:
This behavior is similar to a read-through cache, where data is automatically loaded into the cache upon a cache miss.
When writing data to a file, the data is initially written to the page cache, not to disk directly. These pages are marked as dirty, meaning they've been modified in memory but not yet persisted.
The OS then uses background threads to flush these dirty pages to disk asynchronously. This makes the page cache behave like a write-back cache.
Write-back caching helps group small writes and reduces disk I/O, but introduces a trade-off that recent writes may be lost if the system crashes before the flush.
You generally cannot disable the page cache globally via standard operating system settings. However, the most common way to bypass it at the application level is by using the O_DIRECT flag when opening a file.
This tells the kernel: "Do not cache this read or write. Operate directly on the disk."
O_DIRECT?There are several reasons why an application might choose to bypass the page cache using O_DIRECT, and these reasons can differ for read and write operations.
Additionally, if a system needs to benchmark raw disk performance, whether for reads or writes, it will often bypass the page cache in both cases to avoid memory-based optimizations that could distort the results.
Here's a simple demo you can try on macOS to observe the page cache behavior:
Assume you have a file named largefile (~4 GB in this example):
This takes around 2.2 seconds as the data is read from disk.

Now it took only 265 ms, which is much faster than the last time, because it's served from RAM.

You'll notice the read time is again close to the original cold read.

Several major systems rely on the OS page cache for performance and simplicity.
Kafka depends heavily on the page cache for log I/O. It reads and writes segment files directly through the filesystem and relies on the OS to cache frequently accessed log entries.
In GFS, chunkservers store file chunks but do not maintain their own in-memory cache. Instead, they rely on the page cache to retain recently accessed data, offloading the caching responsibility to the OS.
RocksDB is a popular key-value store built on a log-structured merge tree (LSM). It stores data in immutable SSTables on disk, which makes them ideal candidates for page caching. Random reads are much faster when SST files are already cached in memory.
The page cache is one of those hidden performance boosts most applications unknowingly benefit from. While it's incredibly useful in most cases, understanding how it works and how to bypass it when needed gives you a powerful tool for optimizing performance.
Hope you learned something new today.
Thank you for reading. Happy coding!