Jab ek CPU kisi cached address par store instruction perform karta hai, toh hume decide karna padta hai kab main memory ko update karein. Cache aur memory dono data ki copies hold karte hain—ye inconsistent ho sakti hain.
Ye kyun zaroori hai: Reads easy hain (cache hit = fast, cache miss = memory se fetch). Lekin writes ek dirty state create karte hain jahan cache ke paas memory se zyada naya data hota hai.
Buffer N writes absorb kar sakta hai stall hone se pehle (typical N=4-8). Agar writes buffer drain se zyada fast aayein:
Stall occurs whenRwrite>Tmemory-writeN
Ye step kyun? Buffer CPU speed ko memory speed se decouple karta hai, lekin sirf apni depth tak.
Kuch systems selective write policies use karte hain:
Frequently accessed data (stack, heap): write-back
I/O regions, memory-mapped devices: write-through
Ye step kyun? I/O devices expect kar sakte hain ki writes immediately visible hon. Write-through ye complex coherence protocols ke bina ensure karta hai.
Zyaatar modern processors mein, saare cache levels (L1, L2, L3) write-back use karte hain taaki levels ke beech aur memory tak traffic minimize ho. L1 par write sirf L1 update karta hai aur uska dirty bit set karta hai; L1 eviction par dirty line L2 mein write back hoti hai, aur L2 eviction par memory mein (aur aage bhi).
L1writeback on evictL2writeback on evictMemory
Ye step kyun? Har level par write-back fast L1→L2 aur slow L2→Memory paths ko critical write path se door rakhta hai. Kuch designs write-through L1 backed by write-back L2 use karte hain (L1 coherence aur error recovery simplify karne ke liye), lekin ye exception hai rule nahi—saare levels par write-back modern common choice hai.
Recall Feynman: Ek 12-saal ke bachche ko samjhao
Socho tum class mein notes le rahe ho. Tumhare paas ek chhota notepad (cache) hai aur ek bada notebook (memory).
Write-through is tarah hai jaise teacher jo kuch bhi kahe, tum turant apne notepad AUR bade notebook dono mein copy karo. Isme zyada time lagta hai kyunki tum do jagah likh rahe ho, lekin tumhara bada notebook hamesha up-to-date rehta hai. Agar tumhara notepad kho jaaye, koi baat nahi—sab kuch bade notebook mein hai.
Write-back is tarah hai jaise class ke dauran notepad mein jaldi se notes scribble karo (super fast!), aur unhe din ke end mein hi bade notebook mein copy karo. Class ke dauran bahut fast, lekin agar copy karne se pehle notepad kho jaaye, toh aaj ke notes gaye! Saath hi, agar koi din ke dauran tumhara bada notebook dekhe, unhe tumhare latest notes nahi dikhenge.
"Dirty bit" is tarah hai jaise notepad ke un pages par star⭐ lagaana jo abhi copy nahi hue, taaki tumhe yaad rahe ki baad mein kaunse pages copy karne hain.
6.2.03-Memory-ConsistencyModels - Parallel systems mein write visibility
5.3.02-DRAM-Architecture - Memory writes slow kyun hote hain
#flashcards/hardware
Write-through cache policy :: Har write cache aur memory dono ko immediately update karta hai; hamesha synchronized lekin slower (~100 ns per write)
Write-back cache policy
Writes sirf cache ko dirty bit ke saath update karte hain; memory eviction par update hoti hai; faster (~1 ns) lekin delayed consistency
Dirty bit kya hota hai?
Ek per-line flag jo indicate karta hai ki cache line modify ho chuki hai aur memory se alag hai; eviction par writeback ki zaroorat signal karta hai
Write-through speedup with write buffer
CPU ko memory speed se decouple karta hai writes queue karke; effective agar buffer depth ≥ write burst length ho; buffer fill hone par stall hota hai
Write-back memory traffic reduction
Locality wale workloads ke liye writes ~40× reduce karta hai; sirf evictions memory mein likhti hain, har store instruction nahi
Write miss in write-back cache (write-allocate)
Pehle memory se line fetch karni padti hai (~T_memory), phir cache mein write, plus writeback agar evicted line dirty thi; ~100–200 ns cost even in write-back
Write-through kab jeetata hai?
Write-once patterns with no temporal locality; streaming writes jahan write-back koi amortization offer nahi karta; I/O regions jahan immediate visibility chahiye
Eviction cost in write-back
Dirty line eviction 2× memory latency cost karta hai (writeback old + fetch new); same line par bahut saari writes mein amortized
Dirty bit per line kyun, per byte nahi?
Memory writes line-granular hoti hain (64B bursts); per-byte tracking hardware mein bahut expensive hai; koi bhi byte dirty ho toh poori line write back hoti hai
Kya modern L1 caches write-through use karte hain?
Usually nahi—zyaatar modern processors saare cache levels (L1, L2, L3) par write-back use karte hain; write-through L1 exception hai, rule nahi