Write Policy: Write-Through, Write-Back and the Dirty Bit
A write to a cached block leaves two copies that disagree. A cache must decide when memory hears about it: at once, or when the block leaves.
You Will Be Able To
- Compare write-through and write-back by what each sends to memory, and when.
- Explain the dirty bit, and what the controller does when a dirty block must leave.
- Say what a write that misses can do, with and without allocate.
- Lesson
- S3.17
- Time
- 28 min
- Runs on
- Your browser
- Check
- 6 questions
A program adds one to a counter, eight times, and the counter is in the cache, so the adds are fast. Then the power fails. After the restart the counter reads zero. All eight values had lived in one cache line, and memory never saw any of them.
Before You Start
You need S3.5: the states IDLE, COMPARE and ALLOCATE, and the refill. You need S3.3: one line for each index.
Nothing else is needed.
| Question | |
|---|---|
| 1 | What goes wrong if a write changes only the copy in the cache? |
| 2 | What do write-through and write-back each send to memory, and when? |
| 3 | What does the dirty bit record? |
| 4 | What does the controller do when a dirty block must leave its line? |
| 5 | What can a write do when its block is not in the cache? |
The Idea: Two Copies, and a Promise to Memory
A read leaves the cache and memory in agreement: the line holds a copy of what memory holds. A write breaks that. If the write changes only the line, memory holds old data. As long as the block stays in the cache, nobody notices. When the block is replaced, or the power goes, the new data is gone.
So every cache makes a promise: each write will reach memory. There are two ways to keep it. Write-through sends every write to the cache and to memory at once. It is simple and always safe. It also makes every write as slow as memory.
Write-back writes the line only, and remembers that it did. One bit beside each line, the dirty bit, is set by the first write. From then on the line holds the only good copy of the block. Further writes cost nothing extra. The debt is paid once, when the block has to leave: the controller writes the whole block to memory before it reuses the line.
That last step is a job for the controller. It gets a fourth state, between COMPARE and ALLOCATE. A block that was only read has a dirty bit of 0, and is dropped without a write.
One more choice remains. A write can miss, like a read. The cache can fetch the block first and then write into it. That is write-allocate. Or it can send the write straight to memory and leave the cache as it is.
New Words
| Word | Meaning |
|---|---|
| Write-through | A write policy. Every write goes to the cache line and to memory at the same time. |
| Write-back | A write policy. A write goes to the cache line only. Memory gets the block when it leaves the cache. |
| Dirty bit | One bit for each line. At 1, the line was written and memory does not have its content. |
| Clean line | A line whose dirty bit is 0. Memory holds the same content. |
| WRITE_BACK | The state in which the controller writes a dirty block to memory, before the line is reused. |
| Write-allocate | On a write that misses: fetch the block into the cache first, and then write into it. |
| No write-allocate | On a write that misses: send the write to memory, and do not touch the cache. |
One Picture: Where a Write Goes
- Processor
- Memory
- Control / FSM
- Bus
- A write hits
The processor writes a word, and its block is in the cache. The copy in the line changes. Memory still holds the old word.
- Write-through
The first policy sends every write to memory as well, at once. The two never disagree for long. Every write pays for slow memory.
- Write-back
The second policy writes the line only, and sets one bit beside it: the dirty bit. Later writes to the same block cost nothing more.
- A new block wants the line
A miss needs this line for another block. The controller looks at the dirty bit before it lets the old block go.
- The write back
A dirty block holds the only good copy. The controller writes it to memory first, in a state of its own. Then it fetches the new block.
Read the diagram as text
- Processor: writes a word
- Cache line: holds a copy of the block
- Dirty bit: 1: memory does not have this content
- Memory: holds the block itself
- WRITE_BACK: a fourth state of the controller
- A new block: wants this line
- Processor → Cache line: the write
- Cache line → Memory: write-through: at once
- Cache line → Dirty bit: write-back: mark it
- A new block → Dirty bit: is it dirty?
- Dirty bit → WRITE_BACK: yes
- WRITE_BACK → Memory: the old block goes back
What happens to a write, under the two policies. A block whose dirty bit is 0 is simply dropped.
The Smallest Example, Worked by Hand
The counter from the top of the page. It lives in block 5. A program writes it eight times. Then the program reads block 9, which uses the same line, so block 5 must leave. Count what goes to memory under each policy.
| Step | Write-through | Write-back |
|---|---|---|
| first write to block 5: a miss | fetch the block; write line and memory | fetch the block; write the line; dirty bit to 1 |
| seven more writes: hits | 7 writes to line and memory | 7 writes to the line only |
| read of block 9: a miss in the same line | drop block 5; fetch block 9 | write block 5 back; then fetch block 9 |
| Memory writes in all | 8 | 1 |
Both policies fetch two blocks. Only the number of memory writes differs.
If a memory write takes 10 cycles, write-through spends 80 cycles on them and write-back spends 10. The saving grows with every repeated write to the same block.
Now the price of the write back, in the controller. The lesson on the miss path counted a miss at 8 cycles. With a dirty block in the line, the controller first writes four words back, which takes five cycles like a refill.
| State | Cycles | What happens |
|---|---|---|
| IDLE | 1 | the request is seen |
| COMPARE | 1 | a miss, and the line is dirty |
| WRITE_BACK | 5 | the four words of the old block go to memory |
| ALLOCATE | 5 | the four words of the new block come in |
| COMPARE | 1 | a hit: ready |
Thirteen cycles in all: five more than a miss with a clean line.
The Rules
Rule 1: Every write must reach memory at some time.
Until then the cache line holds the only good copy.
Rule 2: Write-through sends each write to memory at once.
Memory is never behind. Every write costs a memory write.
Rule 3: Write-back writes the line and sets its dirty bit.
Memory gets the block once, when the block leaves the cache.
Rule 4: A dirty block is written back before its line is reused.
The controller does this in the state WRITE_BACK, before ALLOCATE. A clean block is dropped.
Rule 5: A fetch clears the dirty bit.
The line and memory agree again from that moment.
Rule 6: A write that misses either fetches its block first, or goes to memory alone.
The first is write-allocate. With it, the write itself is then an ordinary hit.
Sources for These Rules
The textbook "Computer Organization and Design" by Patterson and Hennessy, section 5.3, "Handling Writes": write-through, write-back, and write allocate. Section 5.9 gives the controller with its Write-Back state.
The textbook "Digital Design and Computer Architecture" by Harris and Harris, section 8.3.4: write-through and write-back caches, and the dirty bit.
The counting model, its trace and the 10 cycles of a memory write are our own teaching example. The five cycles of a write back follow from the bus of the lab, with no wait state.
Books pair write-back with write-allocate, and so does the controller of this page. Write-through is seen with and without allocate.
Now Run It
First the counting. A small model in test code holds four lines of one block each. It runs the trace of the hand table three times and counts what goes to memory. You find its code in the first question of the exit check.
write-through | hits=7 fetches=2 memory writes=8 dirty lines left=0
write-back | hits=7 fetches=2 memory writes=1 dirty lines left=0
write-through, no allocate | hits=0 fetches=1 memory writes=8 dirty lines left=0Rules 2 and 3 at work: the first two rows. Both policies have 7 hits and 2 fetches. Write-through makes 8 memory writes, and write-back makes 1.
Rule 6 at work: the third row. With no allocate, the block is never brought in, so all eight writes miss and go to memory. There is only one fetch, for block 9.
Now the controller. The lab at the end of this page is the script of the lesson before, with writes added. It writes one word, and then reads another block that needs the same line.
e1 IDLE | the processor asks to write 5555aaaa to 1a4
e2 COMPARE | miss: ready stays 0, and the processor waits
e3 ALLOCATE | bus: address of word 68
e4 ALLOCATE | bus: address of word 69, data of word 68 = c0de0068
e5 ALLOCATE | bus: address of word 6a, data of word 69 = c0de0069
e6 ALLOCATE | bus: address of word 6b, data of word 6a = c0de006a
e7 ALLOCATE | bus: data of word 6b = c0de006b. The line is filled
e8 COMPARE | hit: ready=1. The word is in the line, and the line is dirty
memory word 69 holds c0de0069
e9 IDLE | the processor asks to read 2a8
e10 COMPARE | miss: ready stays 0, and the processor waits
e11 WRITE_BACK | bus: write address of word 68
e12 WRITE_BACK | bus: write address of word 69, data of word 68
e13 WRITE_BACK | bus: write address of word 6a, data of word 69
e14 WRITE_BACK | bus: write address of word 6b, data of word 6a
e15 WRITE_BACK | bus: data of word 6b. The old block is in memory
e16 ALLOCATE | bus: address of word a8
e17 ALLOCATE | bus: address of word a9, data of word a8 = c0de00a8
e18 ALLOCATE | bus: address of word aa, data of word a9 = c0de00a9
e19 ALLOCATE | bus: address of word ab, data of word aa = c0de00aa
e20 ALLOCATE | bus: data of word ab = c0de00ab. The line is filled
e21 COMPARE | hit: ready=1 data=c0de00aa
memory word 69 holds 5555aaaaRules 3 and 6 at work: rows e1 to e8. The write is a miss, so the block is fetched first. At e8 the word goes into the line and the line is dirty. The next row shows that memory word 69 still holds c0de0069.
Rule 4 at work: rows e10 to e15. The read is a miss and the line is dirty. The controller goes to WRITE_BACK and sends the four words of the old block to memory. Only then, at e16, does ALLOCATE start. The last row shows memory word 69 with 5555aaaa.
The read took 13 edges, from e9 to e21, as in the hand table. This lab leaves out write-through, which needs no new state: it adds a memory write to every write hit.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Write-back | Write-through | Repeated writes to a block cost one memory write. Memory is behind, and a dirty block is lost if the power fails. |
| Write-through | Write-back | Memory is always right, and no dirty bit is needed. Every write is as slow as memory. |
| One dirty bit for a line | One dirty bit for each word | One bit and a simple controller. One written word sends the whole block back. |
| Write-allocate | No write-allocate | Later reads and writes of the block hit. A write to a block that is never used again fetches it for nothing. |
The Trap: The Dirty Block That Was Dropped
A controller is written for reads first, and writes are added later. The write hit sets the dirty bit. The miss path is not touched: it still goes from COMPARE straight to ALLOCATE. Every test with one block passes. Here is the lab with that fault, and one more read at the end:
e9 IDLE | the processor asks to read 2a8
e10 COMPARE | miss: ready stays 0, and the processor waits
e11 ALLOCATE | bus: address of word a8
e12 ALLOCATE | bus: address of word a9, data of word a8 = c0de00a8
e13 ALLOCATE | bus: address of word aa, data of word a9 = c0de00a9
e14 ALLOCATE | bus: address of word ab, data of word aa = c0de00aa
e15 ALLOCATE | bus: data of word ab = c0de00ab. The line is filled
e16 COMPARE | hit: ready=1 data=c0de00aa
e17 IDLE | the processor asks to read 1a4
e18 COMPARE | miss: ready stays 0, and the processor waits
e19 ALLOCATE | bus: address of word 68
e20 ALLOCATE | bus: address of word 69, data of word 68 = c0de0068
e21 ALLOCATE | bus: address of word 6a, data of word 69 = c0de0069
e22 ALLOCATE | bus: address of word 6b, data of word 6a = c0de006a
e23 ALLOCATE | bus: data of word 6b = c0de006b. The line is filled
e24 COMPARE | hit: ready=1 data=c0de0069
memory word 69 holds c0de0069There is no row with WRITE_BACK. From e11 on the new block overwrites the line, and the word 5555aaaa is gone. The read of 1a4 at the end fetches its block again and gets c0de0069, the old word. The last row shows that memory never changed.
Rule 4 was broken. The fault shows only when a written block is pushed out and read again later. A small test that stays inside the cache never sees it.
At Scale
This section looks ahead. Real caches are almost always write-back, and they need more than one way to pay the debt.
Paying the whole debt
Before power-down or a hand-over to another master, software asks the cache to write every dirty line back.
A buffer for writes
The slow memory write can be taken out of the path of the processor. The next lesson shows how, and what it breaks.
Other masters
A block that copies memory by itself reads memory, not the cache. It can see old data behind a dirty line.
Several cores
With more than one cache, a dirty line in one of them is a problem for all the others: Cache Coherence.
Try It
Run the lab. Find the edge at which memory word 69 changes.
Change the read to 10'h1a8. Predict how many edges it takes, and whether a write back happens.
Add a second write to 10'h1a4 before the read. Predict what changes in the rows.
e1 IDLE | the processor asks to write 5555aaaa to 1a4 e2 COMPARE | miss: ready stays 0, and the processor waits e3 ALLOCATE | bus: address of word 68 e4 ALLOCATE | bus: address of word 69, data of word 68 = c0de0068 e5 ALLOCATE | bus: address of word 6a, data of word 69 = c0de0069 e6 ALLOCATE | bus: address of word 6b, data of word 6a = c0de006a e7 ALLOCATE | bus: data of word 6b = c0de006b. The line is filled e8 COMPARE | hit: ready=1. The word is in the line, and the line is dirty memory word 69 holds c0de0069 e9 IDLE | the processor asks to read 2a8 e10 COMPARE | miss: ready stays 0, and the processor waits e11 WRITE_BACK | bus: write address of word 68 e12 WRITE_BACK | bus: write address of word 69, data of word 68 e13 WRITE_BACK | bus: write address of word 6a, data of word 69 e14 WRITE_BACK | bus: write address of word 6b, data of word 6a e15 WRITE_BACK | bus: data of word 6b. The old block is in memory e16 ALLOCATE | bus: address of word a8 e17 ALLOCATE | bus: address of word a9, data of word a8 = c0de00a8 e18 ALLOCATE | bus: address of word aa, data of word a9 = c0de00a9 e19 ALLOCATE | bus: address of word ab, data of word aa = c0de00aa e20 ALLOCATE | bus: data of word ab = c0de00ab. The line is filled e21 COMPARE | hit: ready=1 data=c0de00aa memory word 69 holds 5555aaaaCheck Yourself
Common Mistakes
Wrong Reuse a line without looking at its dirty bit.
A dirty line holds the only good copy of its block.
Fix Write a dirty block back first. Drop only clean blocks.
Wrong Leave the dirty bit at 1 after a fetch.
The new block would be written back although nobody changed it.
Fix Clear the dirty bit when a block is fetched, and set it on a write.
Wrong Read memory from outside while lines are dirty.
Under write-back, memory is behind the cache.
Fix Write the dirty lines back first, or read through the cache.
Remember
- Every write must reach memory. Write-through does it at once, and write-back when the block leaves.
- Write-back sets a dirty bit: the line then holds the only good copy.
- A dirty block is written back before its line is reused. A clean block is dropped.
- A write that misses fetches its block first (write-allocate), or goes to memory alone.