Buffer Cache and Write Policies
You'll learn: How operating systems cache disk blocks in memory and the critical tradeoffs between write-through and write-back policies.
The Core Idea
Reading from and writing to disk is slow—thousands of times slower than accessing RAM. To speed things up, file systems maintain a buffer cache (or page cache) in memory that holds recently used disk blocks. When you read a file, the OS checks if those blocks are already cached; if so, it skips the disk entirely. When you write, the OS updates the cached copy first, then decides when to write it back to disk—that's where write policies come in.
Write-Through vs Write-Back
Think of the buffer cache like notes you take while working on a project:
Write-through: Every change you make gets immediately saved to the main document on disk. Safer (your work is always backed up), but slower because each write waits for disk I/O.
Write-back (also called write-behind or lazy write): You update your notes in memory and periodically save them later. Much faster (no waiting for disk), but riskier—if the system crashes before flushing, recent changes are lost.
The Tradeoff
-
Write-through prioritizes durability and consistency. Data hits disk immediately, so crashes won't lose acknowledged writes. Cost: performance penalty on every write operation.
-
Write-back prioritizes performance. Writes complete instantly in memory, and the OS batches disk writes for efficiency. Cost: data loss risk during crashes and complexity in ensuring consistency (hence journaling systems like you saw in lesson 1406).
Most modern file systems use write-back with periodic syncing and journaling to balance speed and safety.
Key Takeaway: Buffer caching speeds up file I/O by keeping disk blocks in memory; write-through trades performance for immediate durability, while write-back trades safety for speed—most systems choose write-back with protective mechanisms.