Cloud & Data Centers

Scaling Up vs. Scaling Out

When to buy a bigger machine, when to add more, and how to find the bottleneck before either.

Key takeaways

  • Scale up, also called vertical scaling, when you want the quickest fix with no changes to the application.
  • Scale out, or horizontal scaling, when you need resilience or more capacity than one machine can give.
  • Scaling out works smoothly for stateless parts and is much harder for databases that accept writes.
  • Measure first, because adding capacity to the wrong component changes nothing.
  • Autoscaling needs sensible limits, quick startup and a dependency that can take the extra load.

Scaling up means giving one machine more: more processor cores, more memory, faster storage. Scaling out means adding more machines and sharing the work among them. Up is simpler and has a ceiling. Out can grow much further and survive the loss of a machine, but your software has to be ready for it.

Before choosing either, find out what's actually running short. Many performance problems are solved by an index, a cache or a fixed query, at no extra monthly cost.

Two directions of growth

Say your web server struggles at midday. Scaling up, you move it to a machine with twice the cores and memory. In the cloud that's often a change of instance size and a restart. Nothing about your code has to change.

Scaling out, you run three copies of the server behind a load balancer that spreads requests among them. If one fails, the other two carry on. To add capacity, you add a fourth.

Vertical and horizontal scaling
Scale up One bigger machine Little or no code change Limited by the largest size Still a single point of failure Scale out Several cooperating machines Needs load balancing Grows in smaller steps Can survive losing one VS Scale up One bigger machine Little or no code change Limited by the largest size Still a single point of failure Scale out Several cooperating machines Needs load balancing Grows in smaller steps Can survive losing one

Find the bottleneck first

A bottleneck is the resource that's full while others wait. Check these:

  • CPU: consistently busy cores and rising response times.
  • Memory: swapping to disk, or processes being killed.
  • Storage: slow reads and writes, long queues.
  • Network: a link near its capacity.
  • Database: slow queries, lock waits, too many connections.
  • External services: time spent waiting on someone else.

On Linux, top, free -h and vmstat 5 give a quick view. On Windows, Task Manager and Resource Monitor do the same job. Application timing or traces show where requests spend their time.

If the processor is idle and pages are still slow, a faster processor won't help.

Cheaper fixes to try first

Work that you avoid doesn't need capacity. Add a database index for a frequent search. Fix a loop that runs one query per item when a single query would do. Cache results that many users share. Move static files to a content delivery network. Push slow tasks such as sending email onto a queue.

These changes can reduce load dramatically, and they pay off on one machine or many.

The server is slow. What now?
Is a slow query or wasteful code to blame? YES Fix it before buying capacity 1 NO Is repeated work being redone for each visitor? YES Add caching 2 NO Is there a larger machine that gives ample headroom? YES Scale up for the quickest relief 3 NO Can requests go to any copy of the app? YES Scale out behind a load balancer 4 NO Remove shared state first Move sessions and files off the server, then add copies. Is a slow query or wasteful code to blame? 1 IF YES Fix it before buying capacity NO Is repeated work being redone for each visitor? 2 IF YES Add caching NO Is there a larger machine that gives ample headroom? 3 IF YES Scale up for the quickest relief NO Can requests go to any copy of the app? 4 IF YES Scale out behind a load balancer NO Remove shared state first Move sessions and files off the server, then add copies.

A worked example

Say one server handles about 200 requests per second before response times climb, and you expect peaks of 500. These figures are invented to show the reasoning.

Scaling up, you might choose a machine with four times the capacity, good for roughly 800 if your software uses the extra cores. You get comfortable headroom with a single change. If that machine fails, though, you serve nothing until a replacement is running.

Scaling out with the original size, three servers give about 600, which covers the peak. But if one fails, the remaining two offer 400, short of 500. Four servers leave 600 after a failure. You now have both capacity and tolerance for a loss, provided the load balancer is redundant too and the database behind them can handle 500 requests per second of work.

That last condition is where plans often break. Adding web servers moves the pressure downstream, so check each tier in turn.

Why scaling up eventually stops

Larger machines exist, but only up to a point, and the biggest sizes often cost more per unit of capacity. Software may not use extra cores well; a task that runs on a single thread finishes no sooner on a machine with 64.

Resizing frequently needs a restart, which means downtime unless you have a standby. And one very large machine is still one machine. When it fails, everything it hosted fails together.

None of this makes it a bad choice. Modern servers are powerful, and many successful applications run comfortably on one well-sized database server for years.

