Hardware
CPU Cache Explained: L1, L2 and L3 in Plain Words
CPU cache explained: what L1, L2 and L3 cache do, sizes and speeds, cache lines and misses, a worked access-time example, and when more cache helps.
CPU cache is a small amount of very fast memory built into the processor that keeps copies of the data and instructions the CPU is most likely to need next. Main memory (RAM) is large but slow compared with the processor: fetching something from RAM can take hundreds of clock cycles, during which a core could have done useful work. Cache hides most of that delay by serving the majority of requests from memory sitting right next to the cores.
Modern CPUs use several levels: a tiny, extremely fast L1 per core, a larger L2 per core, and a big L3 shared by many cores. With CPU cache explained level by level, this guide covers what it is, how the levels work, typical sizes and speeds, what cache lines and misses are, a worked example of how much cache saves, how code can use it well, and when buying a CPU with more cache actually helps.
CPU Cache at a Glance
| Level | Typical size (modern desktop) | Approximate speed | Shared? | Role |
|---|---|---|---|---|
| L1 | Tens of KB per core, split into instruction and data caches | A few cycles | Per core | Holds the data being worked on right now |
| L2 | Around 1–3 MB per core | Roughly 10–20 cycles | Per core (some designs share between core pairs) | Catches what L1 misses |
| L3 | Tens of MB, sometimes over 100 MB | Tens of cycles | Shared by many or all cores | Last stop before RAM; shares data between cores |
| RAM | GBs | Roughly 60–100+ nanoseconds (hundreds of cycles) | Whole system | Main working memory |
Exact figures vary by processor generation and design. AMD's Ryzen 7 7800X3D, for example, lists 512 KB of L1, 8 MB of L2 and 96 MB of L3 cache across its 8 cores.
What Is CPU Cache?
What is CPU cache, physically? It's SRAM (static RAM) built on the processor die itself. SRAM is much faster than the DRAM used for main memory, but each bit takes far more space and power, so caches stay small. Our comparison of SRAM vs DRAM covers the trade-off.
Why is CPU cache important? Because the gap between processor speed and memory speed is so large that without it, cores would spend most of their time waiting. Cache works because programs are predictable in two ways:
- Temporal locality: data used recently is likely to be used again soon, like a loop counter or a frequently called function.
- Spatial locality: data near something just used is likely to be used next, like the next element in an array.
The cache keeps recently used data (temporal) and fetches whole neighbouring chunks at once (spatial). When the CPU needs a value, it checks L1 first, then L2, then L3, and only then goes to RAM. Finding it in cache is a hit; not finding it is a miss.
L1, L2, L3 Cache Explained
Together, the L1 L2 L3 cache hierarchy trades size for speed:
L1 cache
The smallest and fastest, inside each core. It's usually split in two: an instruction cache for the program's code and a data cache for the values it works on, so both can be read in the same cycle. Access takes only a few clock cycles.
L2 cache
Larger and somewhat slower, still private to each core in most current designs. It holds data that has recently been pushed out of L1 or is likely to be needed soon.
L3 cache
The largest and slowest level, shared by many or all cores on the chip. Besides catching misses from L2, it lets cores share data without going to RAM. Some processors stack extra L3 on top of the die; AMD's "3D V-Cache" chips, such as the 7800X3D, use this to reach 96 MB of L3.
A library analogy
L1 is the book open on your desk. L2 is the shelf beside you. L3 is the library on your floor that colleagues share. RAM is the warehouse across town. Most of the time, what you need is on the desk or the shelf.
Cache in Multi-Core and Chiplet Processors
With many cores sharing one chip, cache design shapes how cores cooperate:
- Shared L3 lets one core find data another core recently used without a trip to RAM, which speeds up multi-threaded programs that share data.
- Chiplet designs, where a processor is built from several smaller dies, often give each chiplet its own L3. A core reaches its own chiplet's L3 quickly, but data held in another chiplet's cache takes longer to fetch. Operating systems and games can be sensitive to which cores a program's threads run on.
- Hybrid processors with performance and efficiency cores may give each type different cache arrangements, such as small clusters of efficiency cores sharing an L2.
For everyday use, none of this needs tuning. For performance-critical servers and games, pinning related threads to cores that share a cache can help.
Cache Lines, Hits and Misses
Caches don't move single bytes. They move fixed-size blocks called cache lines, 64 bytes on mainstream x86 processors. Reading one byte brings its whole 64-byte neighbourhood into cache, which is why reading an array in order is so fast: one miss loads the next several elements too.
Misses come in three kinds:
| Miss type | Cause | Example |
|---|---|---|
| Compulsory | First time the data is touched | Reading a file buffer for the first time |
| Capacity | The working set is bigger than the cache | Scanning a 200 MB table repeatedly |
| Conflict | Several addresses compete for the same cache slots | Strided access hitting the same set over and over |
Other mechanisms help or hurt:
- Prefetchers in the CPU spot regular access patterns and load lines before they're requested.
- Associativity sets how many places a given line can live in the cache; more flexibility means fewer conflict misses.
- Coherence protocols keep each core's cached copies consistent when one core writes shared data.
- False sharing happens when two cores write different variables that sit on the same cache line, forcing the line to bounce between cores and slowing both down.
How Much Does Cache Save? A Worked Example
The average memory access time (AMAT) combines hit times and miss rates across the levels. Assume a processor with these illustrative figures:
| Level | Access time | Miss rate (of requests reaching it) |
|---|---|---|
| L1 | 4 cycles | 5% |
| L2 | 14 cycles | 40% |
| L3 | 50 cycles | 30% |
| RAM | 300 cycles | — |
AMAT = 4 + 0.05 × (14 + 0.40 × (50 + 0.30 × 300)) = 4 + 0.05 × 70 = 7.5 cycles
Without any cache, every access would take about 300 cycles, 40 times longer. And small changes in miss rates matter:
| Scenario | AMAT |
|---|---|
| Baseline | 7.5 cycles |
| L1 miss rate doubles to 10% | 11.0 cycles (+47%) |
| Bigger L3 halves its miss rate to 15% | 6.6 cycles (−12%) |
| Working set outgrows L3; miss rate rises to 60% | 9.3 cycles (+24%) |
That's why code that fits its working data into cache can run several times faster than code that doesn't, on the same processor.
Writing Cache-Friendly Code
Programmers can't control the cache directly, but they can arrange data and loops to suit it. The classic example is traversing a two-dimensional array stored row by row (as C does):
// Fast: walks memory in order, using every byte of each cache line
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
sum += a[i][j];
// Slow for large N: jumps a whole row each step, touching a new line every time
for (int j = 0; j < N; j++)
for (int i = 0; i < N; i++)
sum += a[i][j];Both loops do the same arithmetic, but on large arrays the second is often several times slower because nearly every access misses.
Other cache-friendly habits:
- Prefer contiguous arrays to linked lists and pointer-heavy structures.
- Keep hot data together and cold data elsewhere (for example, structure-of-arrays layouts for data processed field by field).
- Process data in blocks that fit in cache (tiling), which is how fast matrix libraries work.
- Avoid false sharing by padding per-thread counters onto separate cache lines.
- Measure. Tools such as Linux
perf stat -e cache-missesshow whether misses are the bottleneck.
Is More CPU Cache Better?
Is more CPU cache better? Usually it helps, but only when your workload's working set doesn't already fit and is limited by memory access:
| Workload | Benefit from extra L3 |
|---|---|
| Many games, especially CPU-bound ones at high frame rates | Often noticeable; this is the main pitch for AMD's X3D chips |
| Large simulations, databases, compilation of big projects | Often helpful |
| Video encoding and rendering | Usually small; these stream data and are compute-bound |
| Office work and web browsing | Negligible |
| Workloads that already fit in L2 or L3 | None |
Cache size is one factor among core count, clock speed, architecture and memory speed. Compare benchmarks for the software you actually use rather than cache size alone. For memory bandwidth, our guide to dual-channel RAM covers the next level down.
CPU Cache vs Other "Caches"
The word cache appears all over computing, always meaning "a faster copy of something slower":
| Term | What it caches | Where |
|---|---|---|
| CPU cache (L1–L3) | Memory contents | On the processor |
| GPU cache | Data for thousands of GPU threads | On the graphics chip; see GPU vs CPU |
| Disk cache / page cache | File data from storage | In RAM, managed by the operating system |
| Browser cache | Web pages, images, scripts | On your disk |
| KV cache | Attention keys and values in language models | In GPU memory |
Common Myths
- "Clearing CPU cache speeds up my PC." CPU caches manage themselves continuously; there's nothing for users to clear.
- "More L3 always means faster." Only for workloads that run out of cache.
- "L1 size is the most important spec." L1 sizes change little between generations; L2 and L3 sizes vary far more and show up in marketing.
- "Cache and RAM are the same thing." Cache is a small, fast copy on the processor; RAM is the large main memory.
FAQ
What does L1, L2 and L3 mean?
They're levels of CPU cache. L1 is the smallest and fastest, per core; L2 is larger and slower, usually per core; L3 is the largest and slowest, shared between cores.
How much CPU cache do I need?
There's no fixed number. For gaming, chips with large L3 caches often help; for most other everyday tasks, core count and clock speed matter more.
Can CPU cache be upgraded?
No. Cache is built into the processor; getting more means buying a different CPU.
Is cache faster than RAM?
Yes, by a large margin. L1 answers in a few cycles; RAM takes hundreds of cycles.
What happens on a cache miss?
The CPU looks in the next level down, and if all levels miss, it fetches the line from RAM, stalling or switching to other work while it waits.