WRITE-THROUGH vs WRITE-BACK

Safety vs speed — pick one, lose the other

L214 MINCACHING
PREDICT

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.

▶ SIMULATION
APP · WRITE-BACK
acknowledged: —
mean write ack: —
reads: —
—
CACHE (volatile)
(empty)
dirty: 0
STORE (durable)
x = 0
y = 0
store writes: 0
virtual clock0 ms
acknowledged writes lostnone
✓ no acknowledged write is lost on a crash
SCHEDULE
01[App]policy: write-back
02[App]write x = 1
03[App]write y = 5
04[App]write x = 2
05[App]read x
06[Fault]CRASH: cache process dies, memory lost
07[App]report
Start of write-back + crash scenario. 7 steps.
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.

BREAK IT NOT YET

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

FIX IT NOT YET

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.

Mental model:

an acknowledgement is only as durable as the slowest thing it waited for. Write-through waits for the store, so it is slow and safe. Write-back waits for memory, so it is fast and safe only up to the last flush. Whenever you see a 1 ms write in front of a 10 ms store, ask where the dirty data lives if the process dies.

IN PRODUCTION

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.

IN INTERVIEWS

“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.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: CACHE-ASIDE →