Making an application ready to scale out

The main requirement is that any copy can handle any request. That means the app tier is stateless: it keeps nothing important between requests on its own disk or memory.

  • Store sessions in a shared cache, a database or a signed token.
  • Save uploads to shared or object storage, as compared in object, block and file storage.
  • Send logs to a central place.
  • Run scheduled jobs in one coordinated place, not on every copy.
  • Build servers from an image or script so that new ones are identical.

"Sticky sessions," which send a returning visitor to the same server, can paper over local state. They also unbalance load and lose sessions when that server goes.

Databases are the hard part

Reads are usually the easier half. Read replicas hold copies of the data and answer queries, taking pressure off the primary. Because copies lag slightly, a user may not immediately see their own change unless you read it from the primary.

Writes are harder. Sharding splits data across several databases by a key, such as customer ID. Each shard handles part of the load. Queries that span shards become slow or awkward, transactions across them are complicated, and a popular key can create a hot shard. Choosing the key badly is expensive to undo.

Connections can be a limit of their own. Fifty application copies each opening many connections may exhaust a database, and a connection pooler often solves that. Databases in the cloud compares systems that distribute data for you.

Limits on parallel speedup

Adding workers helps only the part of a job that can run in parallel. Amdahl's law states the limit. If a tenth of a task must happen one step at a time, no number of processors can make it more than ten times faster.

Example: maximum speedup when 10% of a task is serial
1 worker 1x 2 workers 1.82x 4 workers 3.08x 8 workers 4.71x 16 workers 6.4x 64 workers 8.77x 1 worker 1x 2 workers 1.82x 4 workers 3.08x 8 workers 4.71x 16 workers 6.4x 64 workers 8.77x

Basis: Amdahl's law with a serial fraction of 0.1.

Web requests are mostly independent, so web tiers often scale well. The serial part tends to hide in shared resources: one database, one lock, one counter everybody updates.

Keeping copies consistent

With several servers, they need to stay the same. If each was set up by hand, one will end up with a different library or setting, and you'll chase a bug that appears on every third request.

Build all copies from one image or from infrastructure as code, and release new versions to all of them through the same pipeline. During a rollout, old and new versions serve side by side, so database changes and shared cache formats have to work with both.

Shared caches deserve care too. If a popular cached item expires, many copies may rush to rebuild it at the same moment and flood the database. Letting only one rebuild while others briefly serve the old value avoids that stampede.

Health checks tie it together. The balancer should send traffic only to copies that are ready, and a new instance shouldn't report ready until it has loaded what it needs.

Autoscaling

Cloud platforms can add and remove copies automatically based on signals such as CPU use, request count or queue length. Pick a signal that rises before users suffer.

Set a minimum high enough to survive a failure, and a maximum that protects your budget and your database. New instances take time to start, so sudden spikes may outrun them. Scheduled scaling helps with predictable peaks. Scale down gradually, and let departing servers finish requests in progress.

Serverless platforms handle much of this for you, with their own limits and delays when starting cold. See serverless hosting.

Cost and headroom

Scaling out lets you match spending to demand, paying for extra copies only at busy times. Each copy has overhead, though, and a load balancer, shared cache and extra data transfer add to the bill. Scaling up has less machinery and may be cheaper at modest size.

Whatever you choose, keep headroom. As utilization approaches 100 percent, waiting times climb steeply. Also plan to lose a machine: with three servers, the remaining two need room for the whole load.

Test before you need it

Load testing shows where the limit lies. Use a test environment, raise traffic gradually and watch which resource fills first. Only test systems you own or are authorized to test, and check your provider's rules for large tests.

After fixing one bottleneck, expect another. Repeat the test, record the capacity of each tier and compare it with your expected growth. That gives you a plan with lead time, which is far more comfortable than resizing during an outage.

Common questions

Can I do both?

Yes, and most systems do: larger database servers, and several modest application servers in front of them.

Does Kubernetes make an app scalable?

It can start and stop copies for you. It can't make software that depends on local state behave correctly with many copies. Kubernetes explained describes what it handles.

Which one costs less?

It depends on your traffic and your prices. Compare the monthly cost of each design with enough headroom to survive your expected peak, and include the engineering time needed to get there.

Written for 4Internet with AI writing tools and not reviewed line by line by a person. Check anything you rely on with the maker, provider or standard concerned. Report an error.

Keep reading

All 45 articles in Cloud & Data Centers →

Latest news on this topic