WRITE-THROUGH vs WRITE-BACK
Safety vs speed — pick one, lose the other
Write-back cache. The app writes x=1, y=5, x=2, gets three acks, reads x, then the cache crashes before any flush. What does the store hold?
A cache in front of a store has to decide when a write reaches the store. Write-through writes both before it acknowledges, so the app waits for the slow store every time. Write-back writes only the cache, marks the entry dirty and acknowledges at once; a later flush copies dirty entries to the store, and several writes to one key become one store write. Write-around skips the cache, writes the store and drops any cached copy. The ack is a promise. Under write-back the promise is backed by memory that a crash erases.
Three write policies (cache 1 ms, store 10 ms)
================================================
WRITE-THROUGH write(k, v): cache[k] = v; store.write(k, v); ack
ack 11 ms. Durable at ack. Reads after the write hit the cache.
WRITE-BACK write(k, v): cache[k] = v; dirty.add(k); ack
flush(): for k in dirty: store.write(k, cache[k])
ack 1 ms. Durable only at flush. Repeated writes to one key
coalesce into one store write. A crash loses every dirty key.
WRITE-AROUND write(k, v): store.write(k, v); delete cache[k]; ack
ack 10 ms. Durable at ack. The next read misses and refills.
Good for data written once and rarely read (logs, bulk loads),
which would otherwise evict useful entries.
A flush that happens to run before a crash is luck, not safety:
the ack still came from memory. Write-back is safe only when the
dirty data survives the cache dying: a battery-backed RAID
controller, an fsync'd log, or dirty data replicated to another
node before the ack. That costs a little ack latency (the log
append), but keeps the coalesced store writes. Compare the mean ack latency and the store-write count across scenarios. Only WRITE-BACK + CRASH breaks the invariant. LUCKY: FLUSH BEFORE CRASH stays green only because the flush happened to run first; the acks still came from memory. FIX: LOG BEFORE ACK appends each write to a durable log before acking, so recovery replays what the crash wiped. It pays 2 ms per write instead of 10. Write-through and write-around pay 10 ms per write to be safe at every moment.
This write-back cache flushes periodically in the background. Inject one crash so that the app loses a write it was told had succeeded.
GOAL: an acknowledged write is lost
The starter treats every policy as write-back, so write-through and write-around lie about durability. Implement all three. Write-around writes only the store, so the next read of that key misses. The grader counts store calls during each write (that is the latency the app waits) and store reads after it, then crashes the cache by throwing your object away and reads the store.
CPU caches and RAID controllers are write-back (the controller's cache is battery-backed for this reason). The OS page cache is write-back until you call fsync. Application caches such as Redis in front of a database are usually write-through or cache-aside; write-behind setups need a durable queue or replication to be safe.
“Write-through or write-back for this service?” State what each acknowledges, the latency each pays, and what a crash loses. Then say how you would make write-back safe if you needed its latency: persist or replicate dirty entries before the ack, and bound the flush interval.
USED IN
Chapters that build on this lesson.
- Caching in front of the databaseDatabases