Computer Hardware DesignUnit 512 min read
Memory Hierarchy: Caches, RAM, Storage & Speed Tradeoffs
Unit 5 of Computer Hardware Design explores how computers balance speed and capacity using a layered memory hierarchy (registers → caches → RAM → SSDs → HDDs), covering cache mapping (direct, associative, set-associative), cache performance metrics (hit rate, miss penalty), and real-world optimizations like prefetching
Why Memory Hierarchy Matters
Computers need fast access to data but cheap storage for large datasets. A single memory technology (e.g., only RAM) cannot satisfy both needs:
- Speed: Registers (1–4 cycles) > L1 cache (3–5 cycles) > L2/L3 (10–40 cycles) > RAM (100+ cycles) > SSD (microseconds) > HDD (milliseconds).
- Cost: 1 GB of RAM costs ~$10, while 1 GB of SSD costs ~$0.10. Tradeoffs are inevitable.
classDiagram
class MemoryHierarchy {
+Speed ↑ (faster at top)
+Cost ↓ (cheaper at bottom)
+Layers: Registers → L1 → L2 → L3 → RAM → SSD → HDD
}
class Cache {
+Types: Direct-Mapped / Associative / Set-Associative
+Policies: Write-Through / Write-Back
+Metrics: Hit Rate / Miss Rate / Miss Penalty
}
MemoryHierarchy "1" *-- "5" Cache : contains1. The Memory Hierarchy Layers
The goal: keep frequently used data closer to the CPU to minimize access time.
| Layer | Technology | Speed (cycles) | Size (typical) | Cost per GB | Example Use Case |
|---|---|---|---|---|---|
| Registers | CPU internal | 1–4 | KB (32–256) | Very high | Loop counters, ALU inputs |
| L1 Cache | SRAM (static RAM) | 3–5 | 32–256 KB | High | Instruction fetch, data ops |
| L2/L3 Cache | SRAM | 10–40 | 256 KB–8 MB | Medium | Prefetching, branch prediction |
| Main RAM | DRAM (DDR4/DDR5) | 100+ | 4–64 GB | Low | Running programs, OS data |
| SSD | NAND Flash | ~10,000 (µs) | 256 GB–4 TB | Very low | OS storage, databases |
| HDD | Magnetic disks | ~1,000,000 (ms) | 500 GB–10 TB | Lowest | Archival storage, backups |
Key Idea: Higher layers are smaller but faster; lower layers are larger but slower. The CPU uses copy-on-write and prefetching to move data up the hierarchy.
2. How Caches Work: Mapping Strategies
Caches store copies of frequently used RAM blocks. The cache mapping determines where a RAM block can be stored.
A. Direct-Mapped Cache
- Definition: Each RAM block maps to exactly one cache line (determined by
block_address % number_of_lines). - Pros: Simple, fast lookup.
- Cons: Conflict misses (two frequently used blocks map to the same cache line).
Worked Example (Direct-Mapped):
Assume a 4-block cache (lines 0–3) and RAM blocks B0, B1, B2, B3, B4.
- Access sequence:
B0 → B1 → B2 → B3 → B4 → B0 - Misses:
B0, B1, B2, B3, B4(all first accesses). - Conflict Miss: When
B4maps to line 0, it evictsB0(even thoughB0is still needed).
B. Fully Associative Cache
- Definition: A RAM block can go anywhere in the cache.
- Pros: No conflict misses; higher hit rate.
- Cons: Slow lookup (requires searching all cache lines).
Worked Example (Fully Associative):
Same sequence B0 → B1 → B2 → B3 → B4 → B0:
- All blocks fit in cache (no evictions).
- Hit Rate = 5/6 ≈ 83% (vs. 0% in direct-mapped due to conflict).
C. Set-Associative Cache (Best of Both Worlds)
- Definition: Cache is divided into sets; each block maps to a specific set but can replace any line in that set.
- Example: 4-way set-associative with 4 sets → 16 cache lines (4 lines/set).
Worked Example (Set-Associative): Assume 2-way set-associative cache with 2 sets (4 lines total).
- Access sequence:
B0 → B2 → B4 → B6 → B0 B0maps to Set 0,B2to Set 0 → conflict in Set 0.- Solution: Replace one line in Set 0 (e.g., LRU eviction).
3. Cache Performance Metrics
| Metric | Definition | Formula | Example |
|---|---|---|---|
| Hit Rate | % of accesses found in cache. | Hits / (Hits + Misses) |
95% hit rate → 1 miss per 20 accesses |
| Miss Rate | % of accesses not in cache. | 1 – Hit Rate |
5% miss rate → 1 miss per 20 accesses |
| Miss Penalty | Time to fetch a block from next level. | Time(RAM) – Time(Cache) |
100 cycles (RAM) – 5 cycles (L1) = 95 cycles |
| AMAT | Average Memory Access Time. | Hit Time + Miss Rate × Miss Penalty |
5 + 0.05 × 100 = 10 cycles |
Worked Example (AMAT Calculation):
- L1 Cache: Hit time = 5 cycles, Miss rate = 5%, Miss penalty = 100 cycles.
- AMAT = 5 + (0.05 × 100) = 10 cycles.
4. Cache Write Policies
When the CPU writes to a cache line, how is the lower-level memory updated?
| Policy | Definition | Pros | Cons | Example Use Case |
|---|---|---|---|---|
| Write-Through | Write to cache and RAM immediately. | No data inconsistency. | Slower writes (RAM bottleneck). | Embedded systems (low power). |
| Write-Back | Write only to cache; update RAM later. | Faster writes (no RAM delay). | Risk of data loss if cache flushed. | Desktops/laptops (performance). |
Worked Example (Write-Back):
- CPU writes to
Cache[0](value = 10). Cache[0]is modified but RAM is not updated yet (dirty bit = 1).- When
Cache[0]is evicted, it writes back to RAM (saving time).
5. Cache Coherence (Multi-Core Systems)
In multi-core CPUs, each core has its own cache. How do we ensure all caches have consistent data?
MESI Protocol (Mostly Used)
| State | Meaning | Action on Read | Action on Write |
|---|---|---|---|
| M | Modified (dirty, not in RAM) | Send data to requester. | Keep modified. |
| E | Exclusive (clean, only in this cache) | Grant read. | Transition to M. |
| S | Shared (clean, in other caches too) | Grant read. | Transition to M/E. |
| I | Invalid (stale) | Fetch from RAM. | Transition to M/E. |
Example (MESI in Action):
- Core 1 writes to
Cache[5]→ state M. - Core 2 reads
Cache[5]→ Core 1 sends data; Core 2’s cache line becomes S. - Core 1 modifies
Cache[5]again → invalidates Core 2’s S state.
6. Real-World Applications of Memory Hierarchy
A. eSewa (Nepal’s Digital Payment System)
- Problem: Millions of transactions per second → RAM bottlenecks.
- Solution:
- L1/L2 Caches: Store frequently accessed user profiles (e.g.,
user_id → balance). - SSD Prefetching: Predict next transactions (e.g., if user A pays electricity, prefetch their phone bill data).
- Write-Back Caches: Reduce RAM write latency for payment logs.
- L1/L2 Caches: Store frequently accessed user profiles (e.g.,
B. Pathao (Ride-Hailing App)
- Problem: Real-time driver-location updates → high I/O load.
- Solution:
- Multi-Level Caches: L1 for active drivers, L2 for nearby drivers, RAM for all drivers.
- Set-Associative Mapping: Avoid conflict misses for hotspots (e.g., Thapathali, Lakshmi Path).
- Write-Through for Safety: Critical updates (e.g., ride cancellations) go to RAM immediately.
C. NEPSE (Nepal Stock Exchange)
- Problem: High-frequency trading → microsecond delays matter.
- Solution:
- L3 Cache: Stores recent stock prices (e.g., NABIL, NMB, NTC).
- Prefetching: Predicts next trades based on historical patterns.
- Non-Uniform Memory Access (NUMA): Distributes data across multiple RAM banks for parallel processing.
7. Exam Tip: How This Unit is Tested
- Definitions & Comparisons (3–5 marks):
- Differentiate direct-mapped vs. associative caches.
- Explain write-through vs. write-back with examples.
- Calculations (5–10 marks):
- Given a cache size, block size, and access pattern, calculate hit rate, miss rate, AMAT.
- Example:
"A 16 KB, 4-way set-associative cache has 64-byte blocks. If the access sequence is
B0, B16, B32, B48, B0, what is the miss rate?"
- Real-World Scenarios (5 marks):
- Relate cache optimizations to eSewa, Pathao, or NEPSE.
- Example:
"How would you improve the performance of a ride-hailing app’s driver-location system using memory hierarchy?"
- Diagrams (5 marks):
- Draw a direct-mapped cache with given mappings.
- Show a MESI state transition for a write operation.
Common Pitfalls:
- Forgetting to include miss penalty in AMAT calculations.
- Confusing set-associative with fully associative in mapping.
- Ignoring eviction policies (LRU vs. FIFO) in worked examples.
8. Key Formulas to Memorize
| Concept | Formula |
|---|---|
| Cache Hit Rate | Hits / (Hits + Misses) |
| Miss Rate | 1 – Hit Rate |
| AMAT | Hit Time + Miss Rate × Miss Penalty |
| Cache Lines | Cache Size / Block Size |
| Sets in Set-Associative | Cache Lines / Associativity |
9. Worked Example: Cache Simulation
Problem:
A direct-mapped cache has 4 lines (0–3). RAM blocks are accessed in this order:
B0, B4, B8, B12, B0, B4, B8, B12
Assume blocks map to cache lines as: Bx → line (x mod 4).
Steps:
- B0 → Miss (load to line 0).
- B4 → Miss (load to line 0) → evicts B0.
- B8 → Miss (load to line 0) → evicts B4.
- B12 → Miss (load to line 0) → evicts B8.
- B0 → Miss (load to line 0) → evicts B12.
- B4 → Hit (in line 0).
- B8 → Hit (in line 0).
- B12 → Hit (in line 0).
Results:
- Hits: 3 (
B4, B8, B12in steps 6–8). - Misses: 5.
- Hit Rate:
3/8 = 37.5%. - Miss Rate:
5/8 = 62.5%.
Improvement: Use 2-way set-associative to reduce conflict misses.
10. Visual Summary: Memory Hierarchy in Action
flowchart LR
subgraph CPU["CPU Core"]
L1["L1 Cache\n(3-5 cycles)"] -->|"Hit"| CPU
L1 -->|"Miss"| L2["L2 Cache\n(10-40 cycles)"]
L2 -->|"Miss"| RAM["Main RAM\n(100+ cycles)"]
RAM -->|"Miss"| SSD["SSD\n(µs)"]
SSD -->|"Miss"| HDD["HDD\n(ms)"]
end
label "Faster\nAccess" -->> CPU
label "Slower\nAccess" -->> HDD11. Real Picture: Inside a Modern CPU Cache
12. Real Picture: SSD vs. HDD Speed
13. Final Checklist for Exams
✅ Can you draw a direct-mapped cache with given mappings? ✅ Can you calculate AMAT given hit/miss rates and penalties? ✅ Can you explain MESI states for cache coherence? ✅ Can you compare write-through vs. write-back with pros/cons? ✅ Can you relate memory hierarchy to eSewa/Pathao/NEPSE?
Based on the TU BSc CSIT syllabus for Computer Hardware Design, unit 5.
Discussion
Loading…