# CPU cache

> A tiny, very fast memory inside the processor that keeps the data it is about to need.

- Level: Intermediate
- Topics: Hardware
- URL: https://tidbit.legios.com.ar/cpu-cache/
- In Spanish: [Caché de CPU](https://tidbit.legios.com.ar/es/cpu-cache/)

A CPU can do billions of operations per second, but fetching data from RAM takes far longer than one operation: the processor would spend most of its time waiting. The **CPU cache** fixes that. It is a small amount of extremely fast memory built into the chip itself, holding copies of the data and instructions the processor used recently or is likely to use next.

Caches come in levels. **L1 cache** sits right next to each core: tiny (tens of kilobytes) and almost instant. **L2 cache** is bigger and a bit slower. **L3 cache** is larger still, often several megabytes or more, and usually shared by all the cores. When the CPU needs something it checks L1, then L2, then L3, and only goes to RAM if none of them has it. Finding it is a hit; going all the way out is a miss.

Caches work because programs are predictable: they tend to reuse the same data and to read memory in order. The chip loads whole neighboring chunks at once and keeps what was used lately. That is why two programs doing the same math can run at very different speeds depending on how they lay out their data, and why chipmakers advertise cache size next to cores and clock speed.

## An analogy

A cook's workspace. The knife and salt are within arm's reach (L1), the spices are on the shelf behind (L2), the pantry is down the hall (L3), and the supermarket is RAM. A good cook keeps what the recipe needs next close at hand.

## Keep in mind

- Cache is small, very fast memory on the CPU chip itself.
- L1 is the smallest and fastest, L3 the largest and usually shared.
- A hit is served from cache; a miss has to wait for RAM.
- Software that reads memory in order makes much better use of the cache.

## Go deeper

- [CPU cache on Wikipedia](https://en.wikipedia.org/wiki/CPU_cache)
- [Cache (computing) on Wikipedia](https://en.wikipedia.org/wiki/Cache_(computing))

## Related terms

- [CPU](https://tidbit.legios.com.ar/cpu/): The chip that runs your programs, one instruction after another, very fast.
- [RAM](https://tidbit.legios.com.ar/ram/): The computer's short-term memory: fast, small and wiped when you turn it off.
- [Chip](https://tidbit.legios.com.ar/semiconductor-chip/): A sliver of silicon carrying billions of tiny switches that compute and remember.
- [Caching](https://tidbit.legios.com.ar/caching/): Keeping a copy of an answer close by so you do not have to work it out again.
- [Bit and byte](https://tidbit.legios.com.ar/bit-and-byte/): The smallest unit of digital information, and the group of eight that computers count in.
- [Cores and threads](https://tidbit.legios.com.ar/cpu-cores-and-threads/): Why a CPU is really several processors, and why it can juggle even more tasks.
- [Clock speed](https://tidbit.legios.com.ar/clock-speed/): How many times per second a processor ticks, measured in gigahertz (GHz).
