Courses / Computer Science I
Introduction to Computers

Cache and the Memory Hierarchy

Computer Science I 313 words Free to read

The Pyramid of Storage

There is a brutal trade-off in memory: fast storage is small and expensive, while large storage is cheap and slow. No single technology is both fast and huge, so computers use a memory hierarchy — a stack of layers, fast-and-small at the top, slow-and-large at the bottom.

LevelSpeedSizeCost per byte
RegistersFastestBytesHighest
Cache (L1, L2, L3)Very fastKB–MBHigh
Main memory (RAM)FastGBMedium
Disk / SSDSlowTBLow

A cache is a small, fast memory that sits between the CPU and main memory and holds copies of recently or frequently used data. When the CPU needs a value, it checks the cache first. A cache hit (the value is there) is fast; a cache miss forces a slow trip to main memory — and the fetched block is then copied into the cache for next time.

Caches work because real programs exhibit locality of reference. Temporal locality: a value used now is likely to be used again soon (so keep it close). Spatial locality: if one address is used, nearby addresses are likely to be used soon (so caches fetch a whole block at once, not one byte). The fraction of accesses that hit the cache is the hit rate, and because a miss is so much slower than a hit, even a small drop in hit rate can badly hurt performance.

Common pitfall: imagining the cache stores different data than main memory. It does not — a cache holds fast copies of data that also lives in main memory. Its value is purely speed: keeping the data the program is likely to touch next in the layer closest to the CPU.

Practise this lesson

The explanation above is free to read. The graded practice for this lesson lives in the Tryals app.

10practice questions
2interactive scenes
Start Computer Science I free

Introduction to Computers