5.4.6 · D4Memory Hierarchy & Caches

Exercises — Write-through vs write-back

2,179 words10 min readBack to topic

Before we start, one shared cost model we will reuse everywhere so no number is a surprise:


Level 1 — Recognition

Exercise 1.1

State, in one sentence each, when main memory gets updated under (a) write-through and (b) write-back.

Recall Solution

(a) Write-through: memory is updated on every single write (a store to a cached line writes cache and memory at the same time). (b) Write-back: memory is updated only when a dirty line is evicted (replaced) — the store itself only touches the cache and sets the dirty bit.

Exercise 1.2

A cache line has dirty bit = 0 and is chosen for eviction. Does memory get written? Why?

Recall Solution

No. Dirty bit means the cache copy and memory copy are identical, so writing back would just copy the same bytes onto themselves — pure waste. The line is simply discarded and the new line loaded. Memory write only happens when dirty bit .

Exercise 1.3

Under write-back, the dirty bit is tracked per line, not per byte. Give the two hardware reasons.

Recall Solution
  1. Memory transactions are line-granular. A memory write moves a whole -byte line in one burst, so you cannot send "just the 1 changed byte" cheaply anyway.
  2. Per-byte dirty bits cost too much. A -byte line would need dirty bits instead of — that's the flag storage and logic for essentially no traffic saving.

Level 2 — Application

Exercise 2.1

Compute the write latency of a single store that hits the cache under each policy.

Recall Solution

Write-through: cache write + memory write happen together, but the store isn't "done" until memory acknowledges: Write-back: only the cache is touched; memory is left stale: Speedup on this one write: (parent rounds this to "" by dropping the ).

Exercise 2.2

A program writes to the same cache line times, then the line is evicted while dirty. Give total write-related time for each policy.

Recall Solution

Write-through — every write pays the full memory latency: Write-back cheap cache writes, then one memory write on eviction: Speedup: . The many repeated writes are why write-back wins: it amortizes one memory write over all .

Exercise 2.3

A read miss occurs on a set whose victim line is dirty. Explain and quantify the penalty versus a read miss on a clean victim (write-back cache). See the figure.

Figure — Write-through vs write-back
Recall Solution

Clean victim: just fetch the new line. Dirty victim: you must first write the old dirty line back to memory, then fetch the new one — two memory accesses (the two arrows in the figure): So a dirty eviction doubles the miss penalty. This is the price write-back pays for its cheap writes.


Level 3 — Analysis

Exercise 3.1

For a workload: , . Every store instruction is one "write". Compute the ratio of memory-bus writes under write-back to write-through.

Recall Solution

Write-through: every store hits the bus → per store. Write-back: a store only reaches the bus if it causes a miss and evicts a dirty line: Ratio: Write-back cuts memory-write traffic to (a reduction). This is the argument for write-back on the DRAM bus where bandwidth is scarce.

Exercise 3.2

Write-through with a write buffer of depth . Write rate writes/ns (one write every ns), ns. Does the CPU stall? Use .

Recall Solution

Compute the "writes in flight during one memory latency": . No stall. The buffer holds pending writes, but only arrive per drain time, so it never fills. The buffer fully hides memory latency here.

Exercise 3.3

Same buffer (), but a write-heavy burst raises writes/ns (one write every ns). Compute and the average write time .

Recall Solution

Writes in flight: . Now writes arrive faster than the buffer drains, so it saturates of the time and the CPU eats most of the memory latency. The buffer only helps up to its depth.


Level 4 — Synthesis

Exercise 4.1

Build the crossover. Under write-back, a line is written times before eviction (eviction always dirty). Write and , then find the at which write-back becomes cheaper. Use , .

Recall Solution

Write-through: each of the writes pays cache + memory: Write-back: cheap cache writes + one memory write at eviction: Crossover (write-back cheaper when ): So for any writes to the same line, write-back wins; at (write-once) they are essentially tied (, ). This is the mathematical statement of "write-back needs temporal locality."

Exercise 4.2

A design uses write-back for L1 but write-through from L1→L2. Give one benefit for coherence and one cost. Link the coherence idea to the right vault topic.

Recall Solution

Benefit (coherence): write-through to L2 keeps L2 always up-to-date, so a snooping coherence protocol can service other cores' reads straight from L2 without hunting for a dirty L1 copy — this simplifies the invalidation/ordering rules discussed under memory consistency. Cost: every L1 store now generates L1→L2 traffic (bandwidth ↑) even for data reused many times, partly giving up write-back's traffic savings on the L1→L2 link.


Level 5 — Mastery

Exercise 5.1

Full latency model. Write-back with write-allocate. Given: Miss Rate , , , . Use . Then compute write-through's (assume its 8-deep buffer never stalls). Which wins, and by how much?

Recall Solution

Write-back: Write-through (buffer never stalls ⇒ ): Winner: here write-through is faster on average ( ns vs ns)! The reason: at miss rate the write-back miss/writeback overhead ( ns) outweighs the buffer-hidden write-through cost. Latency is not the whole story — write-back still wins on bus traffic (Ex 3.1) and never depends on a non-stalling buffer, so under a saturated buffer (Ex 3.3) the verdict flips. Policy choice depends on which metric your system is bottlenecked by.

Exercise 5.2

Design task. You are building a cache for a GPU streaming kernel: each address is written exactly once, never re-read, gigabytes flowing through. Choose the write policy and the allocation policy, and justify from the numbers you've computed.

Recall Solution

Policy: write-through + no-write-allocate.

  • Why not write-back? Each address is written once (). From Ex 4.1, write-back gives zero amortization benefit at — you pay one memory write per line either way. Its extra machinery (dirty bits, writeback logic, dirty-eviction double penalty from Ex 2.3) buys nothing.
  • Why no-write-allocate? The data is never re-read, so fetching a line into cache on a write miss (write-allocate) is wasted bandwidth — you'd fetch bytes just to overwrite them and never read them. No-write-allocate sends the write straight to memory, no fetch.
  • Buffer: add a deep write buffer to coalesce sequential writes and hide latency for the streaming pattern (Ex 3.2 shows a buffer fully hides latency when writes don't out-race the drain). This is exactly the "write-once / streaming" case the parent's Mistake 1 steel-mans.