Inside the Linux Page Cache
When your database (PostgreSQL, MongoDB, etc.) reads a row from disk, it doesn't just read the bytes and forget them. The Linux kernel intercepts the request and caches the data in a region of RAM called the Page Cache. This is the single most important performance component in modern storage systems.
1. The 1000x Speedup
Reading from an SSD takes ~100 microseconds. Reading from RAM takes ~100 nanoseconds. The Page Cache provides a 1000x speedup by ensuring that frequently accessed database pages never hit the physical disk.
2. Dirty Pages and pdflush
When the database writes data, it writes to the Page Cache in RAM first. This page is now marked as Dirty.
- The Optimization: The kernel doesn't write to disk immediately. It waits for a background process (
pdflushorwriteback) to flush dirty pages to disk asynchronously. - The Risk: If the server loses power before the flush, data is lost. This is why databases use a Write-Ahead Log (WAL) to ensure durability.
3. The fsync() Bottleneck
When a database commits a transaction, it calls the fsync() system call. This forces the kernel to flush the specific dirty pages for that transaction to the physical disk right now.
- Performance Hit:
fsyncis the most expensive operation in a database. It forces the CPU to wait for the disk. - Optimization: Use Group Commits to batch multiple
fsynccalls into one, drastically increasing throughput.
4. Cold Cache vs. Warm Cache
If you restart your database server, the Page Cache is cleared. This is a Cold Cache.
- The Symptom: Your database will be extremely slow for the first few minutes after a reboot because every read must hit the physical disk.
- The Solution: Use a "Cache Warmer" script to read the most important tables into memory before opening the database to traffic.
Summary
The Linux Page Cache is the silent partner of every database engine. By understanding how the kernel manages RAM and disk I/O, you can tune your system for the high-throughput, low-latency requirements of a production environment.
