Computer ArchitectureUnit 717 min read
Memory Hierarchy & Cache Design: Mapping, Replacement & Performance
Unit 7 of Computer Architecture explores how modern systems organize memory layers (registers → cache → RAM → disk) to balance speed, cost, and capacity, focusing on cache mapping techniques (direct, associative, set-associative), replacement policies (LRU, FIFO), and real-world trade-offs in CPU design.
TAKEAWAYS:
- Memory hierarchy is a layered design where smaller, faster layers (cache) hide the latency of larger, slower layers (RAM/disk) via locality principles (temporal/spatial).
- Cache mapping determines how main memory blocks map to cache lines: direct mapping is simple but prone to conflicts; associative mapping is flexible but expensive; set-associative offers a middle ground.
- Replacement policies (LRU, FIFO, random) decide which cache line to evict when full, directly impacting hit rates and performance.
- Cache performance is measured by hit time, miss penalty, and hit rate, with the average memory access time (AMAT) formula: .
- Write policies (write-through vs. write-back) trade off consistency for speed, with write-back reducing bus traffic but requiring dirty-bit tracking.
- Real-world systems (e.g., Intel Core i7, ARM Cortex) use multi-level caches (L1/L2/L3) with different associativities and sizes to optimize for different workloads.
1. Why Memory Hierarchy? The Speed-Cost Tradeoff
Computers need fast access to data (e.g., CPU registers) but cannot afford to store all data in fast memory (expensive). The solution: a layered hierarchy where each layer is faster but smaller and more costly than the one below it.
graph TD
A["CPU Registers\n(~1 cycle, ~KB)"] -->|"Fastest"| B["L1 Cache\n(~3-5 cycles, ~KB-MB)"]
B --> C["L2 Cache\n(~10-20 cycles, ~MB)"]
C --> D["L3 Cache\n(~30-50 cycles, ~MB-GB)"]
D --> E["Main Memory (RAM)\n(~100-300 cycles, ~GB-TB)"]
E --> F["Secondary Storage (Disk/SSD)\n(~1M+ cycles, ~TB-PB)"]
F -->|"Slowest"| G["Tertiary Storage (Tape)\n(~10M+ cycles, ~PB-YB)"]Key Principle: Locality – Programs tend to access the same data repeatedly (temporal locality) or nearby data (spatial locality). The hierarchy exploits this to keep frequently used data in faster layers.
In the Real World
WhatsApp (Meta) uses a multi-level cache hierarchy in its servers:
- L1/L2 caches store frequently accessed message metadata (e.g., chat histories of active users).
- RAM holds full message payloads for quick retrieval.
- SSDs store long-term messages, with prefetching (spatial locality) to load related messages before they’re needed.
- Example: When you open a chat, WhatsApp’s servers cache the last 100 messages in L2 and prefetch the next 50 to reduce disk I/O.
eSewa (Nepal Government) processes billions of transactions annually using a write-back cache policy in its database servers:
- L3 cache holds frequently accessed bill records (e.g., electricity/water bills for a specific ward).
- Dirty bits track modified cache lines (e.g., a bill payment update), which are written back to RAM only when evicted, reducing disk writes.
- Example: During peak hours (e.g., 6–8 PM), eSewa’s caches hit rate exceeds 95% due to temporal locality (same users paying bills repeatedly).
Daraz (Alibaba Group) uses set-associative caches in its recommendation engine:
- L1 cache stores user session data (e.g., items viewed in the last 5 minutes).
- L2 cache holds product catalog metadata (e.g., price, stock) for spatial locality (users often view related products).
- Example: When you search for a phone, Daraz’s cache preloads accessories (cases, chargers) into nearby cache sets to reduce RAM access.
2. Cache Basics: How It Works
A cache is a small, fast memory layer between the CPU and RAM. It stores copies of frequently used data to reduce average access time.
Key Components of a Cache
- Cache Lines: Fixed-size blocks (e.g., 64 bytes) transferred between RAM and cache.
- Tags: Identify which main memory block is stored in a cache line.
- Index: Determines the cache set (for set-associative caches).
- Valid Bit: Indicates if the cache line contains valid data.
- Dirty Bit: (For write-back caches) Marks if the cache line has been modified and needs writing back to RAM.
Cache Mapping: How Data Finds Its Home in Cache
The mapping function determines where a main memory block is placed in the cache. Three main types:
A. Direct Mapping
- Simplest and fastest: Each main memory block maps to exactly one cache line.
- Formula:
- Problem: Conflict misses occur when two frequently used blocks map to the same cache line.
graph LR
A["Main Memory\nBlock 0"] -->|"0 mod 8 = 0"| B["Cache Line 0"]
C["Main Memory\nBlock 8"] -->|"8 mod 8 = 0"| B
D["Main Memory\nBlock 16"] -->|"16 mod 8 = 0"| B
E["Main Memory\nBlock 1"] -->|"1 mod 8 = 1"| F["Cache Line 1"]Example (Direct Mapping Conflict):
- Suppose a cache has 8 lines (index 0–7).
- Block 0 (from RAM) maps to Cache Line 0.
- Later, Block 8 also maps to Cache Line 0 → Block 0 is evicted (even if still needed soon).
- Real-world tie-in: Imagine NTC’s billing system where two popular payment methods (e.g., Khalti and eSewa) keep evicting each other from cache, causing slowdowns.
B. Fully Associative Mapping
- Most flexible: Any main memory block can go into any cache line.
- No conflicts, but slow (requires searching all cache lines for a match).
- Used in small, high-speed caches (e.g., CPU register files).
graph TD
A["Main Memory\nBlock X"] -->|"Can go anywhere"| B["Cache Line 0"]
A --> C["Cache Line 1"]
A --> D["Cache Line 2"]
A --> E["Cache Line 3"]Example (Fully Associative Cache):
- Ncell’s customer service chatbot uses a small associative cache to store frequently asked questions (FAQs).
- When a user asks, "How to check my balance?", the system searches all cache lines for a match before checking the database.
C. Set-Associative Mapping
- Compromise: Cache is divided into sets, and each block maps to a specific set but can go into any line within that set.
- Example: A 4-way set-associative cache with 8 sets means each block maps to one of 8 sets, and within that set, it can go into any of 4 lines.
graph TD
A["Main Memory\nBlock 0"] -->|"Set 0"| B["Set 0"]
B --> C["Line 0"]
B --> D["Line 1"]
B --> E["Line 2"]
B --> F["Line 3"]
G["Main Memory\nBlock 8"] -->|"Set 1"| H["Set 1"]Example (Set-Associative Cache in Banks):
- Global IME Bank’s ATM system uses a 2-way set-associative L2 cache to store:
- Set 0: Account balances for users A–D.
- Set 1: Account balances for users E–H.
- If user A and user E access their accounts simultaneously, their data won’t conflict (unlike direct mapping).
Comparison Table: Mapping Techniques
| Feature | Direct Mapping | Fully Associative | Set-Associative |
|---|---|---|---|
| Complexity | Low | High | Medium |
| Speed | Fastest | Slowest | Medium |
| Conflict Misses | High | None | Low |
| Hardware Cost | Low | High (tag comparator) | Medium |
| Example Use Case | L1 Cache (Intel i7) | CPU Register File | L2/L3 Cache (ARM) |
3. Cache Replacement Policies: What to Evict?
When a cache is full and a new block needs to be loaded, a replacement policy decides which block to evict. Common policies:
A. Least Recently Used (LRU)
- Evicts the block not used for the longest time.
- Most effective for temporal locality.
- Implementation: Requires hardware support (e.g., a stack or timestamp for each block).
Example (LRU in Pathao’s Ride-Hailing):
- Pathao’s servers cache driver locations for a specific area.
- If the cache is full and a new driver enters the area, the least recently used driver’s location (e.g., a driver who hasn’t accepted a ride in 10 minutes) is evicted.
B. First-In-First-Out (FIFO)
- Evicts the block that arrived first.
- Simple but suboptimal (doesn’t consider usage patterns).
- Used in older systems where LRU is too costly.
Example (FIFO in NTC’s Network Routers):
- NTC’s routers cache frequent IP routes (e.g., to Kathmandu University).
- If the cache is full, the oldest route entry (even if still heavily used) is replaced.
C. Random Replacement
- Evicts a random block.
- Fast but unpredictable performance.
Example (Random Replacement in YouTube’s CDN):
- YouTube’s content delivery network (CDN) caches video chunks for users in Nepal.
- If a new chunk needs to be loaded, a random cached chunk (e.g., a less popular video segment) is evicted.
Comparison Table: Replacement Policies
| Policy | Pros | Cons | Example Use Case |
|---|---|---|---|
| LRU | High hit rate, adaptive | Complex hardware | Modern CPUs (Intel/ARM) |
| FIFO | Simple, no usage tracking | Poor performance | Legacy systems, routers |
| Random | Fast, no tracking needed | Unpredictable misses | CDNs (YouTube, Netflix) |
4. Write Policies: How to Handle Cache Writes
When the CPU writes to a cached block, the system must decide whether to update the cache, RAM, or both.
A. Write-Through
- Writes to both cache and RAM immediately.
- Pros: Always consistent (no stale data).
- Cons: Slower (extra RAM write), more bus traffic.
Example (Write-Through in Banks):
- When you transfer money via an ATM, the transaction is written to both the ATM’s cache and the bank’s main database immediately.
- Downside: Slower transactions during peak hours (e.g., 10 AM–12 PM).
B. Write-Back (Copy-Back)
- Writes only to cache; RAM is updated only when the block is evicted.
- Pros: Faster, less bus traffic.
- Cons: Risk of data loss if cache is cleared (requires dirty bit).
Example (Write-Back in eSewa):
- When you pay a bill, eSewa’s server marks the cache line as "dirty" and updates RAM only when the block is evicted (e.g., after 10 minutes of inactivity).
- Advantage: Reduces disk I/O by ~70% during high traffic.
Comparison Table: Write Policies
| Policy | Cache Write | RAM Write | Consistency | Performance | Example Use Case |
|---|---|---|---|---|---|
| Write-Through | Yes | Immediate | Always | Slower | ATM transactions (banks) |
| Write-Back | Yes | On evict | Needs dirty bit | Faster | eSewa, WhatsApp servers |
5. Cache Performance Metrics
To evaluate cache effectiveness, we use:
A. Hit Time (t_h)
- Time to access a block if it’s in cache.
- Goal: Minimize (e.g., 1–3 CPU cycles for L1).
B. Miss Rate (MR)
- Fraction of memory accesses that miss the cache.
- Goal: Minimize (e.g., L1 hit rate > 95%).
C. Miss Penalty (t_m)
- Time to fetch a block from the next level (e.g., RAM).
- Goal: Reduce (e.g., 100–300 cycles for RAM).
D. Average Memory Access Time (AMAT)
Example Calculation:
- Suppose:
- cycles (L1 cache hit time).
- .
- cycles (RAM access time).
- Then:
Real-World Example (Intel Core i7):
- L1 Cache: cycle, , cycles (L2).
- L2 Cache: cycles, , cycles (RAM).
6. Cache Design Tradeoffs
| Parameter | Impact on Performance | Real-World Example |
|---|---|---|
| Cache Size | Larger = fewer misses | ARM Cortex-A76 (L1: 32KB, L2: 256KB) |
| Associativity | Higher = fewer conflicts | Intel i9 (L3: 16-way set-associative) |
| Block Size | Larger = better spatial locality | Modern CPUs: 64-byte cache lines |
| Write Policy | Write-back = faster but complex | eSewa uses write-back to reduce I/O |
7. Advanced: Multi-Level Caches
Modern CPUs use multiple cache levels to optimize for different workloads:
graph TD
A["CPU"] --> B["L1 Cache\n(Private, Small, Fast)\n32KB-64KB"]
A --> C["L2 Cache\n(Private/Shared, Medium, Slower)\n256KB-1MB"]
A --> D["L3 Cache\n(Shared, Large, Slowest)\n1MB-64MB"]
B -->|"Miss"| C
C -->|"Miss"| D
D -->|"Miss"| E["Main Memory (RAM)"]Example (Intel Core i7-12700K):
- L1: 32KB (8-way associative), cycle.
- L2: 1MB (private per core), 12-way associative.
- L3: 20MB (shared), 16-way associative.
8. Real Hardware: A Look Inside a CPU Cache
classDiagram
class CacheLine {
+Tag: 20 bits
+Index: 6 bits
+Valid: 1 bit
+Dirty: 1 bit
+Data: 64 bytes
}
class CacheSet {
+Lines: CacheLine[]
}
class Cache {
+Sets: CacheSet[]
+Mapping: Direct/Associative/Set-Associative
}
Cache "1" --> "*" CacheSet : contains
CacheSet "1" --> "*" CacheLine : contains9. Worked Example: Cache Hit/Miss Analysis
Scenario: A direct-mapped cache with:
- Cache size: 16 blocks (index 0–15).
- Block size: 64 bytes.
- Main memory: 256 blocks (addresses 0–255).
- Access sequence: 0, 16, 32, 48, 64, 80, 96, 112, 128, 144, 160, 176, 192, 208, 224, 240.
Step 1: Determine mapping.
- Cache line = (Memory Block) mod 16.
- Example:
- Block 0 → Line 0 (0 mod 16).
- Block 16 → Line 0 (16 mod 16 = 0).
- Block 32 → Line 0 (32 mod 16 = 0).
Step 2: Simulate accesses.
| Block | Cache Line | Hit/Miss | Action |
|---|---|---|---|
| 0 | 0 | Hit | Load into Line 0 |
| 16 | 0 | Miss | Evict Block 0, load 16 |
| 32 | 0 | Miss | Evict Block 16, load 32 |
| 48 | 0 | Miss | Evict Block 32, load 48 |
| 64 | 0 | Miss | Evict Block 48, load 64 |
| ... | ... | ... | ... |
Result: All accesses miss because they all map to Cache Line 0 (conflict misses). Hit Rate: 0%.
Real-World Tie-In:
- This is like Kathmandu traffic where all routes lead to the same bottleneck (e.g., Thapathali–Kageshwori road). A set-associative cache would distribute blocks across multiple lines, reducing conflicts.
10. Exam Tip: How to Score Full Marks
Diagrams are mandatory:
- Always draw block diagrams for cache mapping (direct/associative/set-associative).
- Label tags, indices, valid/dirty bits, and data arrays.
- Example: For a 4-way set-associative cache with 8 sets, show 8 sets, each with 4 lines.
Define terms precisely:
- Conflict miss: Miss due to mapping (not capacity).
- Capacity miss: Miss because cache is full.
- Cold-start miss: First access to a new block.
Compare mapping techniques:
- Use a table (as above) to highlight trade-offs (speed vs. complexity).
Calculate AMAT:
- Always show the formula and plug in numbers.
- Example: If , , , then .
Relate to real systems:
- Mention Intel/ARM caches, banking systems (eSewa), or e-commerce (Daraz) in explanations.
- Example: "Like how Daraz prefetches related products into cache, a set-associative cache reduces conflict misses by distributing blocks across multiple lines."
Common pitfalls:
- Direct mapping: Forgetting to mention conflict misses.
- LRU: Not explaining how it requires hardware support (e.g., timestamps).
- Write-back: Forgetting to mention the dirty bit.
Past Exam Questions Covered in This Note
| Question | Key Topics Addressed |
|---|---|
| Define associative memory and explain with a block diagram. | Fully associative mapping, tag comparison. |
| Advantages/disadvantages of direct vs. associative mapping. | Conflict misses, hardware cost, hit rate. |
| Explain Direct Memory Access (DMA) with a diagram. | (Covered in Unit 8, but cache mapping is similar in layered design.) |
| What is cache memory? Explain mapping and differentiate direct vs. associative. | All mapping types, diagrams, trade-offs. |
| What are the main characteristics of a memory system? | Hierarchy, locality, speed-cost tradeoff. |
Final Summary
- Memory hierarchy exploits locality to reduce average access time.
- Cache mapping (direct/associative/set-associative) trades off speed vs. flexibility.
- Replacement policies (LRU/FIFO/random) impact hit rates.
- Write policies (write-through/write-back) balance consistency vs. speed.
- AMAT is the key metric: .
Remember: In exams, draw diagrams, compare techniques, and tie concepts to real systems (eSewa, Daraz, Intel CPUs).
Based on the TU BSc CSIT syllabus for Computer Architecture (CSC213), unit 7.
Discussion
Loading…