Sub topic 12
Write-through, write-back and allocation on write miss
This chapter assumes you can split an address into tag, index and offset, and that you know hit, miss and miss penalty. Reads were enough up to now. A write is harder, because it creates two copies of the same data: one in the cache and one in main memory. A write policy decides when those copies are brought back into agreement. A separate policy decides what to do when the write misses.
Two questions, two policies
Every write raises two independent questions.
- Write hit: the block is in the cache. Do we update main memory now or later? This is the choice between write-through and write-back.
- Write miss: the block is not in the cache. Do we bring it in first? This is the choice between write-allocate and no-write-allocate.
The two choices can be combined in four ways. Only two combinations are common in practice, and GATE questions assume one of them.
Write-through
In a write-through cache, every write updates the cache line and main memory at the same time.
- Memory always holds the current value. A line can be dropped at any moment without losing data.
- Each write costs a memory access, so writes run at memory speed unless a buffer hides the delay.
- A write buffer is a small queue between the cache and memory. The CPU drops the write into the queue and continues. It stalls only when the queue is full.
Write-through needs no extra state per line. Eviction is trivial because the victim is never newer than memory.
Write-back
In a write-back cache (also called copy-back), a write hit updates only the cache. The line is marked with a dirty bit, which is 1 when the line differs from memory. Memory is updated only when a dirty line is evicted.
- Many writes to the same block cost one memory write in total.
- Memory is temporarily stale. This matters for I/O and for other processors that share memory.
- Each line needs one extra bit, the dirty bit.
- A miss that evicts a dirty line costs two memory transfers: write the victim back, then fetch the new block.
Allocation on a write miss
Write-allocate (fetch on write) loads the missing block into the cache and then performs the write as if it were a hit. No-write-allocate (write-around) sends the write straight to memory and leaves the cache unchanged.
| Combination | Write hit | Write miss | Typical use |
|---|---|---|---|
| Write-back + write-allocate | Cache only, set dirty bit | Fetch block, then write in cache | Most modern caches |
| Write-through + no-write-allocate | Cache and memory | Memory only | Simple designs, often with a write buffer |
| Write-through + write-allocate | Cache and memory | Fetch block, then write both | Legal but rare |
| Write-back + no-write-allocate | Cache only, set dirty bit | Memory only | Rare |
The usual pairings follow from locality. Write-back assumes that a written block will be used again soon, so allocating it pays off. Write-through sends every write to memory anyway, so allocating the block on a miss gains little.
Decision flow
Counting memory traffic in code
The simulator below uses a direct-mapped cache whose blocks are one address unit wide, so a block fill is one read and a write-back is one write. It counts memory reads and memory writes for each of the four combinations.
def simulate(ops, num_lines, write_back, write_allocate):
lines = {} # index -> [tag, dirty]
mem_reads = 0
mem_writes = 0
for op, addr in ops:
idx = addr % num_lines
tag = addr // num_lines
line = lines.get(idx)
hit = line is not None and line[0] == tag
if op == "R":
if not hit:
if line is not None and line[1]:
mem_writes += 1 # write back dirty victim
mem_reads += 1 # fetch block
lines[idx] = [tag, False]
else:
if hit:
if write_back:
line[1] = True
else:
mem_writes += 1
elif write_allocate:
if line is not None and line[1]:
mem_writes += 1 # write back dirty victim
mem_reads += 1 # fetch block
if write_back:
lines[idx] = [tag, True]
else:
lines[idx] = [tag, False]
mem_writes += 1 # write-through of the new value
else:
mem_writes += 1 # write around the cache
return mem_reads, mem_writes
# Addresses 0 and 4 map to the same line in a 4-line cache.
trace = [("W", 0), ("W", 0), ("W", 0), ("R", 4)]
for wb in (True, False):
for wa in (True, False):
name = ("write-back" if wb else "write-through") + \
(" + write-allocate" if wa else " + no-write-allocate")
print(name, simulate(trace, 4, wb, wa))The output is (reads, writes) for each combination:
write-back + write-allocate (2, 1)
write-back + no-write-allocate (1, 3)
write-through + write-allocate (2, 3)
write-through + no-write-allocate (1, 3)Write-back with write-allocate performs one memory write for three program writes. The first write misses and brings the block in. The next two hit and only set the dirty bit. The read of address 4 evicts the dirty line, which produces the single write-back. The other three combinations send every write to memory, so each performs three writes.
Worked example: GATE-style traffic count
A write-back cache serves 1000 memory accesses, of which 20% are writes. The miss ratio is 5%, and 30% of evicted blocks are dirty. The block size is 4 words.
Write-through: every write goes to memory, so there are 1000 × 0.20 = 200 word writes.
Write-back:
- Misses = 1000 × 0.05 = 50.
- Each miss evicts one block (assume the cache is full), so 50 blocks are evicted.
- Dirty evictions = 50 × 0.30 = 15 block writes, which is 15 × 4 = 60 words.
Write-back moves 60 words against 200 for write-through. The comparison depends on the dirty fraction and the block size. If every evicted block were dirty, the write-back total would be 50 × 4 = 200 words, equal to write-through.
Effect on access time
When a question asks for effective access time with a write policy, treat reads and writes separately.
- Write-through without a buffer: every write takes the memory write time, so a write hit costs memory time, not cache time.
- Write-through with a perfect buffer: a write hit costs only cache time.
- Write-back: a write hit costs cache time. The miss penalty becomes
(1 + d) × block transfer timefor a dirty fractiond, because a dirty victim adds a second transfer.
A common form of the average read-and-write time is:
T = f_read × T_read + f_write × T_writeRead the question for the stated fractions and for how the write time is defined.
Common mistakes
- Counting write-back traffic per write instead of per eviction. Memory is written only when a dirty line leaves the cache.
- Forgetting the dirty bit in storage totals. A write-back cache adds one bit per line to the tag-store size. Write-through adds none. Some questions also include a valid bit. Add only the bits the question mentions or clearly implies.
- Applying write-allocate to reads. Allocation on a read miss always happens. The policy name refers to write misses only.
- Assuming write-through caches are always consistent for multiprocessors. Memory is current, but other processors' caches can still hold stale copies. That needs a coherence protocol, which is a separate topic.
Summary of trade-offs
| Property | Write-through | Write-back |
|---|---|---|
| Memory traffic on repeated writes | One memory write per write | One memory write per dirty eviction |
| Memory consistency | Always current | Stale until eviction |
| Extra storage | None | Dirty bit per line |
| Eviction cost | Never writes memory | May write a block |
| Control complexity | Low | Higher |
Exercise
A write-back, write-allocate, direct-mapped cache has 4 lines with one-word blocks, and starts empty. The access sequence is W 3, W 3, R 7, W 7, R 3. Addresses 3 and 7 map to the same line. How many memory reads and memory writes occur?
Show answer
Line index is address mod 4, so 3 and 7 both map to line 3. W 3: miss, fetch (reads 1), line dirty. W 3: hit, no memory traffic. R 7: miss, the dirty line for 3 is written back (writes 1), fetch 7 (reads 2), line clean. W 7: hit, line becomes dirty. R 3: miss, the dirty line for 7 is written back (writes 2), fetch 3 (reads 3). Total: 3 memory reads and 2 memory writes.
Exercise
A program issues 5000 accesses, 40% writes. The cache is write-back with a 4% miss ratio, 25% of evicted blocks are dirty, and the block size is 8 words. How many words does memory receive under write-back, and how many would it receive under write-through?
Show answer
Write-through: 5000 × 0.40 = 2000 word writes. Write-back: misses = 5000 × 0.04 = 200, dirty evictions = 200 × 0.25 = 50 blocks, and 50 × 8 = 400 words. Write-back sends 400 words and write-through sends 2000.
Exercise
Why is write-through usually paired with no-write-allocate, while write-back is usually paired with write-allocate?
Show answer
Write-through sends every write to memory anyway, so fetching a block on a write miss adds a transfer without saving future writes. Write-back saves memory writes only when a block is written repeatedly in the cache, so bringing the block in on a write miss lets later writes to it hit and set the dirty bit instead of going to memory.