5. Page cache and writeback
One-liner. The kernel uses free RAM as a cache, and writes back lazily unless you force it.
Memorize
- Reads populate the page cache; repeated reads hit memory, not disk.
- Writes go to the cache and are marked dirty; writeback flushes them later (
dirty_ratio,dirty_background_ratio). fsync/fdatasyncforce durability;syncflushes everything (rarely what you want).O_DIRECTbypasses the cache (databases), trading simplicity for control.- Read-ahead prefetches sequential data; random I/O gets little benefit.
- Cached memory is reclaimable: "free" RAM being low is normal; look at
available. - io_uring submits many async I/Os with far fewer syscalls than
epoll+read.
Trap. Assuming a successful write() survives a power loss — it doesn't until fsync or writeback completes. Do this. fsync the file and the directory entry when creating files that must survive.
Check
Why can write() return successfully and still lose data on power loss?
Why
Durability requires an explicit flush to stable storage; writeback is asynchronous by design.