Open In App

Write Through and Write Back in Cache

Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite – Multilevel Cache Organisation 
Cache is a technique of storing a copy of data temporarily in rapidly accessible storage memory. Cache stores most recently used words in small memory to increase the speed at which data is accessed. It acts as a buffer between RAM and CPU and thus increases the speed at which data is available to the processor. 

Whenever a Processor wants to write a word, it checks to see if the address it wants to write the data to, is present in the cache or not. If the address is present in the cache i.e., Write Hit

We can update the value in the cache and avoid expensive main memory access. But this results in Inconsistent Data Problem. As both cache and main memory have different data, it will cause problems in two or more devices sharing the main memory (as in a multiprocessor system). 
This is where Write Through and Write Back comes into the picture. 

Write Through: 

In write-through, data is simultaneously updated to cache and memory. This process is simpler and more reliable. This is used when there are no frequent writes to the cache(The number of write operations is less). 

It helps in data recovery (In case of a power outage or system failure). A data write will experience latency (delay) as we have to write to two locations (both Memory and Cache). It Solves the inconsistency problem. But it questions the advantage of having a cache in write operation (As the whole point of using a cache was to avoid multiple access to the main memory). 

Write Back: 

The data is updated only in the cache and updated into the memory at a later time. Data is updated in the memory only when the cache line is ready to be replaced (cache line replacement is done using Belady’s Anomaly, Least Recently Used Algorithm, FIFO, LIFO, and others depending on the application). 
Write Back is also known as Write Deferred. 

Dirty Bit: Each Block in the cache needs a bit to indicate if the data present in the cache was modified(Dirty) or not modified(Clean). If it is clean there is no need to write it into the memory. It is designed to reduce write operation to a memory. If Cache fails or if the System fails or power outages the modified data will be lost. Because it’s nearly impossible to restore data from cache if lost. 

If write occurs to a location that is not present in the Cache(Write Miss), we use two options, Write Allocation and Write Around

Write Allocation: 

In Write Allocation data is loaded from the memory into cache and then updated. Write allocation works with both Write back and Write through. But it is generally used with Write Back because it is unnecessary to bring data from the memory to cache and then updating the data in both cache and main memory. Thus Write Through is often used with No write Allocate. 

Write Around:
 

Here data is Directly written/updated to the main memory without disturbing the cache. It is better to use this when the data is not immediately used again.
 


Last Updated : 21 Aug, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads