03 CPU Cache Write Policies: Through, Back & More

Mustafa NJ

CPU Cache Essentials

When the CPU reads something from the cache, it always gets the correct and most up-to-date data from the memory address it asked for whether that data was already in the cache or had to be fetched.

But things get a bit tricky when it comes to writing. If the CPU updates a piece of data in the cache, the main memory might still be holding the older version, which can cause problems if other parts of the system rely on that outdated information.

Cache Coherence Problem

This is known as the cache coherence problem. The main issue with stale data in memory is that other components in the system might access this outdated information, leading to incorrect results. This problem is even more critical in multi-core and multi-processor systems, where each core or processor has its own cache, but they all connect to the same main memory.

As a result, if one core modifies data in its cache without updating main memory or notifying other caches, other cores might continue reading the outdated data directly from memory or from their own caches. To address this issue, caches use specific write policies suited to system needs. Cache write policies cover two scenarios: write hits and write misses.

In a write hit scenario, the requested address is already in the cache. When the CPU sends new data to the cache, the cache controller updates the data at the specified address. At this point, however, the cache and main memory are inconsistent.

Cache Write Policies Overview

To manage this, the cache follows one of two policies:

  • Immediate update: The cache updates main memory immediately, restoring consistency.
  • Deferred update: The cache delays the main memory update until a later time, such as during eviction or a cache flush.

In a write miss scenario, the modified address is not found in the cache. The system must then choose between allocating cache space and updating data there or writing directly to memory.

Write-Through Policy

In the write-through cache policy, any data written to the cache is immediately written to main memory as well. This immediate update keeps the cache and main memory synchronized at all times, reducing the complexity of tracking modified data in the cache.

Advantages:

  • Simplified data tracking.
  • Maintains consistency.

Disadvantages:

  • Slower write operations due to dual writes.
  • Increased traffic on the memory bus, possibly affecting other components.

Write-Back Policy

In the write-back policy, when data is modified, it is initially written only to the cache. The main memory is updated later, typically when the data is evicted or during specific events like a cache flush.

Benefits:

  • Faster cache writes.
  • Reduced main memory traffic.
  • More efficient bandwidth use.

However, this approach adds complexity:

  • Requires dirty bit tracking for each cache line to monitor modifications.
  • Carries a risk of data loss in case of power failure before flushing to main memory.

Dirty Bit Explained:

  • 0: No modification since last load.
  • 1: Data modified and differs from main memory.

On eviction, if dirty bit = 1, data is written back to main memory; otherwise, it’s discarded. During a cache flush, all dirty lines are updated in memory and marked as clean.

Write-Allocate Policy

This policy applies during write misses:

  • Allocates space in the cache.
  • Loads the data from main memory.
  • Writes the new data in the cache.

Depending on the write policy:

  • With write-through, the data is immediately written to memory.
  • With write-back, the dirty bit is set, and the update to memory is delayed.

Use Case: Write-allocate assumes the new data will be accessed again. This is effective for repeated access patterns but leads to inefficiency and cache pollution if the data is not reused.

No-Write-Allocate Policy

Also triggered by a write miss, but unlike write-allocate:

  • It does not load the data into cache.
  • It writes directly to main memory, bypassing the cache entirely.

Advantages:

  • Avoids cache pollution.
  • Saves time on unnecessary cache fetches.

Disadvantages:

  • If the data is accessed again, it leads to a cache miss, degrading performance.

Use Case: Ideal for systems where data is typically written once and not reused.

Common Policy Combinations

Different systems combine write policies depending on their goals:

  • Write-Back + Write-Allocate: Maximizes cache utilization, reduces memory bus traffic. Common in CPU L1 data caches, where performance and frequent read-write operations are critical.
  • Write-Through + No-Write-Allocate: Ensures high consistency, avoids caching rarely reused data. Suitable for logging systems or similar applications with write-once data patterns.

Final Thoughts

Understanding and implementing the right cache write policy is crucial for balancing performance, memory traffic, and consistency. While write-back with write-allocate is favored in performance-driven systems, write-through with no-write-allocate is ideal for scenarios where consistency and minimal cache pollution matter more.

Choosing the correct combination depends on the system’s workload and design priorities. By grasping how each strategy works, developers and architects can better design systems that make the most efficient use of their cache and memory resources.

Post a Comment