Part 01 · Thinking About Scale

Vertical vs Horizontal Scaling

Scale upScale outCost curve

A bigger server or more servers? What each buys you, where each stops, and why most teams should scale up first.

⏱ 9 min · Station 6 of 93

The problem

We know the shop's bottleneck: the database's one CPU thread, full at about 580 requests per second. There are two ways to give it more room. Buy a bigger machine for it, or spread the work over more machines. The first is called scaling up (vertical), the second scaling out (horizontal). Teams argue about this as if one were modern and the other old-fashioned. They solve different problems, and most real systems use both.

The idea

  • Scale up (vertical): same number of machines, each one bigger: more CPU cores, more memory, faster disks. Nothing in the code changes.
  • Scale out (horizontal): more machines of the same size, each doing part of the work. The code and the architecture must allow it: something has to spread requests across the machines, and no machine may hold anything the others need.

The rule of thumb most teams end up with: scale out what's stateless, scale up what holds state, and only split state when one machine truly can't hold it. App servers hold no data, so adding more is easy. A database holds the data, so spreading it means replicas (Part 5) or shards (Part 6), both with real costs.

How it works

Scaling up:

  1. Move the database to a machine with more cores (in a cloud, change the instance size; a restart or failover is usually needed).
  2. PostgreSQL runs one process per connection, so with more cores, more queries really run at the same time.
  3. Capacity grows roughly with cores, until something shared runs out: memory bandwidth, locks inside the database, disk.
  4. There is a ceiling: the biggest machine you can buy. And it is still one machine: if it fails, everything stops.

Scaling out:

  1. Run several copies of the app.
  2. Put a load balancer in front to spread requests over them (next part of the course).
  3. Every request must be serveable by any copy. If a user's session lives in one copy's memory, the next request that lands elsewhere doesn't know them (that's the first lesson of Part 2).
  4. There's no hard ceiling and one copy failing isn't an outage, but you've added a component and network hops.

Scaling out only helps where the bottleneck is. Three app servers in front of our full database would do nothing; that's why we scale it first.

Predict first

The database runs on 1 CPU thread and tops out near 580 requests/s. You give it a second thread. What capacity do you measure?

Visual

scale up (vertical)scale out (horizontal)PostgreSQL1 CPU threadPostgreSQL2 CPU threads~580 req/s~1,075 req/ssame code, bigger boxload balancerapp 1app 2app 3needs: stateless app, a load balancerscale up: give the bottleneck a bigger machine, no code changesscale out: add machines, but the app must not keep users' state inside it
Left: scaling up gives the same database box a second CPU thread (measured: from about 580 to about 1,075 requests/s). Right: scaling out adds app boxes behind a load balancer, which only works when any box can serve any request.

Common misconception

"Horizontal scaling is always better." It's better for things that hold no state, and for surviving the loss of a machine. For a database, scaling up is often the cheapest and safest step for a long time: a large database server today has dozens of cores and hundreds of gigabytes of memory, and moving to it changes no code. Splitting a database early (sharding) buys complexity you pay for every day.

"Scaling up is a dead end." It has a ceiling, but the ceiling is high, and you can scale up the database and scale out the apps at the same time. The question is not which philosophy is right but which resource is the bottleneck and what's the cheapest way to give it room.

Try it

Measured on the lab (Intel i3-10110U: 2 cores, 4 threads, 7 GB RAM). To give the database two threads on a 4-thread laptop, k6 runs on one thread for this test (K6_CPUS=0). Docker can change a running container's CPUs without a restart, which is our stand-in for "move to a bigger machine":

cd ../../lab
export K6_CPUS=0              # k6 gets one thread, so the database can have two
echo "== database on 1 CPU thread"
docker update --cpuset-cpus "2" syslab-db-1 >/dev/null
./k6.sh product.js -e RATE=80 -e DURATION=15s >/dev/null 2>&1
./ramp.sh product.js "500 700" 20s
echo
echo "== database on 2 CPU threads (scale up)"
docker update --cpuset-cpus "1,2" syslab-db-1 >/dev/null
./ramp.sh product.js "500 700 900 1100" 20s
docker update --cpuset-cpus "2" syslab-db-1 >/dev/null   # back to one
== database on 1 CPU thread
 rate  got/s      p50      p95      p99  failed  dropped
  500     500   2.1 ms    14 ms    31 ms   0.0%       0
  700     583   814 ms   917 ms   946 ms   0.0%    1926

