What is a computer?

Free preview 4 min to read

The machine behind your code

Every time you type ruby app.rb and press Enter, something extraordinary happens. Your human-readable Ruby code gets turned into electrical signals that switch billions of times per second inside silicon chips. But what is doing all that work?

A computer, at its core, is just three systems working together:

  1. CPU (Central Processing Unit): the brain that processes instructions
  2. RAM (Random Access Memory): the fast, temporary workspace
  3. Storage (Disk): the permanent filing cabinet

Every single thing your computer does (from running a Rails server to displaying a cat video) is just these three components passing data back and forth.


The kitchen analogy

Imagine a professional kitchen:

  • The Chef is the CPU: the only one who does things (chops, cooks, plates)
  • The Counter is RAM: everything the chef is actively working with sits here: ingredients, bowls, cutting boards. It is fast to reach but limited in space
  • The Pantry is the Disk: massive storage capacity, but the chef has to walk there and back every time they need a new ingredient. Slow, but permanent
flowchart LR
  CPU["CPU (chef)<br/>fast · processes"] <--> RAM["RAM (counter)<br/>medium · temporary"]
  RAM <--> DISK["Disk (pantry)<br/>slow · permanent"]

The chef (CPU) never goes to the pantry (Disk) directly. They always place ingredients on the counter (RAM) first, work with them there, and only store finished dishes back in the pantry when done.


What happens when you run ruby hello.rb?

Let us trace through what happens when you execute a Ruby file:

Step 1, loading: The operating system reads the Ruby interpreter program from Disk into RAM.

Step 2, reading Your File: Ruby reads hello.rb from Disk into RAM.

Step 3, processing: The CPU takes the Ruby code from RAM, interprets it, and executes the instructions.

Step 4, output: The result (puts "Hello, world!") travels from RAM to your screen.

flowchart LR
  A["Disk"] -->|load| B["RAM"] -->|run| C["CPU"] -->|result| D["RAM"] -->|display| E["Screen"]

Every program you have ever run follows this same basic pattern.


Why should a Rails developer care?

Understanding these three components explains why common performance problems happen:

Problem Hardware Cause Example
Slow database queries Too many trips to the database, which may read from Disk N+1 queries: 100 separate queries instead of 1
Out of memory errors RAM is full Loading 1 million records with User.all
CPU spikes CPU overloaded Complex calculations in a loop without optimization
Caching makes things fast Skipping slow work Rails.cache with a memory store keeps results in RAM (this app's Solid Cache keeps them in the database)

When a senior developer says "that query is doing too much I/O", they mean the code is making the CPU wait for the slow Disk too many times. When they say "we need to cache that", they mean: keep the result somewhere fast, often RAM, so the slow work happens once.


The speed gap is enormous

The difference in speed between these components is not small: it is astronomical:

Component Speed Analogy
CPU ~0.3 nanoseconds Blinking your eye
RAM ~100 nanoseconds Making a cup of coffee
SSD ~100,000 nanoseconds Taking a shower
HDD ~10,000,000 nanoseconds Flying from New York to London

This is why your computer has all three: if RAM were permanent, we would not need disks. If disks were fast, we would not need RAM. Each exists because the one above it is too expensive to scale.


Summary

  • A computer is CPU + RAM + Storage working together
  • The CPU processes, RAM holds temporary data, Disk stores permanent data
  • Data always flows: Disk → RAM → CPU → RAM → output
  • Many performance problems in Rails map to one of these three components
  • The speed difference between RAM and Disk is the single biggest reason caching exists
Lesson 1 of 6 · free

Keep going with lesson 2

The CPU: your computer's brain

Covers what the CPU does, clock speed, cores, the fetch-decode-execute cycle and how Ruby's GVL limits threads.

A free account also gives you the quizzes, your progress and notes, and the discussion.

Type to start searching...