Topic 009 / Core
CPU, RAM, storage and I/O
Chapter 1.2 · The Operating System and the Machine
What and why
Not all memory access costs the same: a CPU's own cache is extremely fast, RAM is slower, local storage (SSD/disk) is far slower again, and a network round trip is slower still — and the gap between consecutive hops is roughly an order of magnitude each time. Almost every later design decision in this book — caching, batching, avoiding chatty network calls — exists because of this one hierarchy. Builds on: → 8 (memory management).
Diagram
(illustrative — relative order of magnitude only; the full reference numbers are → 19.)
CPU cache fastest
│ (an order of magnitude slower)
▼
RAM
│ (another order of magnitude slower)
▼
SSD / disk
│ (slower again, and far more variable)
▼
network round trip slowest, least predictable
How it works
- The CPU computes on data already in registers or cache; anything else must be fetched first, and each hop further from the CPU costs roughly an order of magnitude more time than the hop before it.
- RAM is "slow" only relative to cache — it is still vastly faster than any persistent storage.
- Storage I/O is slow enough that the OS and database both keep pages of it in RAM (a cache, → 82) specifically to avoid touching the device on every request.
- Network I/O is the slowest and least predictable hop — its cost depends on distance, congestion, and how many round trips the protocol on top of it requires.
- Because of this hierarchy, most backend services spend most of their time waiting on I/O rather than computing — which is why a concurrency model that handles many waiting tasks cheaply (→ 6) usually matters more than raw CPU speed.
Practical example
Measure, on your own machine: (a) the average time per call of one million plain function calls in a loop, (b) the average time to read a small file already cached by the OS, repeated a thousand times, (c) one network round trip to a host outside your machine. Rank the three from fastest to slowest and compute the ratio between each consecutive pair. The resulting numbers are specific to your machine and network — the point is the relative order of magnitude between the three, not any absolute value; the canonical reference table is → 19.
When to use it
- Use this hierarchy to decide what to optimize first: an extra network round trip usually costs more than an extra in-memory computation, by a wide margin.
- Avoid optimizing CPU-bound code when the actual bottleneck is I/O wait — check where time is going (→ 103) before rewriting anything.
- Failure scenarios: adding an in-memory cache in front of something that was never the slow part, while the real cost was a protocol doing ten network round trips where one would do.
- Simpler alternative: before any hierarchy-aware optimization, measure where time actually goes (→ 103) rather than optimizing from the table alone.
Practice
(numeric — measure and record; 15–20 min, no lab services). Measure and record, on your machine: (1) the per-call average of one million plain function calls, (2) the average time to read a small, OS-cached local file a thousand times, (3) one network round trip to a host outside your machine. Rank the three fastest-to-slowest and compute the ratio between each consecutive pair. Done when you have three measured numbers, correctly ranked, with their ratios recorded — this is the raw input topic 19's table will formalize.
Key takeaway
Each step away from the CPU — cache to RAM to storage to network — costs roughly an order of magnitude more time than the step before it, and most services spend most of their time waiting on the slowest steps they touch. Done when you can: rank cache, RAM, storage, and network by cost from your own measurements, not from memory. Used next in → 19 (the canonical latency table, Part 2), → 82 (cache hierarchy), → 103 (profiling and bottleneck analysis).
References
None — this topic makes no external claims.
Source directory