Skip to content
Episode 3 · The flash sale

What is a server?

Friday, 20:04: Maya's flash-sale email reached 60,000 past guests four minutes ago. Traffic is five times the usual, search takes 8 seconds, and our one server in Frankfurt shows 100% CPU.

A server is just a program that waits for requests and answers them, running on a machine with limited power. Here's what that really means.

30 min foundationServer resources: CPU, memory, disk, networkProcesses vs threadsQueueing: why latency explodes near saturationStateless serversVertical vs horizontal scalingLatency
  1. Problem

    Thousands of guests search for hotels at the same time, and each expects an answer in well under a second.

  2. Why the simple system fails

    One program on one machine has a fixed amount of CPU time per second. Past that, requests wait in a queue, latency explodes, clients time out and retry, and things get worse.

  3. Engineering concept

    A server is a process listening on a port, using CPU, memory, disk and network. Its capacity is finite; queueing makes latency non-linear near saturation. Stateless servers can be copied.

  4. Technology

    Operating system processes and threads, web servers (Node, Gunicorn, Go net/http), reverse proxies (nginx), load balancers, autoscaling.

  5. Trade-offs

    A bigger machine (vertical) is simple but capped and a single point of failure. More machines (horizontal) scale further but require statelessness and a load balancer.

01

The problem

2 min
Friday, 20:04. Maya's flash-sale email went out four minutes ago.
  1. Omar · On-call SRE
    225 requests per second. A normal Friday is 45. CPU at 100%. Search p99 went from 150 ms to 8 s.
  2. Karim · CTO
    Five times the traffic, fine. But fifty times slower? What is that server even doing?
  3. Lina · Senior engineer, your mentor
    Guess first, then we look inside it.
02

The big idea

4 min
Why 5× traffic means 50× slower
The big idea

A server is fast until it's nearly full. Then requests queue and latency explodes.

The server isn't slower at each search tonight. It's full, and full means waiting. So you plan for headroom below the cliff, not for the edge.

03

How it works

5 min

A is a program that waits on a port (a numbered door on a machine, 443 for HTTPS) and answers requests, plus the machine it runs on, with limited CPU, memory, disk and network.

One search inside our server
  1. 1
    Knock on the port

    The guest connects to our IP on 443. The operating system hands the connection to our app .

  2. 2
    Compute

    A worker parses the request and filters hotels: about 20 ms of CPU on one core.

  3. 3
    Wait

    It asks PostgreSQL for prices: about 60 ms of waiting. The core serves someone else meanwhile.

  4. 4
    Answer

    It turns the result into JSON and sends the response.

  5. 5
    Or queue

    If every core is busy, the request waits. Its time = wait in line + its own work.

  6. 6
    Watch utilisation

    Utilisation is how busy a resource is, 0–100%. Past about 80%, waits grow very fast: at 90% a request takes about 10× its normal time.

The ceiling: 4 cores × (1,000 ms ÷ 20 ms) = about 200 searches per second. A normal Friday is 45 (CPU ~22%). Tonight 225 arrive, so the queue grows by 25 every second. Timeouts fire, the app retries, and retries join the same queue.

04

Watch a decision being made

4 min
Worked example

Lina sizes the server for the next sale

Another flash sale in two weeks, same 225 searches per second at peak. Keep search under 200 ms without rewriting StayHub.

  1. 1Measure the cost of one request

    Each search uses about 20 ms of CPU and waits about 60 ms on the database.

    Why: CPU per request fills the cores. Waiting doesn't use a core, so it doesn't count toward the ceiling.

  2. 2Compute the ceiling, and size for 60%

    4 cores × 50 = 200/s at 100% CPU, below 225. Target about 60% busy at peak: 225 ÷ 0.6 ≈ 375/s needed.

    Why: Near 100% the wait in line dominates. At 60%, spikes stay in the flat part of the curve.

  3. 3Pick the cheapest fix that gets there

    An 8-core machine gives about 400/s: 225 ÷ 400 ≈ 56% busy, with no code change. A second 4-core server would also work, but sessions live in memory, so it would log guests out.

    Why: With two weeks to go, a change with no code is the safest one.

  4. 4Prove it before the sale

    Load-test at 250/s and watch CPU and p99. Expect about 60% CPU and p99 well under 200 ms.

    Why: The ceiling is an estimate. A load test finds the real knee before guests do.

The whole thing in one line: Cost per request → ceiling → size for ~60% busy → cheapest fix → load test. The spare 40% is the price of staying fast.
05

In real life

3 min
Scale up or scale out

Stack Overflow: a few powerful servers

  • Its engineers have publicly described serving a huge site from a small number of large servers
  • Few machines to run, debug and pay for
  • Relies on tuned code and plenty of headroom per box

Works when traffic is steady and a small team wants few moving parts.

