Write-Back and Write-Allocate Policies in Caches
Caches use write policies to determine how data is handled when the CPU writes to memory. Two key policies—write-back and write-allocate—work together to optimize performance by reducing memory traffic and improving efficiency. Here’s how they function in combination:
1. Write-Back Policy
Definition
- A write-back cache delays writing modified data (dirty blocks) back to main memory until absolutely necessary.
- Instead of immediately updating main memory on every write, the cache keeps track of modified blocks and writes them back only when they are evicted (replaced).
How It Works
- When the CPU writes to a cache block:
- The block is marked as dirty (modified).
- The data is updated only in the cache, not in main memory.
- The dirty block is written back to memory only when:
- It is evicted (due to a cache miss requiring replacement).
- A cache flush is triggered (e.g., by software or power management).
Advantages
- Reduces memory traffic: Avoids unnecessary writes to main memory.
- Improves performance: Fewer writes to slow DRAM, reducing latency.
- Efficient for frequently modified data: If a block is written multiple times, only the final version is written back.
Disadvantages
- Complexity: Requires tracking dirty bits and handling evictions carefully.
- Potential data loss: If not properly managed (e.g., during power failure), unsaved dirty data may be lost.
2. Write-Allocate Policy
Definition
- A write-allocate cache ensures that a block is loaded into the cache before a write occurs, even if the data is not already present.
- This is in contrast to write-no-allocate, where writes bypass the cache and go directly to memory.
How It Works
- On a write miss (the data is not in the cache):
- The cache fetches the block from memory into the cache.
- The CPU then modifies the block in the cache.
- On a write hit (the data is already in the cache):
- The CPU updates the block directly in the cache.
Advantages
- Temporal locality: If a block is written once, it’s likely to be accessed again soon.
- Works well with write-back: Allows modified data to stay in the cache until eviction.
- Reduces future misses: Subsequent reads/writes to the same block are faster.
Disadvantages
- Increased cache pollution: Unnecessary blocks may be loaded into the cache.
- Higher latency for initial writes: Requires fetching the block before modification.
How Write-Back and Write-Allocate Work Together
When combined, these policies create an efficient caching strategy:
-
On a Write Hit (Data is in Cache):
- The CPU modifies the block in the cache.
- The block is marked dirty (write-back policy).
- The modified data stays in the cache until eviction.
-
On a Write Miss (Data is Not in Cache):
- The cache allocates space and loads the block from memory (write-allocate).
- The CPU modifies the block in the cache.
- The block is marked dirty (write-back).
- The modified data stays in the cache until eviction.
Example Scenario
- A CPU writes to memory address
0x1000 (not in cache).
- The cache fetches the block containing
0x1000 (write-allocate).
- The CPU modifies the data in the cache and marks it dirty (write-back).
- Later, when the block is evicted, the modified data is written back to memory.
Performance Impact
- Reduced Memory Traffic: Write-back minimizes unnecessary writes to DRAM.
- Better Spatial Locality: Write-allocate ensures that modified data stays in the cache for future accesses.
- Lower Latency: Subsequent reads/writes to the same block are served from the cache.
Common in Modern CPUs
Most high-performance processors (e.g., x86, ARM) use write-back + write-allocate for L1 and L2 caches because:
- It maximizes cache utilization.
- It minimizes memory bandwidth usage.
- It improves performance for workloads with repeated writes to the same memory locations.
Alternatives
- Write-Through + Write-No-Allocate:
- Used in some embedded systems where simplicity is preferred over performance.
- Every write goes to both cache and memory (write-through).
- Writes to uncached blocks bypass the cache (write-no-allocate).
Conclusion
The combination of write-back and write-allocate policies optimizes cache performance by:
- Reducing memory writes (write-back).
- Ensuring modified data stays in the cache (write-allocate).
- Improving temporal and spatial locality for better hit rates.
This approach is crucial for modern processors to balance speed, efficiency, and memory bandwidth usage.