The Storage Pyramid -- Why So Many Layers
Have you ever wondered why a computer has both "memory" and "disk"? Why not put all data in the fastest place?
The answer is hidden in the design of the computer storage system - the storage pyramid. This lecture gives you a thorough understanding of this architect's most core trade-off wisdom.
An everyday analogy: kitchen vs supermarket warehouse
Use where ingredients are placed while cooking to first build an intuitive impression.
Imagine you are cooking.
Salt and soy sauce you keep beside the stove, within arm's reach - these are the computer's registers and L1 cache.
Ingredients in the fridge, which take a few steps to reach - this is equivalent to memory (RAM).
Rice, flour, and oil stocked in the basement, which need a special trip - this is the solid-state drive (SSD).
And stock in the supermarket warehouse, which you would never keep at home - that is the mechanical hard disk (HDD) and cloud storage.
This reveals a simple rule: the more frequently used something is, the closer it is placed, but close places are limited in space and more expensive; less frequently used things are placed farther away, with larger capacity and lower cost.
The computer's storage system is designed according to this simple rule.
The storage pyramid: the trade-off between speed and capacity
Inside a computer, storage devices are arranged in a pyramid structure according to the progressive relationship of "speed-capacity-cost".
Storage pyramid diagram: the higher up, the faster and smaller; the lower down, the slower and larger. Hover over a trapezoid to see details of each layer.
Core rule
This pyramid diagram reveals an iron law of hardware design:
- Faster means more expensive and smaller: registers are inside the CPU, made of the fastest transistors, but only a few kilobytes, because chip area is extremely expensive.
- Slower means cheaper and larger: mechanical hard disks use heads and spinning platters, cost very little, and can hold tens of TB of data.
- The purpose of the pyramid structure: use a combination of "small+fast and large+slow" to achieve an experience close to the fastest storage at a reasonable cost.
If a computer used only registers for all storage, not only would the price be astronomical, but the chip area would be too large to manufacture.
Conversely, if it used only mechanical hard disks, the computer would be unusably slow - opening any program would take hundreds of milliseconds.
Detailed comparison of each layer
Put the key attributes of the seven storage layers into one table for easy side-by-side comparison.
| Level | Typical capacity | Access latency | Relative speed | Physical location | Manufacturing cost |
|---|---|---|---|---|---|
| Registers | ~1 KB | ~0.3 ns | 1x (baseline) | Inside the CPU core | Extremely high |
| L1 cache | ~64 KB | ~1 ns | 3x slower than registers | Inside the CPU core | Extremely high |
| L2 cache | ~256 KB | ~4 ns | 13x slower | Inside the CPU core | Very high |
| L3 cache | ~8 MB | ~12 ns | 40x slower | On the CPU chip, shared by multiple cores | High |
| Memory (RAM) | ~16 GB | ~100 ns | 333x slower | On the motherboard (separate chip) | Medium |
| Solid-state drive (SSD) | ~1 TB | ~100 μs | 333,333x slower | Inside the case (separate device) | Lower |
| Mechanical hard disk (HDD) | ~10 TB | ~10 ms | 33,333,333x slower | Inside the case (separate device) | Low |
Note the jump in orders of magnitude: memory is 100x slower than L1 cache, SSD is another 1000x slower than memory, and HDD is another 100x slower than SSD. The speed gap between each layer is measured in orders of magnitude.
Why is SSD so much faster than HDD?
The key difference is whether there are mechanical parts.
SSD uses flash memory chips, purely electronic reads, with no mechanical parts.
HDD uses spinning platters and moving heads - to read a piece of data, the head must first move to the correct track (seek time, about 5-10ms), then wait for the platter to rotate to the correct position (rotational latency, about 2-4ms).
This mechanical movement process is exactly the root reason HDD is hundreds of times slower than SSD.
Interactive demo: simulate access times of the multi-level storage
Use a piece of Python code to simulate the latency difference when reading the same address from each of the seven storage layers.
Example
"""
Storage Pyramid Access Latency Simulator (runoob demo)
Simulate reading a data block of the same size from different levels, intuitively comparing the speed differences of each layer
"""
class
StorageHierarchy:
"""Simulate the computer's storage hierarchy"""
def
init
(
self
)
:
Simulated latency (unit: nanoseconds) and typical capacity of each storage layer
self
.
layers
=
{
'Register'
:
{
'latency_ns'
:
0.3
,
'capacity'
:
'~1 KB'
,
'color'
:
'red'
}
,
'L1 Cache'
:
{
'latency_ns'
:
1
,
'capacity'
:
'~64 KB'
,
'color'
:
'orange'
}
,
'L2 Cache'
:
{
'latency_ns'
:
4
,
'capacity'
:
'~256 KB'
,
'color'
:
'gold'
}
,
'L3 Cache'
:
{
'latency_ns'
:
12
,
'capacity'
:
'~8 MB'
,
'color'
:
'yellow'
}
,
'RAM'
:
{
'latency_ns'
:
100
,
'capacity'
:
'~16 GB'
,
'color'
:
'green'
}
,
'SSD'
:
{
'latency_ns'
:
100000
,
'capacity'
:
'~1 TB'
,
'color'
:
'blue'
}
,
'HDD'
:
{
'latency_ns'
:
10000000
,
'capacity'
:
'~10 TB'
,
'color'
:
'purple'
}
,
}
def
read
(
self
,
layer_name
,
address
)
:
"""
Simulate reading data from a specified layer
Return the simulated data value
"""
value
=
f
"DATA_FROM_{layer_name.upper()}_{address}"
return
value
def
compare_access
(
self
,
address
,
num_accesses
=
3
)
:
"""Compare the speed of reading the same address from each layer"""
(
"="
65
)
(
f
"Storage hierarchy access latency comparison (address: {address})"
)
(
"="
65
)
(
f
"{'Level':<12} {'Latency (ns)':>12} {'Capacity':<12} {'Relative to registers':>12}"
)
(
"-"
65
)
baseline
=
self
.
layers
[
'Register'
]
[
'latency_ns'
]
for
name
,
info
in
self
.
layers
.
items
(
)
:
lat_ns
=
info
[
'latency_ns'
]
ratio
=
lat_ns / baseline
cap
=
info
[
'capacity'
]
(
f
"{name:<12} {lat_ns:>10,.1f} ns {cap:<12} {ratio:>10,.0f}x"
)
Run the demo
(
"RUNOOB storage system tutorial: storage pyramid access latency comparison"
)
(
)
store
=
StorageHierarchy
(
)
store.
compare_access
(
"0x7FFF1234"
)
(
)
(
"="
65
)
(
"Conclusion analysis:"
)
(
"="
65
)
Compute key ratios
register_lat
=
0.3
ns
ram_lat
=
100
ns
ssd_lat
=
100000
ns
hdd_lat
=
10000000
ns
(
f
"1. Memory (RAM) is {ram_lat / register_lat:,.0f} times slower than registers"
)
(
f
"2. SSD is {ssd_lat / ram_lat:,.0f} times slower than memory (RAM)"
)
(
f
"3. HDD is {hdd_lat / ram_lat:,.0f} times slower than memory (RAM)"
)
(
f
"4. HDD is {hdd_lat / register_lat:,.0f} times slower than registers"
)
(
)
(
"If accessing data from a register takes 1 second, then:"
)
(
f
" - Fetching from L1 cache takes {1/register_lat:.0f} seconds"
)
(
f
" - Fetching from memory takes {ram_lat/register_lat:,.0f} seconds (about {ram_lat/register_lat/60:.0f} minutes)"
)
(
f
" - Fetching from HDD takes {hdd_lat/register_lat:,.0f} seconds (about {hdd_lat/register_lat/3600:,.0f} hours)"
)
Running result:
RUNOOB 存储系统教学: 存储金字塔访问延迟对比
=================================================================
存储层次访问延迟对比(地址: 0x7FFF1234)
=================================================================
层级 延迟(纳秒) 容量 相对寄存器
-----------------------------------------------------------------
Register 0.3 ns ~1 KB 1x
L1 Cache 1.0 ns ~64 KB 3x
L2 Cache 4.0 ns ~256 KB 13x
L3 Cache 12.0 ns ~8 MB 40x
RAM 100.0 ns ~16 GB 333x
SSD 100,000.0 ns ~1 TB 333,333x
HDD 10,000,000.0 ns ~10 TB 33,333,333x
=================================================================
Conclusion Analysis:
=================================================================
1. 内存(RAM) 比 寄存器 慢 333 倍
2. SSD 比 内存(RAM) 慢 1,000 倍
3. HDD 比 内存(RAM) 慢 100,000 倍
4. HDD 比 寄存器 慢 33,333,333 倍
If accessing register data takes 1 second, then:
- 从 L1 缓存获取需要 3 秒
- 从内存获取需要 333 秒(约 6 分钟)
- 从 HDD 获取需要 33,333,333 秒(约 9,259 小时)
Interactive demo: logarithmic comparison chart of access latency across layers
The chart below draws a bar chart on a logarithmic scale; adjacent cells on the horizontal axis differ by a factor of 10. Hover over a bar to see capacity and latency details.
How the pyramid works: layer-by-layer caching strategy
The pyramid is not static—it has an automatically operating data flow mechanism.
Data flow rules
- When the CPU needs data: it first looks in the fastest L1 cache, then L2 if not found, then L3 if still not found, and finally main memory.
- After reading data from a slower layer: it not only gives the data to the CPU but also stores a copy in a faster layer, so next time it is faster to use.
- What happens when a fast layer is full: following a certain policy (such as LRU—evicting the least recently used), infrequently used data is pushed back to a slower layer to make room for new data.
This process is completely transparent to programmers—you do not need to manually manage which cache layer is used when writing programs; the hardware and operating system do it all automatically.
A real-world example
Suppose you are editing a video file:
- The video file is stored on an HDD or SSD (the lowest layer).
- When you open the file, the operating system loads part of it into memory (RAM).
- When you start playback, the CPU copies the data for the few frames currently being processed into the L3/L2/L1 caches.
- The pixel values currently being computed by the ALU reside in registers.
The reason it feels "smooth" in the editing software is that most of the time the CPU finds the data it needs in the cache.
Historical background: why the pyramid has this shape
The number of levels in the pyramid is a historical product of the ever-widening speed gap between the CPU and memory.
In the 1980s, the speed gap between the CPU and memory was small. But as semiconductor technology advanced, CPU speed grew by about 60% per year, while memory speed grew by only about 10% per year.
This continuously widening gap is called the "Memory Wall".
The memory pyramid is the engineers' response to the "Memory Wall"—using multiple levels of cache to buffer the speed chasm between the CPU and main memory.
| Era | CPU frequency | Memory latency | Speed gap | Cache levels |
|---|---|---|---|---|
| 1980s | ~10 MHz | ~200 ns | About 2x | None or 1 level |
| 1990s | ~200 MHz | ~70 ns | About 14x | L1 + L2 |
| 2000s | ~3 GHz | ~50 ns | About 150x | L1 + L2 + L3 |
| 2020s | ~5 GHz | ~80 ns | About 400x | L1 + L2 + L3 |
Note that memory latency has barely changed in any fundamental way over 40 years! It is not that memory has not improved, but that the CPU has improved too fast. The physical limits of memory (capacitor charge/discharge speed) mean its latency is hard to shorten dramatically.