== database on 2 CPU threads (scale up)
 rate  got/s      p50      p95      p99  failed  dropped
  500     500   1.9 ms   2.7 ms   5.0 ms   0.0%       0
  700     700   1.9 ms   4.4 ms   8.4 ms   0.0%       0
  900     900   2.0 ms   8.4 ms    15 ms   0.0%       0
 1100    1075   127 ms   180 ms   212 ms   0.0%     353

Reading it:

  • One thread: 500 per second is fine, 700 is broken (583 got through, p95 0.9 s). Same as the earlier lessons.
  • Two threads: 700 and 900 per second are fine, p95 4 to 8 ms. At 1,100 asked, 1,075 got through with p95 180 ms: close to the new edge.
  • Capacity went from about 580 to about 1,075, ×1.85, with no change to the code, the queries or the app.
Database CPUCapacityp95 at 700/s
1 thread~580/s917 ms (broken)
2 threads~1,075/s4.4 ms

Why not exactly ×2? The second thread is a hyperthread: it shares a physical core with one of k6's threads, and the database's processes share caches and memory bandwidth. Real servers show the same shape: adding cores helps a lot, but each extra core helps a little less.

Trade-offs

Scale upScale out
Code changesnonethe app must be stateless, state must live elsewhere
Ceilingthe biggest machine availablevery high
One machine failseverything stops (unless you add a standby)the others keep serving
Operating itone thing to watcha load balancer, health checks, deploys across many copies
Costwithin one machine family, about linear with size; the very largest sizes cost extralinear, plus the load balancer and the extra network hops
Resizingusually a restart or failoveradd or remove copies while running

And the cost that isn't on the bill: every new kind of component is something your team must understand at 3 AM.

Key takeaway

Scale up gives the bottleneck a bigger machine with no code changes; it's simple and effective until the ceiling and leaves one machine as a single point of failure. Scale out adds machines and survives failures, but needs stateless services and a load balancer. Scale out the stateless parts, scale up the stateful ones, and split state only when you must.

Practice

Objective: find out what limits the shop after scaling up.

Challenge: with the database on two threads, the shop handled about 1,075 requests per second. Run it at 1,100 per second with two threads and take a docker stats sample. Is the bottleneck still the database, or has it moved to the app?

Expected result: a docker stats reading that names the bottleneck, and a decision about what to scale next.

Hint 1

With two threads, the database container can show up to 200% CPU.

Hint 2

Use docker update --cpuset-cpus "1,2" syslab-db-1, run k6 with K6_CPUS=0, and sample docker stats halfway through. Put it back to "2" afterwards.

Hint 3

If the app is well below 100% and the database is near 200%, which one would a third CPU help?

Solution
NAME           CPU %
syslab-api-1   29.74%
syslab-db-1    191.89%
requests   21796 (1084.2/s)
failed     0.00%
latency    avg 31 ms  p50 21 ms  p95 126 ms  p99 162 ms  max 195 ms
dropped    205 (k6 could not start them in time)
vus max    167

The database is at 192%: both threads full. The app is at 30%. The bottleneck is still the database; the second thread moved the cliff, it didn't move the bottleneck. More database CPU (or less work per page, like the index from the last lesson) is the next step. More app servers would still do nothing here, and they only start to matter when the app's CPU approaches 100%.

Interview check

Q: Your PostgreSQL server is at 90% CPU at peak. Would you add read replicas, shard, or move to a bigger instance?

Model answer

First check what uses the CPU: a missing index or a bad query is cheaper than any of the three. If the load is legitimate, moving to a bigger instance is usually the first step: no code changes, one maintenance window, and it buys a lot of room. Read replicas come next if reads dominate and the app can tolerate slightly stale reads. Sharding is the last step, when writes or data size outgrow the biggest machine, because it changes the application and every query that crosses shards.

Q: Why is it easy to scale out app servers but hard to scale out a database?

Model answer

Stateless app servers can each serve any request, so a load balancer can spread traffic and you add or remove copies freely. A database holds the state: every copy must either have all the data (replication, with lag and failover questions) or a slice of it (sharding, with routing and cross-shard queries). Keeping copies correct is the hard part, and it's what the Distributed Systems course is about.

Station stamp

Finish the chapter

Done reading? Press Complete to save your progress. XP is Pro-only — on Pro this chapter is worth +10 XP.

Go Pro to earn XP →