1.1.9How Computers Work

Memory hierarchy — registers, cache (L1 - L2 - L3), RAM, SSD, HDD — speed - size trade-offs

2,177 words10 min readdifficulty · medium6 backlinks

WHAT is the memory hierarchy?

The levels, fastest → slowest:

Level Typical size Approx. access time Volatile?
Registers ~1–2 KB (few dozen × 64-bit) ~0.3 ns (<1 cycle) yes
L1 cache ~32–64 KB ~1 ns (~4 cycles) yes
L2 cache ~256 KB–1 MB ~4 ns (~12 cycles) yes
L3 cache ~8–32 MB (shared) ~10–20 ns (~40 cycles) yes
RAM (DRAM) ~8–64 GB ~100 ns yes
SSD ~256 GB–4 TB ~100,000 ns (100 µs) no
HDD ~1–16 TB ~10,000,000 ns (10 ms) no
Figure — Memory hierarchy — registers, cache (L1 - L2 - L3), RAM, SSD, HDD — speed - size trade-offs

WHY the trade-off exists (first principles)


HOW it actually works: locality + caching

Because of locality, the CPU brings data into cache in chunks called cache lines (typically 64 bytes), not single bytes. A request that's already in cache is a hit; otherwise it's a miss and must be fetched from a slower level.

Deriving the Average Memory Access Time (AMAT)

We want the expected time to access memory across a program. WHY derive it? Because it tells us exactly how much a low hit rate hurts.

Let:

  • hh = hit rate at a level (fraction of accesses found there), 0h10\le h\le 1
  • thitt_{\text{hit}} = time if it's a hit at this level
  • TlowerT_{\text{lower}} = average time to get it from the next (slower) level if we miss

By definition of expected value, with probability hh we pay thitt_{\text{hit}}, and with probability (1h)(1-h) we additionally pay to go down a level:

AMAT=hthit+(1h)(thit+Tlower)\text{AMAT} = h\cdot t_{\text{hit}} + (1-h)\big(t_{\text{hit}} + T_{\text{lower}}\big)

Expand and simplify (the thitt_{\text{hit}} is paid in both cases):

AMAT=thit+(1h)Tlower\boxed{\text{AMAT} = t_{\text{hit}} + (1-h)\cdot T_{\text{lower}}}


Worked examples


Common mistakes (Steel-manned)


Recall Feynman: explain to a 12-year-old (click to reveal)

Imagine doing homework. Your hands hold the pencil you’re using right now (registers). Your desk has the books you need this hour (cache). Your bookshelf across the room has more books (RAM). The library downtown has every book but takes a trip (SSD/HDD). You keep the most-used stuff on the desk so you barely ever walk to the library. The computer does the same: it keeps the data it uses most in the closest, fastest spot, so it almost never has to make the slow trip.


Active recall

Order the hierarchy fastest to slowest
Registers → L1 → L2 → L3 cache → RAM → SSD → HDD
Why is fast memory always small and expensive?
Fast cells (SRAM) use ~6 transistors/bit and must sit physically near the core (signal-distance limit), so cost and area limit capacity.
Derive AMAT
With hit prob h pay t_hit; with miss prob (1−h) pay t_hit + T_lower. AMAT = h·t_hit + (1−h)(t_hit + T_lower) = t_hit + (1−h)·T_lower.
What is temporal locality?
Recently used data is likely to be used again soon (e.g., loop variables).
What is spatial locality?
Data near a recently accessed address is likely to be accessed soon (e.g., array elements).
What is a cache line?
The fixed-size block (~64 bytes) the CPU transfers between memory levels, exploiting spatial locality.
Cache hit vs miss?
Hit = data found in this fast level; miss = not found, must fetch from a slower level (paying the miss penalty).
Which levels are volatile?
Registers, all cache levels, and RAM lose data on power-off; SSD and HDD are non-volatile.
Two-level AMAT with t_L1=1ns, h=0.95, RAM=100ns
1 + 0.05·100 = 6 ns.
Why does more RAM not directly speed up the CPU?
CPU speed depends on cache hit rates; extra RAM only helps when you were swapping to disk.
RAM vs SSD key difference?
RAM is volatile working memory (~100ns); SSD is non-volatile persistent storage (~100µs), ~1000× slower.
Why fetch 64 bytes instead of 1 byte on a miss?
Spatial locality — nearby data will likely be needed, so amortize the slow fetch over many useful bytes.

Connections

  • CPU and instruction cycle — registers feed the ALU each cycle
  • Cache coherence and multicore — why L3 is shared
  • Locality of reference — the principle making the hierarchy work
  • Virtual memory and paging — extending RAM onto SSD/HDD
  • Big-O vs constant factors — cache-friendly code beats theoretically equal algorithms
  • Volatile vs non-volatile storage

Concept Map

forces stacking

fast means small

fast means expensive

fastest tiny

small fast

large slower

huge slow

makes hierarchy work

fetched as

found gives

absent gives

combine into

adds penalty to

Speed-Size-Cost trade-off

Memory Hierarchy

Physics of distance

Cell cost SRAM DRAM Flash

Registers

Cache L1 L2 L3

RAM DRAM

SSD then HDD

Locality of reference

Cache lines 64B

Hit fast

Miss fetch slower

Average Memory Access Time

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, computer ko ek dard hai: jo memory fast hoti hai wo choti aur mehngi hoti hai, aur jo sasti aur badi hoti hai wo slow hoti hai. Teeno cheezein ek saath nahi mil sakti. Isliye engineers ek pyramid banate hain — CPU ke bilkul paas thodi si super-fast memory (registers, fir L1/L2/L3 cache), uske baad RAM, fir SSD, aur sabse door HDD. Jaise jaise CPU se door jaate ho, memory badi hoti jaati hai par slow hoti jaati hai.

Yeh kaam isliye chalta hai kyunki programs mein locality hoti hai — jo data abhi use kiya wahi thodi der baad phir chahiye hota hai (temporal), aur uske aas-paas ka data bhi chahiye hota hai (spatial). Isliye CPU 64 byte ki cache line ek saath utha leta hai, ek-ek byte nahi. Zyada tar baar data fast cache mein mil jaata hai (hit), aur kabhi-kabhi slow level tak jaana padta hai (miss).

Iska maths simple hai: AMAT=thit+(1h)×Tlower\text{AMAT} = t_{hit} + (1-h)\times T_{lower}. Matlab fast level ka time toh hamesha lagta hai, aur sirf miss wale fraction (1h)(1-h) ko extra slow penalty bharni padti hai. Example: L1 = 1 ns, hit rate 95%, RAM 100 ns → AMAT = 1 + 0.05×100 = 6 ns. Sirf 95% hit se hi 100 ns ka kaam 6 ns mein ho gaya!

Yeh matter isliye karta hai kyunki fast code likhne ka raaz aksar cache-friendly hona hota hai. Do algorithm ka Big-O same ho sakta hai, par jo array sequentially access karta hai (spatial locality) wo linked-list wale se bahut tez chalega. Aur yaad rakho: zyada RAM lagane se CPU automatically tez nahi hota — wo tabhi help karta hai jab aap pehle disk pe swap kar rahe the.

Test yourself — How Computers Work

Connections