Modern processors use multiple levels of cache (L1, L2, L3, and sometimes L4) that work together to bridge the massive speed gap between CPU cores and main memory. Here's how these caches cooperate to deliver both high performance and efficient memory usage.
Overview of Cache Hierarchy
A typical modern CPU memory hierarchy looks like this:
code
CPU Core → L1-I Cache → L2 Cache → L3 Cache → Main Memory (DRAM)
→ L1-D Cache ↗ ↗
(shared across cores)
Typical characteristics:
L1 Cache: 32-64 KB per core, 1-4 cycle latency, split into instruction (I) and data (D)
When data moves from L3→L2→L1, it's removed from the previous level
Advantages: Maximum effective cache capacity (no duplication)
Disadvantages: More complex data movement, harder coherence tracking
Example: AMD Zen architecture L3 cache
3. Non-Inclusive Non-Exclusive (NINE)
No strict relationship between levels
Data may or may not be duplicated
Advantages: Flexible, good performance
Disadvantages: Most complex to implement
Example: Intel Skylake and newer
Data Movement and Cooperation
Cache Hit Path
When the CPU requests data:
Check L1 (1-4 cycles): If hit, return data immediately
Check L2 (10-20 cycles): If hit, return data to CPU and potentially promote to L1
Check L3 (30-50 cycles): If hit, return data and potentially promote to L2/L1
Main Memory (100-300 cycles): Fetch from DRAM, fill into cache hierarchy
Cache Fill Policies
When data is fetched from memory:
Demand Fill: Fill only the requested cache line
Prefetch Fill: Speculatively fetch adjacent lines
Critical Word First: Return requested word immediately while filling rest of line
Write Policies
How caches handle writes affects cooperation:
Write-Through: Write to current level and all lower levels simultaneously
Write-Back: Write only to current level, mark as dirty, write to lower levels on eviction
Most modern caches use write-back with write-allocate
Coherence in Multi-Core Systems
Cache coherence protocols ensure all cores see a consistent view of memory:
MESI Protocol (and variants like MOESI, MESIF)
Each cache line has a state:
M (Modified): Only this cache has the line, and it's dirty
E (Exclusive): Only this cache has the line, but it's clean
S (Shared): Multiple caches may have this line (read-only)
I (Invalid): Line is not valid in this cache
Example coherence flow:
code
Core 0 reads X → L1 miss → L2 miss → L3 hit (state S) → Fill L2, L1
Core 1 reads X → L1 miss → L2 miss → L3 hit (state S) → Both cores now share
Core 0 writes X → Invalidate Core 1's copy → Core 0's line becomes M
Replacement Policies
When caches are full, they must decide what to evict:
Snooping: Caches monitor bus transactions to maintain coherence
Directory-based coherence: Centralized tracking of cache line locations
Write combining buffers: Merge multiple writes before sending downstream
Fill buffers: Queue requests between cache levels
Interconnect networks: Rings, meshes, or crossbars connecting cache levels
The beauty of multi-level caches is that they work together transparently, presenting a simple memory model to software while delivering orders-of-magnitude performance improvements through clever cooperation and data movement strategies.