Netflix: many small, stateless servers

  • Has publicly described running services as many interchangeable cloud instances
  • Adds and removes instances as traffic rises and falls
  • Needs load balancers, shared stores and automation

Works when traffic swings a lot and losing one machine must not matter.

06

What goes wrong

3 min
Assuming latency grows in proportion to load

It stays flat, then goes vertical near saturation. A server at 70% CPU can be one email away from collapse.

Instead

Load-test to find the knee and keep headroom below it.

Adding servers when CPU is low

If requests wait on the database or an API, more servers just add more waiting requests.

Instead

Find what requests are waiting on before scaling anything.

Clients retrying instantly and forever

Retries multiply traffic exactly when the server can least afford it.

Instead

Retry with and jitter, and cap the attempts.

07

Trade-offs

3 min
OptionFits whenYou payMain risk
Scale up (bigger machine)Capacity needed this week, or the databaseSteep prices at the top endSingle point of failure
Scale out (more machines)Uptime matters, traffic swingsLoad balancer, health checks, shared sessionsBottleneck moves to the database
Headroom (~60% CPU)Anything guests wait on~40% idle capacityPaying for unused power
Monday, 10:00. Sessions are in Redis now. Planning the next sale.
  1. Karim · CTO
    Let's go straight to 4 small servers with autoscaling. No more single machine.
  2. Lina · Senior engineer, your mentor
    One 8-core box handles the sale at 56% CPU. Four servers mean four times the things to watch.
  3. Karim · CTO
    And when your one box dies during the sale, StayHub is down.
08

Your job as tech lead

4 min

Claude Code can write the code for this. You still own the decisions, and you have to know what good looks like to accept or reject its work.

Decisions you own
  • How much headroom you keep: a 225 req/s sale needs the 8-core box (~400 req/s) before the email goes out, not after.
  • Bigger machine or more machines. A second server only works once sessions live outside the server's memory.
  • Which numbers you watch during a sale (CPU, queue, p99, error rate) and at what level someone gets paged.
Tell Claude Code
In two weeks marketing emails 60,000 guests about a flash sale. Last time traffic hit about 225 req/s, our 4-core server reached 100% CPU and p99 8 s. Each search costs about 20 ms CPU and 60 ms waiting on PostgreSQL. Plan the move to an 8-core server. Don't add a second server: sessions are in memory. Show the plan, expected CPU at 225 req/s and the rollback first. Then write a load test ramping to 250 req/s on staging, reporting CPU, p99 and errors.
Check before you accept
  • Do the arithmetic yourself: 8 cores × 50 = ~400 req/s, so 225 req/s is ~56% CPU.
  • The load test reports p99, not only the average, which hides guests who wait 8 s.
  • The rollback step is written down and doable in minutes.
Red flags
  • A second server behind a load balancer while sessions are still in memory.
  • More servers when CPU is low and latency high: the time is spent waiting.
  • Timeouts quietly raised so errors vanish from the graph while guests still wait.
09

Check yourself

2 min
Q1/3Reasoning
Each request needs 25 ms of pure CPU time. The server has 4 cores and nothing else is limiting. Roughly how many requests per second can it handle at 100% CPU?
req/s
How confident are you?

Next episode: two guests receive a confirmation for the same last room in Porto, and Lina asks why we trust the database to keep that from happening.

Finished?

Mark it complete to update your skill map.

Go deeper (optional)You don't need this to understand the lesson. Open it when a project needs the details: Technical deep dive, Interactive simulation, Visual diagram, Analogy, Check your understanding, Practice ladder, Explain it yourself.
10

Technical deep dive

Size the server for the next sale

Under every framework, a server is a few lines: a asks the operating system for a port and runs a function for each request.

ts
import { createServer } from "node:http";
const server = createServer(async (req, res) => {
  // runs once per request
  const hotels = await db.query("SELECT id, name FROM hotels WHERE city = $1", ["lisbon"]); // waits on the database
  res.writeHead(200, { "Content-Type": "application/json" });
  res.end(JSON.stringify(hotels));                                                       // CPU: build the JSON
});
server.listen(3000); // ask the OS: "send me every connection to port 3000"
Node.js: a process that listens on port 3000 and answers every request.
Worked example

Lina sizes the server for the next sale

