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.

Five questions this page answers
Question
1What goes wrong if a write changes only the copy in the cache?
2What do write-through and write-back each send to memory, and when?
3What does the dirty bit record?
4What does the controller do when a dirty block must leave its line?
5What 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

New words on this page
WordMeaning
Write-throughA write policy. Every write goes to the cache line and to memory at the same time.
Write-backA write policy. A write goes to the cache line only. Memory gets the block when it leaves the cache.
Dirty bitOne bit for each line. At 1, the line was written and memory does not have its content.
Clean lineA line whose dirty bit is 0. Memory holds the same content.
WRITE_BACKThe state in which the controller writes a dirty block to memory, before the line is reused.
Write-allocateOn a write that misses: fetch the block into the cache first, and then write into it.
No write-allocateOn a write that misses: send the write to memory, and do not touch the cache.

One Picture: Where a Write Goes

A Write, Two Copies, and the Dirty BitStep through the picture. A write makes the cache and memory disagree. Two policies bring them back together.
  • Processor
  • Memory
  • Control / FSM
  • Bus
A Write, Two Copies, and the Dirty BitStep through the picture. A write makes the cache and memory disagree. Two policies bring them back together. component Processor writes a word component Cache line holds a copy of theblock component Dirty bit 1: memory does nothave this content component Memory holds the block itself component WRITE_BACK a fourth state of thecontroller component A new block wants this line the writewrite-through: atoncewrite-back: mark itis it dirty?yesthe old block goesback
  1. component Processorwrites a word
    • the write → Cache line
  2. component Cache lineholds a copy of the block
    • write-through: at once → Memory
    • write-back: mark it → Dirty bit
  3. component Memoryholds the block itself
  4. component Dirty bit1: memory does not have this content
    • yes → WRITE_BACK
  5. component A new blockwants this line
    • is it dirty? → Dirty bit
  6. component WRITE_BACKa fourth state of the controller
    • the old block goes back → Memory
  1. 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.

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

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

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

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

Eight writes to one block, and then the block leaves
StepWrite-throughWrite-back
first write to block 5: a missfetch the block; write line and memoryfetch the block; write the line; dirty bit to 1
seven more writes: hits7 writes to line and memory7 writes to the line only
read of block 9: a miss in the same linedrop block 5; fetch block 9write block 5 back; then fetch block 9
Memory writes in all81

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.

One read that pushes a dirty block out
StateCyclesWhat happens
IDLE1the request is seen
COMPARE1a miss, and the line is dirty
WRITE_BACK5the four words of the old block go to memory
ALLOCATE5the four words of the new block come in
COMPARE1a hit: ready

Thirteen cycles in all: five more than a miss with a clean line.

The Rules

  1. Rule 1: Every write must reach memory at some time.

    Until then the cache line holds the only good copy.

  2. Rule 2: Write-through sends each write to memory at once.

    Memory is never behind. Every write costs a memory write.

  3. Rule 3: Write-back writes the line and sets its dirty bit.

    Memory gets the block once, when the block leaves the cache.

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

  5. Rule 5: A fetch clears the dirty bit.

    The line and memory agree again from that moment.

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

Printed by the counting model on Icarus. The trace: block 5 is written eight times, then block 9 is read.
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=0

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

Printed by the lab on Icarus: a write to 1a4, then a read of 2a8. Both use line 2.
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 5555aaaa

Rules 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 choiceThe alternativeWhat the choice buys, and costs
Write-backWrite-throughRepeated writes to a block cost one memory write. Memory is behind, and a dirty block is lost if the power fails.
Write-throughWrite-backMemory is always right, and no dirty bit is needed. Every write is as slow as memory.
One dirty bit for a lineOne dirty bit for each wordOne bit and a simple controller. One written word sends the whole block back.
Write-allocateNo write-allocateLater 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:

The script with the write back left out. Rows e1 to e8, the write, are as before and are not shown.
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 c0de0069

There 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 page4 lines, 1 dirty bit each
A real cachethousands of dirty bits

This section looks ahead. Real caches are almost always write-back, and they need more than one way to pay the debt.

  1. Paying the whole debt

    Before power-down or a hand-over to another master, software asks the cache to write every dirty line back.

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

  3. Other masters

    A block that copies memory by itself reads memory, not the cache. It can see old data behind a dirty line.

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

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: 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 5555aaaa

Check Yourself

Predict the outputBlocks 2 and 3 are written three times each. Then blocks 6 and 7 are read, and they use the same two lines. The model runs this under three policies. What does the last row say?
// Four cache lines of one block each. A read or a write names a block number. This lab counts what goes to memory.
module tb;
  bit WRITE_BACK = 0;        // 0: write-through. 1: write-back, with a dirty bit
  bit ALLOCATE   = 1;        // 1: a write that misses fetches its block first. 0: it goes to memory only
  logic valid [0:3], dirty [0:3];
  int   holds [0:3];
  int hits, fetches, mem_writes;

  task automatic start(input bit back, input bit alloc);
    WRITE_BACK = back;  ALLOCATE = alloc;
    for (int i = 0; i < 4; i++) begin valid[i] = 0;  dirty[i] = 0; end
    hits = 0;  fetches = 0;  mem_writes = 0;
  endtask

  task automatic bring_in(input int block);        // a miss: the old block leaves, the new one is fetched
    int line;
    line = block % 4;
    if (valid[line] && dirty[line]) mem_writes++;   // a dirty block is written back before it goes
    fetches++;
    valid[line] = 1;  holds[line] = block;  dirty[line] = 0;
  endtask

  task automatic rd(input int block);
    if (valid[block % 4] && holds[block % 4] == block) hits++;
    else bring_in(block);
  endtask

  task automatic wr(input int block);
    int line;
    bit in_cache;
    line = block % 4;
    in_cache = valid[line] && holds[line] == block;
    if (in_cache) hits++;
    else if (ALLOCATE) begin bring_in(block);  in_cache = 1; end
    if (!in_cache)       mem_writes++;              // no allocate: the write goes to memory, and the cache is not touched
    else if (WRITE_BACK) dirty[line] = 1;           // write-back: remember that memory is out of date
    else                 mem_writes++;              // write-through: memory gets every write at once
  endtask

  task automatic report(input string name);
    int left;
    left = 0;
    for (int i = 0; i < 4; i++) if (valid[i] && dirty[i]) left++;
    $display("%-26s | hits=%0d fetches=%0d memory writes=%0d dirty lines left=%0d", name, hits, fetches, mem_writes, left);
  endtask

  task automatic trace;
    repeat (3) wr(2);
    repeat (3) wr(3);
    rd(6);
    rd(7);
  endtask

  initial begin
    start(0, 1);  trace;  report("write-through");
    start(1, 1);  trace;  report("write-back");
    start(0, 0);  trace;  report("write-through, no allocate");
  end
endmodule
ConceptA write changes only the copy in the cache, and nothing else is done. What can go wrong?
ConceptA block is written 20 times and then pushed out. How many memory writes does write-back make?
ConceptWhat does a dirty bit of 1 say?
ConceptA read is a miss, and its line holds a dirty block. In which order does the controller act?
ConceptWhat does write-allocate do with a write that misses?

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.

Practice

Report a problem with this page