CPU caches: small, fast memory kept close
Video 021 of 140 in Virtualization Foundations, in the unit “CPUs: sockets, cores, threads and caches”. 3:51, with English and Hindi audio.
A core can use a number the moment it has one. But main memory is much slower to answer, and that gap would leave the core waiting most of the time. A cache is the answer: a small, fast store placed close to the core. This article explains why the waiting problem exists, how the cache levels form a ladder from L1 to RAM, why small caches work at all, and what goes wrong when the data is missing.
What you will learn
- A cache is a small, fast store close to the core, so the core rarely waits for main memory.
- L1 is the smallest and fastest cache. L2 is bigger and a little slower. L3 is the biggest cache, shared by a group of cores.
- Caches work because programs reuse recent data and read nearby data in order.
- A cache miss sends the core to slower memory, and a busy shared cache can push out your data.
The waiting problem that caches solve
A fast core can use data almost at once, once it has the data in hand. Main memory, called RAM, is much slower. The fastest cache answers in about one nanosecond, and RAM takes roughly one hundred nanoseconds. A nanosecond is one billionth of a second, so RAM is about a hundred times slower.
Without a cache, a core would finish each calculation and then wait, again and again, for the next value. Most of its time would go on waiting, not on working.
The four levels, from the closest to RAM
- L1 cache: the smallest and fastest. It sits inside each core.
- L2 cache: bigger and a little slower. On most chips it belongs to one core.
- L3 cache: the biggest cache. A group of cores on the same chip shares it.
- RAM: the large main store at the bottom, shared by the whole server.
The levels form a ladder. Each step down is bigger and slower than the one above it. Each step up is smaller and faster, and it sits closer to the core.
Why a small cache catches most requests
Caches work because programs repeat themselves. A value used once is often needed again soon. Neighbouring values matter too. Programs read lists and arrays in order, so the next item is usually close by.
A small store that holds the recent and nearby data serves most requests, so the core seldom has to wait. Who shares each level also matters. L1 and L2 are usually private to one core, so one core's work does not crowd out another's. L3 is shared, so cores can reuse each other's data, but they also compete for its space.
lscpu --cachesNote: Find the L1, L2 and L3 rows in the table. Note which caches are per core and which are shared.
What goes wrong: cache misses and crowded shared caches
When data is not in the cache, that is a cache miss. The core must wait for a slower level, and it stalls. Busy neighbours on a shared L3 can also push out your data.
A real case shows the risk. Harbor Point Logistics runs an ERP database, the business system that holds orders and stock. A long report that reads far more data than the caches hold can push out the rows everyone needs. The same query can then run quickly on a quiet day and slowly on a busy one.
Note: You notice this as slow work while the CPU looks busy. The processor is waiting for memory, so check the cache behaviour before you blame the processor itself.
Terms
- Cache
- A small, fast store placed close to a processor core, holding recent and nearby data.
- Cache miss
- A request for data that is not in the cache, so the core must wait for a slower level of memory.
- RAM
- The main memory of a server, shared by every core and every program on it.
- L3 cache
- The largest cache on a chip, shared by a group of cores.
Check yourself
Why do L1 and L2 usually belong to one core, while L3 is shared?
Keeping L1 and L2 private stops one core's work from crowding out another's. L3 is shared so a group of cores can reuse data.
Why does a small cache serve most of a program's requests?
Programs reuse recent data and read nearby values in order, so most requests find their data in the small, fast cache.
What happens when a core has a cache miss?
The core must wait for data from a slower level, such as RAM, and it stalls until the data arrives.
Previous: Clock speed, turbo and why GHz is not speed. Next: Cache misses, with real numbers.