How-to

The Memory Hierarchy, Top to Bottom

Every server stacks small fast memory on top of large slow memory. Programs that respect that stack run several times faster than programs that fight it.

What is the memory hierarchy?

A chain of storage levels, each larger, cheaper per byte and slower than the one above it. At the top sit the processor's registers; below them three levels of on-chip cache; then main memory; then NVMe drives; then anything reached over the network. Hardware and the operating system move data up and down this chain automatically, keeping what is used now close to the core and the rest further away.

How big is the gap between levels?

LevelTypical size per core or systemRough access time
RegistersA few hundred bytesUnder 1 nanosecond
L1 cache32 to 64 KB per coreAbout 1 nanosecond
L2 cache1 to 2 MB per core on current chipsA few nanoseconds
L3 cacheTens to hundreds of MB, shared10 to 40 nanoseconds
DRAM64 GB to 1 TB per machine hereAbout 100 nanoseconds
NVMe SSDTerabytesTens of microseconds
Network storage, other citiesUnlimitedMilliseconds

These are order-of-magnitude figures for current server parts, good for reasoning about design. The key insight is the ratio: a trip to RAM costs about a hundred L1 hits, and a trip to NVMe costs hundreds of RAM accesses.

How do I see the caches on my machine?

lscpu --caches
lscpu | grep -iE 'model name|socket|numa node'
getconf -a | grep -i CACHE_SIZE

lscpu --caches lists each level, its size, which cores share it and the line size, usually 64 bytes. On a dual-socket box such as D4, you will see two separate L3 caches, one per socket; on a single-socket S13, one shared L3 for all 8 cores.

Why does locality matter so much?

Caches load memory in 64-byte lines and guess what comes next. Programs that read memory in order, such as scanning an array, get the next values almost free. Programs that jump around, such as following pointers through a large linked list or hash table, miss the cache often and wait on RAM each time. Two kinds of locality pay off:

  • Spatial: using data stored next to data you just used.
  • Temporal: using the same data again soon, while it is still cached.

Column stores like ClickHouse, vectorized query engines and many compression libraries are fast largely because they are built around this.

How do I measure cache misses?

On bare metal you can read the processor's performance counters directly, something many virtual servers block. Install perf (package linux-tools-common plus the kernel-specific tools on Ubuntu, linux-perf on Debian, perf on Enterprise Linux):

perf stat -e cycles,instructions,cache-references,cache-misses -p $(pgrep -o postgres) -- sleep 10

Instructions per cycle well above 1 means the core is busy computing; values well below 1 with many cache misses mean it mostly waits for memory. That tells you whether faster cores or a different data layout would help.

Where does the operating system fit in?

Linux uses free RAM as a cache for the NVMe level: the page cache. Reading a file twice hits RAM the second time. Databases add their own caches on top, the InnoDB buffer pool or PostgreSQL shared buffers. Sizing those correctly is the most valuable tuning most servers ever get; see Database servers and Check memory usage.

How does this guide machine choice?

Look at which level your workload spills into. If the hot data fits in L3, clock speed and core design dominate, which favors chips like the Intel Core i9-14900K in S11. If it fits in RAM but not cache, memory bandwidth and channel count matter, which favors DDR5 and dual sockets. If it spills to NVMe, buy more RAM before anything else. If it spills to the network, put the data closer: the same city, or the same machine. The Latency entry covers the network end of the chain, and Optane vs SSD vs RAM the layer that once sat between RAM and flash.

Keep Reading

Optane vs SSD vs RAM

Where persistent memory sat between DRAM and flash, why it was discontinued and how today's DDR5 plus NVMe covers the same jobs.

Open

Linux memory management

Virtual memory, pages, overcommit, huge pages, cgroup limits and the OOM killer: how Linux hands out RAM and the knobs worth turning.

Open

Latency

Why distance sets the floor for ping time, how to estimate round trips between our six cities and how to choose a location.

Open

NUMA and dual-socket servers

Why two processors in one box share memory unevenly, and when a dual-socket machine beats a single big chip.

Open

FAQ

Quick Answers

The chain of storage levels from registers and caches through RAM to SSDs and network storage, each larger and slower than the last.

Roughly a hundred times, about 100 nanoseconds compared with about 1 nanosecond.

Run lscpu --caches to list every cache level with size and sharing.

Yes. perf stat reads hardware counters directly on bare metal.

The unit caches load from memory, usually 64 bytes on current processors.