Course contentsShow
← Embedded Systems & Computer Architecture
Lesson 143 of 9362. Instruction Set Architecture and CPU MicroarchitectureFree lesson

Designing Multi-Level Cache Hierarchies

Structure L1, L2, and L3 cache levels to balance fast local access with the capacity required to avoid main memory bottlenecks.

In this lesson, you will learn how to design a multi-level cache hierarchy to reconcile the conflicting needs for lightning-fast memory access and large storage capacity.

The Memory Paradox

We have previously established that a single cache cannot be both tiny (to stay fast) and massive (to hold large datasets). If we make the cache too large, the physical wiring causes latency to spike, negating the speed benefits of the CPU. To solve this, we use a tiered hierarchy: L1, L2, and L3 caches.

The Tiered Approach

Think of this hierarchy like a professional desk:

  • L1 Cache (The Workspace): Built directly into the processor core. It is tiny and extremely fast (often 1–4 clock cycles). Its goal is to provide instructions and data immediately to the pipeline.
  • L2 Cache (The Bookshelf): Larger than L1 but slightly further away. It catches the "misses" from L1. It is slower (10–20 cycles) but keeps more data local to the core.
  • L3 Cache (The Office Library): Shared across multiple cores. It is much larger and significantly slower (50+ cycles), but it prevents the CPU from having to fetch data from the very slow main memory (DRAM).

Why Multiple Levels?

The key is to keep the "working set"—the data the processor is currently manipulating—as close to the CPU as possible. If a load operation fails to find data in the L1 (an L1 miss), it looks to L2. If it misses again, it checks L3.

// Conceptual hit/miss flow
if (data_in_L1) {
    return data; // Fast hit: ~2 cycles
} else if (data_in_L2) {
    move_to_L1(data);
    return data; // Medium hit: ~15 cycles
} else if (data_in_L3) {
    move_to_L2(data);
    return data; // Slow hit: ~50 cycles
} else {
    fetch_from_DRAM(data); // Main memory bottleneck: ~200+ cycles
}

By adding levels, we effectively filter requests. Most memory accesses are satisfied by the L1 and L2, meaning the high-latency main memory is accessed only as a last resort. This hierarchy maintains the high performance of the pipeline while ensuring the processor doesn't starve for data.

Key Takeaway: A multi-level cache hierarchy uses small, fast caches for immediate access and larger, slower caches to catch misses, collectively minimizing the performance penalty of slow main memory.