4.1.12 · HinglishComputer Architecture (Deep)

Write policies — write-through, write-back, write-allocate

2,161 words10 min readRead in English

4.1.12 · Coding › Computer Architecture (Deep)


WHY do we even need a policy?

Do design pressures hain:

  • Speed: main memory (DRAM) mein likhna cache (SRAM) mein likhne se ~100× slow hai. Hum memory writes avoid karna chahte hain.
  • Correctness/Simplicity: memory eventually match karni chahiye. Jitna zyada delay karein, utni zyada bookkeeping (dirty bits) chahiye.

Policy 1 — On a write HIT

The dirty bit — WHAT and WHY

Har cache line ko ek extra bit milta hai.

  • dirty = 0: cache copy == memory (clean). Eviction pe, bas discard karo — koi memory write nahi.
  • dirty = 1: cache copy modify ki gayi hai. Eviction pe, tumhe pehle poora block write back karna zaroori hai.

Isliye write-back ko dirty bit chahiye aur write-through ko nahi (memory kabhi peeche nahi hoti).


Policy 2 — On a write MISS

Jis block mein hum likhna chahte hain woh cache mein nahi hai. Do choices hain:

Figure — Write policies — write-through, write-back, write-allocate

HOW it works — step by step

Write HIT

Policy Action
Write-through Cache aur memory mein likho. (aksar ek write buffer ke through taaki CPU stall na ho)
Write-back Sirf cache mein likho, dirty=1 set karo.

Write MISS

Combination Action
Write-allocate Memory se block → cache mein padho, phir cache mein likho.
No-write-allocate Sirf memory mein value likho; cache unchanged rehta hai.

Eviction of a block (write-back only)

  • Agar dirty=1: poora block memory mein write karo, phir naya block load karo.
  • Agar dirty=0: bas overwrite karo — koi memory write nahi.

Deriving the cost — from first principles


Worked examples


Common mistakes


Flashcards

Write-through memory mein likhta hai
har write pe (cache aur memory saath update hote hain).
Write-back memory mein likhta hai
sirf tab jab ek dirty block evict hota hai.
Write-back ko har line pe kaunsa bit chahiye aur kyun
dirty bit, taaki pata chale ki memory stale hai aur eviction pe likhna zaroori hai.
Kya write-through ko dirty bit chahiye
Nahi — memory hamesha current rehti hai.
Write-allocate vs no-write-allocate apply hote hain ek
write MISS pe.
Write-allocate ka action write miss pe
block ko cache mein fetch karo, phir likho (hit ki tarah treat karo).
No-write-allocate ka action write miss pe
seedha memory mein likho, block cache mein mat laao.
Sabse common modern pairing
write-back + write-allocate.
Common simple pairing
write-through + no-write-allocate.
Average write cost write-through
.
Average write cost write-back
jahan =miss rate, =dirty-eviction fraction.
Write-back hot loops ke liye kyun faster hai
repeated writes cache hit karti hain; slow sirf rate pe (eviction pe) pay hota hai, har write pe nahi.
Ek memset ke liye best policy jo tum kabhi read nahi karoge
no-write-allocate (bekar block fetch avoid karta hai).
Write-through latency ko CPU se kaun hide karta hai
ek write buffer.
Write-back mein clean-block eviction pe memory
untouched rehti hai (bas copy discard karo).

Recall Feynman: 12-year-old ko explain karo

Socho tumhari notebook (cache) class textbook (memory) ki ek copy hai. Jab teacher koi fact change kare, tum ya toh seedha jaake textbook bhi edit kar sakte ho (write-through — safe lekin bahut running) ya bas apni notebook mein fix karo aur page pe ek sticky note lagao (write-back — sticky note dirty bit hai). Tum tabhi textbook theek karne ki taklif lete ho jab us notebook page ki zaroorat kisi aur cheez ke liye ho (eviction). Aur agar koi fact bilkul naya hai aur tumhari notebook mein nahi hai (write miss), tum decide karte ho: pehle poora page notebook mein copy karo (write-allocate) kyunki shayad tum ise dobara use karoge, ya sirf seedha textbook mein scribble karo aur bother mat karo (no-write-allocate) agar yeh ek baar ki cheez hai.

Connections

  • Cache Memory Fundamentals — tags, index, blocks, hit/miss.
  • Cache Replacement Policies — eviction (LRU/random) tab hota hai jab write-back dirty data flush karta hai.
  • Cache Coherence — MESI — write policies M (Modified=dirty) state ko cores mein underlie karte hain.
  • Write Buffers and Store Buffers — write-through memory latency kaise hide karta hai.
  • Memory Hierarchy and AMAT, average memory access time mein feed hote hain.
  • DMA and I/O Consistency — kyun DMA reads se pehle stale dirty cache lines flush karni padti hain.

Concept Map

solves

answers on hit

answers on miss

option A

option B

requires

triggers write on

faster via

option A

option B

pairs with

pairs with

Write policy

Consistency problem: cache copy vs memory

Write HIT policy

Write MISS policy

Write-through: update cache and memory

Write-back: update cache only

Dirty bit

Eviction

Locality: repeated writes

Write-allocate: fetch block first

No-write-allocate: write to memory