Servers, processes and hardware
Overview
Diagrams show a “server” as a single box, but every box is a real machine (or a slice of one) with limits. To decide how many servers you need, or why a database is slow, you need a feel for what one machine can do. This lesson covers the parts of a server and the numbers that matter.
What one machine gives you
- CPU. Executes your code. Modern servers have from a handful to over a hundred cores; each core runs one thing at a time, so more cores means more work in parallel.
- RAM (memory). Fast storage the CPU works from. It is lost when the machine restarts, and it is limited, typically tens to hundreds of gigabytes.
- Disk. Durable storage that survives restarts. SSDs are far faster than old spinning disks but still much slower than RAM.
- Network card. Connects the machine to others; it has its own bandwidth limit, often 10–25 Gbit/s in a data centre.
In the cloud you rarely touch the hardware. You rent a virtual machine (a slice of a physical server) or run containers (isolated processes that share an operating system). The limits are the same: a fixed amount of CPU, memory, disk and network.
Processes and threads
A process is a running program, such as your web server or a database. The operating system gives each process its own memory, so one crashing process does not corrupt another.
A thread is a sequence of work inside a process. Threads in the same process share its memory, which makes communication fast but means they must be careful when changing shared data. A classic web server handles each request on its own thread; event-loop servers such as Node.js use one main thread that switches between many requests while they wait for I/O.
You don’t need the details of schedulers for interviews. You do need two ideas:
- Work runs in parallel only up to the number of cores.
- Waiting (for a disk, a database or another service) does not use the CPU, so a server can juggle many more waiting requests than it has cores.
Latency numbers worth knowing
These rough figures come up again and again. Learn the orders of magnitude, not the exact values.
| Operation | Rough time |
|---|---|
| Read from CPU cache | ~1 ns |
| Read from main memory (RAM) | ~100 ns |
| Read 1 MB sequentially from RAM | ~10 µs |
| Random read from an SSD | ~100 µs |
| Round trip within a data centre | ~0.5 ms |
| Read 1 MB sequentially from an SSD | ~1 ms |
| Disk seek on a spinning hard drive | ~10 ms |
| Round trip from Europe to the US and back | ~80–150 ms |
Some conclusions follow directly:
- Memory beats disk by about 1,000×. That is why caches and in-memory databases exist, and why a database is fast when its working set fits in RAM.
- Sequential beats random. Reading data that sits together on disk is far cheaper than jumping around; many storage engines are designed around this.
- The network is slow compared with memory. A service that makes 20 calls to other services in a row adds up their round trips. Batch calls or make them in parallel.
- Distance costs time. Light in fibre needs tens of milliseconds to cross an ocean. You can’t optimise that away; you can only put data closer to users, which is what CDNs and multi-region designs do.
CPU-bound, memory-bound and I/O-bound
When something is slow, ask which resource runs out first.
- CPU-bound: busy computing, such as video encoding, image resizing or encryption. Add cores or machines, or do less work.
- Memory-bound: the data doesn’t fit in RAM, so the machine keeps going to disk. Add memory, compress data, or split it across machines.
- I/O-bound: waiting for disks or the network, such as a typical API that calls a database. Cache results, reduce the number of calls, use asynchronous I/O and add indexes.
Most web application servers are I/O-bound. That is good news: they are easy to scale out by adding servers, as long as the database behind them can keep up.
How much can one server do?
Rough, workload-dependent figures help with estimates:
- A simple, well-written API server can handle from hundreds to a few thousand requests per second.
- A single relational database on good hardware can handle thousands to tens of thousands of simple queries per second.
- An in-memory cache such as Redis can serve on the order of 100,000 simple operations per second per node.
Treat these as starting points to say out loud in an interview (“assume ~1,000 requests per second per app server”), not as facts about any specific system.
Recap
Every box in a diagram is a machine with limited CPU, memory, disk and network. Processes isolate programs, and threads let one program work on many requests. Memory is about a thousand times faster than disk, and the network is slow compared with both, so good designs keep hot data in memory and avoid long chains of network calls. Before scaling anything, find out which resource is the bottleneck.
CPU-bound or I/O-bound?
For each workload, say whether it is mainly CPU-bound, memory-bound or I/O-bound, and suggest one way to make it faster: (a) resizing uploaded photos into thumbnails, (b) an API that reads a user profile from a database and returns it as JSON, (c) a service that keeps a 50 GB lookup table and answers queries against it, (d) generating a monthly PDF report from 10 million database rows.
(a) Resizing photos:
(b) Profile API:
(c) 50 GB lookup table:
(d) Monthly report:(a) Resizing photos: CPU-bound (image processing is pure computation).
Faster: more cores or more worker machines, process images in parallel from
a queue, or use a GPU/optimised image library.
(b) Profile API: I/O-bound (most time is spent waiting for the database).
Faster: cache profiles in memory/Redis, add an index, use async I/O so one
server can wait on many requests at once.
(c) 50 GB lookup table: memory-bound. If it does not fit in RAM, every query
hits disk and is ~1000x slower. Faster: a machine with more RAM, or split
(shard) the table across several machines so each part fits in memory.
(d) Monthly report: I/O-bound on reading 10 million rows (plus some CPU).
Faster: run it as a background batch job, read from a replica so it doesn't
slow the main database, stream rows instead of loading them all at once,
or pre-aggregate totals during the month.Summary
- A server offers CPU (computation), RAM (fast, temporary memory), disk (slow, durable storage) and a network card.
- A process is a running program with its own memory; its threads share that memory and run in parallel on the cores.
- Memory is roughly 1,000 times faster than an SSD read, and a cross-continent round trip is about 1,000 times slower than a round trip within a data centre.
- Most web servers are I/O-bound: they spend their time waiting for databases and networks, not computing.
- Know whether your workload is CPU-, memory- or I/O-bound before deciding how to scale it.