Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Computer Basics / How Computers Work

    Memory and Storage: Why Speed Differs a Million Times

    RAM (Random Access Memory) is the computer's working memory — where running programs and their

    Chapter 6 of 98 min read

    The Plain Picture#

    Back to the cook. Ingredients can be in four places:

    Output
      Countertop        right in front of you, holds 5 things       grab: instant
      Kitchen shelf     a step away, holds 100 things               grab: 1 second
      Pantry            walk to the next room, holds 10,000 things  grab: 1 minute
      Warehouse         drive across town, holds everything         grab: a whole day

    Nobody keeps everything on the countertop — it's tiny. Nobody drives to the warehouse for every pinch of salt — that would take forever. A good cook keeps what they're using close, and fetches a whole box at a time from far away, not one grain.

    A computer has exactly this layering.

    The Real Names#

      FAST, SMALL, EXPENSIVE
              ▲
              │   Registers       inside the CPU           a few hundred bytes
              │   Cache (L1)      inside each core         ~32–64 KB
              │   Cache (L2)      next to each core        ~1–2 MB
              │   Cache (L3)      shared by all cores      ~8–64 MB
              │   RAM             separate chips           8–64 GB (laptop)
              │   SSD / HDD       the disk                 256 GB – several TB
              ▼
      SLOW, BIG, CHEAP
    • RAM (Random Access Memory) is the computer's working memory — where running programs and their data live. It is volatile: when power goes off, everything in RAM is lost.

    • Disk — an SSD (Solid State Drive, a memory chip with no moving parts) or an older HDD (Hard Disk Drive, a spinning magnetic platter) — keeps data persistently: it survives power-off. Your saved files live here.

    • Cache is small, very fast memory built into the CPU chip. It holds copies of the pieces of RAM the CPU used most recently, so the CPU doesn't have to wait for RAM each time.

    Under the Hood#

    The numbers every engineer should know#

    These are rough, but the ratios are what matter. Let's also scale them up: pretend one CPU tick (~0.3 ns) is 1 second of human time.

    ActionReal timeIf 1 tick = 1 second
    One CPU tick~0.3 ns1 second
    Read L1 cache~1 ns3 seconds
    Read L2 cache~4 ns13 seconds
    Read L3 cache~15 ns50 seconds
    Read RAM~100 ns5–6 minutes
    Read from SSD (random spot)~20–100 µs1–4 days
    Read from HDD (seek)~5–10 ms6 months – 1 year
    Network round trip, same data centre~0.5 ms~3 weeks
    Network round trip, India ↔ USA~200–250 ms~25 years

    (µs = microsecond = one millionth of a second = 1,000 ns. ms = millisecond = one thousandth of a second = 1,000 µs.)

    Read that table twice. It explains a huge amount of software engineering:

    • Why databases keep hot data in RAM (Databases (coming soon)).

    • Why caches like Redis exist (Caching (coming soon)).

    • Why a website served from the other side of the world feels slow, and why CDNs exist (System design (coming soon)).

    How the cache decides what to keep — locality#

    When the CPU needs one byte from RAM, it doesn't fetch just that byte. It fetches a whole block of 64 bytes around it, called a cache line — like grabbing the whole box from the pantry.

    This works because programs usually show two patterns:

    1. Spatial locality — if you used one spot, you'll probably use the spot right next to it soon (walking through a list).

    2. Temporal locality — if you used something, you'll probably use it again soon (a loop counter).

    Why this matters in DSA: an array stores its items side by side in memory. Walking through it uses every byte of each 64-byte cache line. A linked list (taught in Linked Lists (coming soon)) scatters its items all over memory, so almost every step can mean a slow trip to RAM. Both are "O(n)" to walk through (Big-O is taught in Big-O and Complexity), but the array is often several times faster in practice because of the cache.

    Why RAM is "random access"#

    "Random access" means reaching any position takes about the same time — position 5 or position 5,000,000. That's different from a tape, where you'd have to wind past everything in between. (An HDD is partly tape-like: the disk has to spin to the right spot. That's why HDDs are so slow for random reads, and why SSDs changed everything.)

    Gotchas

    • Saved ≠ in RAM. Data in RAM disappears on crash or power-off. A program that says "saved!" before the data is actually on disk can lose it. Databases solve this with a write-ahead log (Storage Engines (coming soon)).

    • Adding RAM doesn't make the CPU faster. It only helps if you were running out of RAM and the computer was using the (much slower) disk as spare memory.

    • Network is slower than disk in most real systems. A design that makes 50 network calls to answer one request will be slow no matter how fast each server is.

    Check yourself

    5 questions
    1

    What happens to data in RAM when the power goes off? What about data on an SSD?

    Show answer

    RAM loses it (volatile). The SSD keeps it (persistent).

    2

    Roughly how many times slower is reading RAM than reading L1 cache?

    Show answer

    About 100× (~1 ns vs ~100 ns).

    3

    What is a cache line, and how big is it usually?

    Show answer

    The fixed-size block (usually 64 bytes) the CPU copies from RAM into cache at once.

    4

    Why is looping over an array usually faster than looping over a linked list with the same items?

    Show answer

    Array items sit side by side, so each cache line brings in many useful items; linked list items are scattered, causing many slow RAM trips.

    5

    Which is slower: reading from a local SSD, or a network call to a server in the USA from India?

    Show answer

    The network call: ~200 ms vs well under 1 ms for an SSD.