Reading data is easy to understand: cache missed, then read it from memory.
But writing data add more complexity: write the data to both cache(s) and memory or only cache(s)?
Write after missed
If the answer of ❸ and ❹ is yes, then you are using Write-through,
else if the answer of ❸ is no but ❹ is yes, then you are using Write-back.
Why call it write-back? It is because the write to the backing store(
cache2) is postponed until the modified content is about to be replaced by another cache block.
The idea is critical, please think it again and make sure you are understood.
It will write-back to
cache2 when original block in
cache1 is replaced by another cache block.
What's the side-effect after replaced by another cache block?
The answer is: write missed.
How to handle write-missed?
As you can see, after the replacement(❶) (if you are using write-back strategy than it will trigger write-back(❷)) next time processor try to write new data(
k1 -> x2), it realized that the data is dirty and start making decision to handle this situation.
There have two main strategy to handle this: write-allocate and no-write allocate which is answered by @TEMLIB.
As you can see, when implement the write-back strategy, there is stale value in the
cache2, how to avoid this situation?
As @Pseudonym answered, communication protocols between the cache managers which keep the data consistent are known as coherency protocols.