Course contents · Lesson 7 of 19

Scaling Up vs Scaling Out: A Beginner's Guide

Scaling and Capacity · Lesson 7 of 19 · 10 min Engineer

Your app runs on one server, and every Monday morning it crawls with the CPU stuck at 100 percent. You can move it to a bigger server, or add servers and share the work. That choice is scaling up vs scaling out. This lesson explains both, why databases make it harder, how autoscaling works, and which to try first.

Scaling up: a bigger machine

Scaling means changing how much work a service can handle. Scaling up, or vertical scaling, gives the one machine running it more CPU, memory or disk. In the cloud, that means a larger instance type, the provider's name for a virtual machine (VM) size.

Its strength is simplicity: the code does not change, and there is still one copy of everything. Its limits:

  • A ceiling. A machine has a fixed number of CPUs and memory slots. The largest cloud instance types have hundreds of virtual CPUs and terabytes of memory, and then there is no next size.
  • Cost and restarts. Each size up costs more per hour, and on most clouds resizing a VM means stopping or restarting it, so teams size for the busiest hour and pay for that size all day.
  • A single point of failure, any part whose failure takes the whole service down. One machine, however large, is one; lesson 11 shows how to hunt for them.

Scaling out: more machines behind a load balancer

Scaling out, or horizontal scaling, adds machines of the same size, each running a copy of the app called an instance. A load balancer, the server from lesson 5 that spreads requests across your app's servers, sends each request to one of them. Removing machines is scaling in; moving to a smaller one is scaling down.

In the diagram, both sides have 16 virtual CPUs in total, an illustrative size. Notice what a failure does: on the left the service stops, while on the right three of four instances keep serving.

Side-by-side diagram with illustrative sizes. Scale up, vertical: users send requests to one big server that grew from 4 to 16 vCPUs; simple, with no code changes, but capped at the largest machine size, and one failure stops everything. Scale out, horizontal: users send requests to a load balancer that spreads them across four equal servers of 4 vCPUs each; lose one and three of four keep serving, servers can be added or removed as traffic changes, but the app must be stateless
Same capacity, different failure: one dead machine loses all of it on the left and a quarter on the right.

That is the resilience, as long as the other three can carry the extra load. Cost is flexible too: add instances for a busy hour and remove them afterwards, with no restarts, so the bill follows traffic.

The price is that the app must be stateless. State is data the app must remember between requests, such as who is logged in or what is in a cart. A stateless instance keeps none of it locally; it stores state in a shared place, such as a database, so any instance can serve any request.

Keep logins in an instance's memory instead, and a user whose next request lands elsewhere appears logged out. Sticky sessions, a load balancer setting that sends each user to the same instance, hide this only until that instance dies. The Twelve-Factor App explains the rule.

The hard part is state: scaling a database

A database cannot be stateless: keeping data is its job. Adding a second database server does not help by itself, because both would need the same data and every write would have to reach both. So databases scale out in three main ways:

  • Read replicas. A read replica is a read-only copy. The primary, the one server that accepts writes, sends each change on to the replicas, and the app spreads its reads across them. Many apps read far more than they write, so this lifts much of the load.
  • Sharding, or horizontal partitioning, splits the rows across several database servers by a shard key, such as customer ID. Each shard takes its share of the writes, so writes scale too, but queries across shards get harder and moving data between them is a project.
  • Caching keeps copies of often-read results in fast memory, so many reads never reach the database; lesson 8 covers it.

In the diagram, notice that only the reads fan out: every write still goes to the one primary.

Diagram of a database. App servers send writes, a pink arrow, only to the primary, which accepts writes, and send reads, green arrows, to replica 1 and replica 2, two read-only copies. Dashed arrows show the primary copying changes to both replicas asynchronously, so the replicas may lag behind. Footer: more replicas add read capacity, not write capacity
Reads spread across copies but writes have one home, so a write-heavy database needs sharding or a bigger primary.

Replicas usually copy changes asynchronously: the primary confirms a write first and sends it on afterwards, so a replica can lag. A user who saves a new address, then loads a page read from a lagging replica, may still see the old one. Managed Databases in the Cloud shows this on Amazon RDS.

Autoscaling: adding instances automatically

Autoscaling adds or removes instances automatically, based on a signal, usually average CPU. For workers that take jobs from a queue, queue depth, the number of jobs waiting, is often better: workers stuck on a slow service show low CPU while the line grows. Queues come in lesson 9.

The loop stays between a minimum and a maximum number of instances. It uses the health check from lesson 5, a test request the load balancer sends to see whether a server is alive, and draining: the load balancer sends an instance no new requests and lets open ones finish. Notice that a new instance gets traffic last.

Flowchart with example thresholds. Every minute, check average CPU. Between 30 and 70 percent, no change. Scale out: CPU above 70 percent for 3 minutes, add an instance, never more than the maximum; the health check passes and the load balancer marks it healthy; traffic flows to it and more instances share the load. Scale in: CPU below 30 percent for 15 minutes, pick an instance to remove, never fewer than the minimum; drain it so open requests finish; remove it and stop paying for it. A note: scaling out lags because a new instance takes minutes, so keep headroom so the current instances cope meanwhile
Scaling in can wait, but scaling out is always late: a new instance must boot and pass its health check first.

That lateness is the catch. The metric must stay high long enough to trigger, then the instance must boot, start the app and pass its health check. For a VM that often takes minutes, while a spike can arrive in seconds.

So autoscalers aim well below 100 percent, and that spare capacity is called headroom. With illustrative numbers, instances at 70 percent CPU that meet a sudden 30 percent rise in traffic reach about 91 percent while help boots. At 85 percent, the same rise demands about 110 percent, and requests queue or fail. Cloud Autoscaling Explained and Autoscale an App on Kubernetes with HPA show both hands-on.

A rule of thumb: up first, out when you must

Scale up first while one machine is enough: a bigger instance type is the fastest change, with no code changes and no new parts. Scale out when one of these is true:

  • The service must survive a machine failure, because one crash or resize would spend more of the error budget than you can afford: 43.2 minutes per 30 days at 99.9 percent.
  • You are near the largest size, or one machine can no longer handle your peak.
  • Traffic swings a lot, so autoscaling saves money.

Most teams combine the two: stateless app instances that autoscale, in front of a database scaled up as far as is sensible, with replicas and caching before sharding. Lesson 10 covers how much capacity to plan for.

Check yourself

Your autoscaler adds instances when CPU passes 95 percent. New instances do arrive, yet every spike still brings minutes of slow pages. Why?

New instances take minutes to boot and pass health checks, and at 95 percent the current ones have almost no headroom meanwhile. A lower target, such as 70 percent, leaves room.

After scaling a shop out from one instance to three, users are logged out at random. What went wrong, and what is the fix?

Each instance keeps logins in its own memory, so a request that lands on another instance finds no session. Storing sessions in a shared database or cache makes the instances stateless.

You add two read replicas, but the database still struggles during a flash sale where almost every request places an order. Why did the replicas not help?

Placing an order is a write, and writes still go only to the primary; replicas take reads alone. A bigger primary, or sharding the orders, adds write capacity.

Go deeper