Maya plans another flash sale in two weeks and expects the same 225 searches per second at peak. Make sure search stays under 200 ms, without rewriting StayHub.

  1. 1Measure the cost of one request

    From the server's metrics on a normal day: each search uses about 20 ms of CPU and waits about 60 ms on the database.

    Why: CPU per request is what fills the cores. Waiting time doesn't use a core, so it doesn't count toward the ceiling.

  2. 2Compute the ceiling

    4 cores × (1,000 ÷ 20) = 200 searches per second at 100% CPU.

    Why: It turns "is the server big enough?" into a number you can compare with the forecast.

  3. 3Compare with the forecast at a safe level

    225 is above the ceiling. And we don't want 100%: at 90% a request takes about 10× its normal time. Target about 60% busy at peak, so we need a ceiling of about 225 ÷ 0.6 ≈ 375 per second.

    Why: Near 100% the wait in line dominates. Sizing for 60% keeps spikes in the flat part of the curve.

  4. 4Pick the cheapest fix that gets there

    An 8-core machine gives about 400 per second: 225 ÷ 400 ≈ 56% busy. It needs no code change. A second 4-core server would also work, but our sessions live in memory, so it would log guests out.

    Why: With two weeks to go, a change with no code to write is the safest one.

  5. 5Prove it before the sale

    Run a load test at 250 per second against the new machine and watch CPU and p99. Expect about 60% CPU and a p99 well under 200 ms.

    Why: The ceiling is an estimate. A load test finds the real knee of the curve before guests do.

The whole thing in one line: Lina measured what one search costs, turned it into a ceiling (200/s), sized for 60% busy instead of 100% (about 375/s needed), and picked an 8-core machine because it needs no code change.
UtilisationLatency multiplier (≈ 1 / (1 − ρ))A 50 ms request takes…
50%2×~100 ms
70%3.3×~170 ms
80%5×~250 ms
90%10×~500 ms
95%20×~1 s
99%100×~5 s, then timeouts
A simplified single-queue model. Real systems differ in the details, but the hockey-stick shape is universal.
11

Interactive simulation

12

Visual diagram

Click a component

One request's life inside a server, and what happens when you need more than one.

13

Analogy

  • At a hotel reception"Reception, ground floor"In a serverThe port where requests arrive
  • At a hotel receptionReceptionists working side by sideIn a serverThreads sharing one process
  • At a hotel receptionHow fast they workIn a serverThe CPU
  • At a hotel receptionDesk space and the archive roomIn a serverMemory and disk
  • At a hotel receptionA line out the doorIn a serverRequests queueing when the server is full
  • At a hotel receptionOpening a second identical deskIn a serverAdding a stateless server

Where the analogy breaks: a receptionist helps one guest at a time. One server thread can juggle thousands of requests, as long as most of them are just waiting for an answer from somewhere else.

14

Check your understanding

Q1/4Debugging
Your API latency rose from 100 ms to 2 s over an hour, but app-server CPU is steady at 20%. What do you investigate first?
How confident are you?
15

Practice ladder

Step 1 · Trace

Trace the hotel-page load

Estimation Warm-up

StayHub's 8-core server now also serves hotel pages. Each hotel page uses 40 ms of CPU and waits 30 ms on the database.

On Sunday evening, 120 hotel pages per second arrive (and nothing else).

  1. What is the ceiling for hotel pages per second on 8 cores?
  2. How busy is the CPU at 120 per second?
  3. Using the table from this lesson, roughly how long does a hotel page take compared with when the server is idle?
Step 2 · Complete

Finish Lina's sizing note for the summer peak

Estimation Practice

Maya expects the summer peak to reach 300 searches per second. Each search uses 20 ms of CPU. Lina's sizing note has two gaps:

  1. Measure the cost of one request: 20 ms of CPU per search.
  2. Compute the ceiling per core: 1,000 ÷ 20 = 50 searches per second per core.
  3. Compare with the forecast at a safe level: ___
  4. Pick the cheapest fix that gets there: ___
  5. Prove it: load-test at a bit above 300 per second and watch CPU and p99.

Fill in steps 3 and 4. Our servers are now stateless (sessions are in Redis), and machines come with 8 or 16 cores.

Step 3 · Solve

Slow owner reports, idle CPU

Debugging Practice

Hotel owners like Nadia download a monthly report from /owner/reports. On the 1st of the month, Omar sees:

  • Report p99: 14 s (normally 900 ms)
  • App server CPU: 18%
  • Database CPU: 95%
  • Each report runs one big query that takes about 800 ms on a quiet database

Karim wants to add two more app servers before tomorrow. Explain what's really happening and what you'd do instead.

Step 4 · Decide

Downsize to save money?

Decision Stretch

Karim looked at the cloud bill. "Our 8-core server averages 5% CPU. Let's go back to 4 cores and save €180 a month."

The facts: - Average load: 20 searches per second. Normal Friday peak: 45 per second. Sale nights: up to 225. - Each search uses 20 ms of CPU. - Maya plans one sale a month. One sale night brings about €9,000 of bookings.

Write a short decision: "We chose X over Y because…, accepting the risk of…"

16

Explain it yourself

Explain what a server is, and why a server at 95% CPU is much slower than one at 60%, even though it's only doing ~1.5× more work.

0 words