When writing to a cache, there are additional issues that need to be considered beyond those of reading. For example, at what point are changes to the cache pushed through to main memory? Writing to main memory immediately simplifies the design but introduces stalls whenever there is a store since accessing main memory is much slower. On the other hand, if only the cache is changed during a store instruction, then one must keep track of which cache lines are changed so that changes are not lost when cache lines are replaced. The two main strategies for writing to a cache are write-through and write-back.
In a write-through cache, stores are immediately sent to main memory. To avoid the latency of main memory, a small queue is maintained of stores that have not finished. Instead of stalling for every store instruction, the data and address are added to the queue which is processed and emptied in parallel to other instructions being executed. Store instructions only need to stall when the queue fills up.
In a write-back cache, stores are not written to main memory immediately. Instead, they are just written to cache and a ``dirty bit'' is set for that cache line. Whenever a cache line is about to be replaced, the dirty bit is checked and the cache line is written to memory if it is set.
Write-back caches allow the cost of writing to memory to be amortized over multiple stores to the same cache line so they tend to be more efficient. However, when a write to main memory is actually done, the whole cache line is written so this write is more expensive. Implementing a write-back cache is also more complicated than implementing a write-through cache.