Linux Internals
CPU Caches L1, L2 and L3: What They Mean for Server Performance
By NileVPS Team · Published · 4 min read
اقرأ المقال بالعربيA modern CPU core can execute several instructions per nanosecond, but fetching a value from main memory takes on the order of a hundred nanoseconds. Without help, the processor would spend most of its time waiting. CPU caches close that gap by keeping recently used data in small, fast memory right next to the cores. For server workloads, how well your code and data fit in those caches often matters as much as raw clock speed. This article explains the cache hierarchy, the effects that hurt performance, and the commands that show what your VPS actually has.
The cache hierarchy
Caches are organized in levels, each larger and slower than the one before it.
| Level | Typical size | Shared by | Rough latency |
|---|---|---|---|
| L1 data and L1 instruction | tens of KB each | one core | a few cycles |
| L2 | hundreds of KB to a few MB | one core (on most modern designs) | around a dozen cycles |
| L3 (last-level cache) | several MB to hundreds of MB | many cores | tens of cycles |
| Main memory (RAM) | GBs | whole system | hundreds of cycles |
These are ranges, not specifications; real numbers depend on the CPU model. The pattern is what counts: each step down the hierarchy costs roughly an order of magnitude more time.
Cache lines: the unit of transfer
Caches do not move single bytes. They move fixed blocks called cache lines, 64 bytes on current x86-64 and most ARM server CPUs. When you read one integer, the whole 64-byte line around it is loaded. This has two consequences:
- Spatial locality pays off. Walking through an array sequentially uses every byte of each line loaded, and the hardware prefetcher detects the pattern and fetches lines ahead of time.
- Scattered access wastes bandwidth. Following pointers through a linked list or a hash table spread across memory loads a full line to use a few bytes, and the prefetcher cannot predict the next address.
This is why arrays of structs that keep hot fields together, compact data formats and sequential scans tend to outperform pointer-heavy structures on the same hardware.
Cache misses and the working set
Your working set is the data a program touches repeatedly over a short period. If it fits in L1 or L2, the code runs near the CPU's peak. If it fits only in L3, performance is still good. Once it spills to RAM, each miss stalls the core. Common causes of excessive misses on servers:
- Large in-memory caches or indexes accessed randomly.
- Too many processes or threads on too few cores, so each context switch evicts the previous task's data.
- Code bloat: very large binaries can overflow the instruction cache.
False sharing
Each core keeps its own copy of a cache line, and a coherence protocol keeps the copies consistent. If two threads on different cores write to different variables that happen to sit in the same 64-byte line, every write invalidates the other core's copy. The line bounces between cores even though the threads never share data logically. This is false sharing, and it can make a "parallel" counter slower than a single-threaded one. The fix is to pad or align per-thread data to separate cache lines, for example with alignas(64) in C++ or per-CPU counters.
Caches on a VPS
On a virtual server your vCPUs run on physical cores whose L3 cache is shared with other workloads on the same host. In practice this means:
- The L3 size your guest reports describes the hardware, not a reserved slice for you.
- Performance-sensitive code should keep its own working set compact rather than relying on a large shared cache.
- Plans with dedicated vCPUs give more predictable cache behavior than shared ones. You can compare options on the pricing page.
Inspecting caches on Linux
These commands work on most distributions without extra packages:
lscpu | grep -i cache
getconf -a | grep -i CACHE
cat /sys/devices/system/cpu/cpu0/cache/index*/size
cat /sys/devices/system/cpu/cpu0/cache/index0/coherency_line_sizelscpu -C prints a table with each level, its size, associativity and line size. For measurement, perf can count misses when the hypervisor exposes hardware counters:
sudo perf stat -e cache-references,cache-misses,L1-dcache-load-misses -- ./your_appIf the events show as "not supported", the counters are not available in your VM, and you will need to rely on application-level timing instead.
Practical checklist
- Prefer contiguous data structures for hot paths.
- Keep frequently accessed fields together and rarely used ones apart.
- Separate per-thread writable data onto different cache lines.
- Avoid running far more busy threads than vCPUs.
- Measure with real data sizes; a benchmark that fits in L2 says little about production.
Cache efficiency also interacts with address translation; the companion article on the TLB covers that side.
FAQ
Is a bigger L3 cache always better for a server?
A larger L3 helps when your working set is bigger than L2 but could fit in L3, which is common for databases and in-memory caches. If your hot data is either tiny or far larger than any cache, the difference will be smaller.
Can I clear the CPU cache from Linux?
Not in a meaningful way for tuning. Commands like echo 3 > /proc/sys/vm/drop_caches drop the kernel page cache in RAM, which is a different thing. CPU caches are managed automatically by hardware.
Why does my benchmark get faster on the second run?
On the second run data is already in the page cache and often partly in CPU caches, and branch predictors are warmed up. Run benchmarks several times and report steady-state results, or deliberately test the cold case if that is what production sees.