Course contentsShow
← Computer Science
Lesson 1132 of 2,87226. Memory Hierarchy and CachingFree lesson

Set-Associative Caches

Discover how set-associative caches balance direct-mapped and fully associative approaches using multiple ways.

Set-Associative Caches

What you'll learn: How set-associative caches strike a balance between the simplicity of direct-mapped caches and the flexibility of fully associative caches.

The Middle Ground

You've seen two extremes: direct-mapped caches where each memory address maps to exactly one cache location (simple but prone to conflict misses), and fully associative caches where data can go anywhere (flexible but requires expensive parallel search hardware). Set-associative caches split the difference.

How Set-Associative Caches Work

A set-associative cache divides the cache into multiple sets, and each set contains multiple ways (cache lines). When you look up an address:

  1. The address maps to a specific set (like direct-mapped)
  2. Within that set, data can reside in any of the ways (like associative)

Think of it like a filing cabinet with drawers (sets), where each drawer has several folders (ways). The document ID tells you which drawer to open, but once there, you check a few folders instead of just one.

The N-Way Terminology

A 4-way set-associative cache means each set has 4 cache lines. An 8-way has 8 lines per set. More ways = fewer conflict misses but slightly more complex hardware.

Why This Design Works

  • Fewer conflicts than direct-mapped: Multiple addresses mapping to the same set won't always collide—they can coexist in different ways
  • Cheaper than fully associative: You only search within one set (a few ways) instead of the entire cache
  • Practical performance: Most real CPUs use 4-way, 8-way, or 16-way set-associative caches in their L1, L2, and L3 levels

Key Takeaway: Set-associative caches provide a practical compromise by grouping cache lines into sets with multiple ways, reducing conflict misses while keeping lookup hardware manageable.