Sub topic 12
Write-Through, Write-Back, and Allocation on Write Miss
Assume you know cache blocks, hits and misses, cache mapping, and miss penalty. This chapter focuses on what happens when the CPU writes data.
A write hit occurs when the target block is already in the cache. A write miss occurs when the target block is not present. A write policy must answer two separate questions:
- When does a write update main memory?
- Should a missed block be brought into the cache?
The first question defines write-through or write-back. The second defines write-allocate or no-write-allocate.
Write-through
With write-through, every write hit updates both the cache and the next memory level immediately.
For example, if the CPU changes X from 10 to 20, the cache stores 20 and main memory also stores 20. Therefore, memory always contains the latest value.
Advantages include:
- Simple consistency between cache and memory.
- No dirty bit is required.
- Evicting a cache block does not require a write-back.
The main disadvantage is traffic. Every write generates a lower-level memory operation. A write buffer is often used so that the CPU does not wait for each slower memory write.
On a write miss, write-through is commonly combined with no-write-allocate. The processor writes directly to the lower level instead of first loading the block into the cache.
Write-back
With write-back, a write hit changes only the cache copy. Main memory is updated later, when the modified block is evicted.
The cache stores a dirty bit for each block. The dirty bit is 1 if the cache copy differs from the lower-level copy.
- On a write hit, update the cache and set the dirty bit.
- On eviction, write the block to the lower level only if the dirty bit is 1.
- After writing it back, the replacement block can occupy the cache location.
Write-back reduces memory traffic when a block is written several times before eviction. However, it needs dirty bits and more complicated eviction logic. At a given moment, main memory may contain an older value.
Allocation on a write miss
Write-allocate loads the missed block into the cache, then performs the write in the cache. This is also called fetch on write.
Suppose the CPU writes byte B in a block that is absent:
- Read the complete block from the lower level.
- Place it in the cache.
- Modify
B. - Apply the selected write policy.
Write-allocate is useful when the program will access nearby bytes or write the same block again. It is commonly paired with write-back.
No-write-allocate sends the write to the lower level without placing the missed block in the cache. This avoids fetching data that may never be read. It is commonly paired with write-through.
| Policy | Write hit | Write miss behavior |
|---|---|---|
| Write-through | Update cache and lower level | Often no-write-allocate |
| Write-back | Update cache and dirty bit | Often write-allocate |
| Write-allocate | Fetch block, then write cache | Useful for repeated access |
| No-write-allocate | Do not fetch block | Useful for one-time writes |
A write decision path
GATE problem strategy
For write-back questions, check whether an evicted block is dirty. A clean block causes no write-back. For write-allocate, include the block-fetch cost on a write miss. For no-write-allocate, include only the lower-level write cost.
Exercise
A cache uses write-back and write-allocate. The CPU writes to an absent block and later evicts it after another write. What lower-level operations occur?
Show answer
The cache first fetches the block on the write miss. The writes update the cached copy and set its dirty bit. On eviction, the dirty block is written back once.