Understanding Horizontal vs. Vertical Scaling

Vertical scaling gives one server more CPU and memory, horizontal scaling runs more of them. How they differ in limits, availability, cost and code, what an app needs before it can scale out, why databases scale up, and how autoscaling fits in.

Marei Morsy··14 min read
Horizontal vs. vertical scaling: a load balancer in front of four 0.5 vCPU replicas with room for one more, next to a small server grown into one 2 vCPU server
On this page

Key takeaways

  • Vertical scaling gives one instance more CPU, memory or storage: no code changes, but a ceiling, a single point of failure and often a restart.
  • Horizontal scaling adds identical replicas behind a load balancer: it goes further, survives failures and follows traffic, but the app must be stateless.
  • Before scaling out, move sessions, uploads, locks and scheduled tasks out of the replica, and size database connection pools for the replica count.
  • Databases usually scale up: fix queries, cache hot reads and pool connections before reaching for read replicas or sharding.
  • Per vCPU both cost the same on a pay-per-use platform, but autoscaled replicas cost less when traffic varies, because quiet hours run fewer of them.
  • On Cloider, apps scale both ways (up to 2 vCPU and 4096 MB per replica, 1 to 100 replicas), databases resize to larger plans, and clusters scale pods with no nodes to manage.

Your app is fast with ten users. With ten thousand, pages take seconds, requests time out and the CPU graph sits at the top. It needs more capacity, and there are two ways to give it some: make the machine it runs on bigger, or run more machines. The first is vertical scaling, also called scaling up. The second is horizontal scaling, or scaling out.

Both work, and most production systems end up using both. But they differ in how far they go, what happens when something fails, what they cost and what they ask of your code. Many scaling problems come from using the wrong one for a component: adding replicas to an app that keeps its sessions in memory, or growing a single server until it can't grow any more.

This article explains how vertical and horizontal scaling work, their tradeoffs, what an application needs before it can scale out, why databases are different, how autoscaling fits in, and what each costs. The examples use Cloider apps, databases and Kubernetes clusters, but the ideas hold on any cloud.

The short answer

Vertical scaling gives one instance more CPU, memory or storage. It needs no code changes and keeps everything in one place, but it stops at the largest size available, a single instance is still a single point of failure, and resizing often means a restart.

Horizontal scaling runs more identical instances and spreads requests across them. It goes much further, keeps serving when one instance dies, and can follow traffic up and down automatically. In return your app has to be stateless: nothing a later request needs may live only in one instance's memory or on its disk.

The common pattern: scale stateless web apps and workers horizontally, scale databases vertically, and size each instance sensibly before you multiply it.

What vertical scaling is

Vertical scaling, or scaling up, means giving the instance you already have more resources: more vCPUs, more memory, a larger disk, faster storage. For a virtual machine you pick a bigger instance type. For a container you raise its CPU and memory. For a managed database you move to a larger plan.

Its appeal is that nothing else changes. The app runs the same code with the same configuration, it just has more room. There is still one copy, so there are no questions about shared state, no load balancer to set up and no data to keep in sync between machines. For a database that matters a lot: every transaction, join and lock happens inside one server, with no network between the pieces.

It also has hard limits:

  • There is a ceiling. Every platform has a largest size. On a public cloud it may be hundreds of vCPUs, on a managed platform it is usually much lower. Once you reach it, vertical scaling is over.

  • One instance is one point of failure. A crash, an out of memory kill, a kernel update or a failed host takes the whole service down, however large the instance is.

  • Resizing often restarts it. Most clouds stop a virtual machine to change its size, and a database restarts to take on its new CPU and memory. For a single instance, that is downtime.

  • You pay for the peak all the time. An instance sized for Monday morning's traffic costs the same at 3 a.m. on Sunday. Scaling down is another manual resize, and disks usually can't shrink at all.

  • Not all software uses more cores. A Node.js process runs your JavaScript on one thread, and a process of the default CPython build runs Python code on one core at a time. Giving it 8 vCPUs mostly buys idle cores, unless you run several processes, and then you are already scaling horizontally inside one machine.

What horizontal scaling is

Horizontal scaling, or scaling out, means running more identical copies of your app, called instances, nodes or replicas, with a load balancer in front that spreads requests across them. To handle twice the traffic you run twice the replicas, and when traffic drops you remove some.

What you get for the extra moving parts:

  • No practical ceiling for the app itself. Need more capacity? Add replicas. The limit moves to what the replicas share, such as the database.

  • Failures become routine. When a replica crashes or its server goes away, the load balancer stops sending it traffic and the others keep serving while it is replaced. Run at least two replicas, on different servers, and losing one is a non-event.

  • Deploys without downtime. A rolling update starts new replicas, waits until they are healthy, and only then removes the old ones.

  • Capacity follows traffic. An autoscaler adds replicas when they get busy and removes them when they are quiet, so you pay for the peak only while it lasts.

The price is paid in design. Because any replica can answer any request, a replica can't keep anything that a later request depends on, and the services behind the replicas have to cope with many clients instead of one. The section after the table lists what that means in practice.

Vertical vs. horizontal scaling at a glance

Vertical scaling (scale up)

Horizontal scaling (scale out)

What changes

The size of one instance: CPU, memory, storage

The number of instances

Upper limit

The largest instance or plan available

What the instances share, such as the database

Code changes

None

The app must be stateless

Availability

One instance, one point of failure

Survives losing a replica

Making the change

Often a restart, so downtime for a single instance

Replicas are added or removed while the app serves

Scaling down

Manual, and disks rarely shrink

Automatic with an autoscaler

Cost

Sized and billed for the peak, all the time

Follows the traffic

Complexity

Low: one thing to run

Higher: load balancing, shared state, more connections

Best for

Databases, stateful or single-instance software, a quick fix

Web apps, APIs, workers, anything that serves many independent requests

What your app needs before it can scale out

Most of the work of horizontal scaling is making sure nothing important lives inside a single replica. Check these before you raise the replica count:

  • Sessions outside the process. Sessions kept in memory or in local files log users out whenever their next request lands on another replica. Store them in Redis or the database, or use signed cookies. Sticky sessions only hide the problem until a replica restarts, and they spread load unevenly.

  • Files in object storage. Uploads written to the local disk exist on one replica only and disappear when it is replaced. Write them to a bucket. Why your application should never store files locally shows how.

  • Shared locks and counters. An in-memory cache is fine for data that may be a little stale, but every replica has its own copy. Anything that must be consistent, such as rate limits, locks or counters, belongs in Redis or the database.

  • Scheduled tasks that run once. If every replica runs the scheduler, the nightly report goes out once per replica. Run scheduled work as a separate cron job, or take a lock before each run.

  • Slow work on a queue. Push emails, exports and image processing to a queue, and run workers that scale on their own, instead of doing the work inside web requests.

  • Connection pools sized for the replica count. Every replica opens its own pool of database connections, so the total grows with every replica you add.

  • Health checks and graceful shutdown. The load balancer needs to know when a replica is ready, and a replica should finish its in-flight requests when it receives SIGTERM instead of dropping them.

  • Logs on standard output. With ten replicas you can't read ten log files on ten disks. Write logs to stdout and let the platform collect them.

In many frameworks this is configuration rather than code. A Laravel app, for example, moves its sessions, cache, queue, uploads and logs out of the replica with a few environment variables:

Bash
SESSION_DRIVER=redis
CACHE_STORE=redis
QUEUE_CONNECTION=redis
FILESYSTEM_DISK=s3
LOG_CHANNEL=stderr

Django gets the same from SESSION_ENGINE and django-storages, and Rails from a Redis cache store and Active Storage.

Why databases usually scale up

Stateless replicas are easy to multiply because they share nothing. A database is the thing they share, and spreading one across machines is a different problem: every write has to land in one agreed place, and every read has to see it.

The horizontal options exist, but each has a cost:

  • Read replicas copy the data from a primary server and answer reads. They help read-heavy apps, but every write still goes to one server, and replicas lag behind it, so a user may not see what they just saved.

  • Sharding splits the data across servers by a key, such as the customer ID. Writes scale too, but queries and transactions across shards, and moving data between them, become your problem.

  • Distributed databases such as CockroachDB, YugabyteDB and Cassandra, or Vitess for MySQL, spread the data for you, with their own tradeoffs in latency, features and operations.

So the usual order for a relational database is: fix slow queries and missing indexes, cache hot reads in Redis, put a connection pooler in front, scale up, and only then consider read replicas or sharding. A single well-sized PostgreSQL or MySQL server handles far more than most applications ever send it.

On Cloider, managed databases scale vertically. Resizing moves a PostgreSQL, MySQL, MongoDB or Redis database to a larger plan with more CPU, RAM and storage. It keeps its data, host and password, and restarts once to apply the new size:

Bash
cloider db resize orders-db --plan plus --wait

A database can only move to a larger plan, because its storage volume can grow but not shrink. Watch the database's Insights tab and resize before storage or memory runs out, not after.

The same goes for other stateful software you run yourself, such as queues and search engines: they scale out only through their own clustering, never by simply running more copies.

Autoscaling: letting the platform decide

Scaling by hand means someone watches graphs and reacts. Autoscaling does it from metrics.

Horizontal autoscaling is the common kind. You set a minimum and a maximum number of replicas and a target, such as 70% average CPU. When the replicas run above the target, the autoscaler adds replicas, and when they have been quiet for a while, it removes some. In Kubernetes this is the HorizontalPodAutoscaler, and most platforms have an equivalent.

A few settings decide whether it works well:

  • The minimum is your availability floor. Keep it at 2 or more for anything users depend on, so one replica can restart while the other serves.

  • The maximum is your cost cap. It also keeps the replicas from opening more database connections than the database accepts.

  • Scale on CPU unless your memory frees up. Many runtimes keep the memory they once took, so an app scaled on memory can climb to its maximum and stay there.

  • Thresholds are relative to the replica's size. A replica given too little CPU always looks busy, and one given too much never triggers a scale out. Size replicas from what they really use.

  • Scaling out takes time. A new replica has to start and pass its health check. A 70% target leaves headroom for a spike while replicas are added.

Vertical autoscaling adjusts the size of each instance from its usage, as the Kubernetes Vertical Pod Autoscaler does. It is mostly a right-sizing tool, it has traditionally restarted pods to apply new sizes, and it shouldn't act on the same CPU or memory metric as a horizontal autoscaler on the same workload, or the two work against each other.

What it costs

On a pay-per-use platform both directions cost the same per vCPU: four replicas of 0.5 vCPU use as much as one replica of 2 vCPU. The difference is what you pay while the traffic isn't there.

Take an app that needs about 2 vCPU during four busy hours a day and much less the rest of the time. On Cloider, at the time of writing, an app pays $32 per 30 days for each compute unit, which is 1 vCPU or about 2 GB of memory, whichever is larger:

  • Scaled up: one replica of 2 vCPU and 4096 MB, sized for the peak. That is 2 compute units, so $64 per 30 days, busy or not. It can't grow past the 2 vCPU a replica may have, and when that replica restarts, the app is down.

  • Scaled out: replicas of 0.5 vCPU and 1024 MB, autoscaling between 2 and 6 at 70% CPU. Two replicas run 20 hours a day and six during the busy hours, an average of about 2.7 replicas at half a compute unit each: about $43 per 30 days, with never fewer than two replicas serving.

The spikier the traffic, the bigger the gap. For flat, predictable load the two cost about the same, and the simpler setup wins. A database is priced by its plan, so there the cost of scaling up is the step to the next plan.

Five mistakes to avoid

Scaling out a stateful app. Users are logged out at random, uploads go missing, carts empty themselves. It looks like a load balancer bug, but it is state that lives in one replica. Move it out first.

Adding replicas when the database is the bottleneck. If requests are slow because they wait on the database, more app replicas only send it more queries over more connections. Ten replicas with a pool of 20 open up to 200 connections, twice PostgreSQL's default limit of 100. Find the slow queries, size pools for the replica count or add a pooler such as PgBouncer, and scale the database up.

Counting on one large instance for availability. A bigger single replica handles more traffic, but it fails just as completely as a small one. Two medium replicas usually beat one large one for the same money.

Hiding a memory leak with more memory or more replicas. If memory climbs until a replica is killed and replaced, both only stretch the cycle. Watch memory over time and fix the leak.

Sizing replicas without measuring. Too small, and they are throttled or run out of memory, often at startup in runtimes that load a lot up front, such as the JVM. Too large, and every replica carries idle capacity you pay for. Look at what a replica uses under normal load, give it some headroom, and let the replica count absorb the rest.

Scaling apps, databases and clusters on Cloider

Cloider apps scale both ways from one Compute configuration, in the app's Advanced settings, the CLI, the API or the MCP tools. You set the CPU and memory of each replica, up to 2 vCPU and 4096 MB, which is the vertical part, and how many replicas run, from 1 to 100, which is the horizontal part. Three options decide how:

  • Horizontal autoscaling, recommended for production: you size each replica, and Cloider adds replicas between your minimum and maximum when they average your CPU or memory threshold, and removes them after 5 quieter minutes.

  • Fixed resource management: a set number of replicas of a set size, for steady load.

  • Automatic resource management: each replica uses what it needs, up to 0.25 vCPU and 500 MB, and is billed for what it uses, and replicas are added at 80% of that CPU or 70% of that memory. It handles both directions for apps whose usage you don't know yet.

Scale out, with replicas of a set size, from the CLI:

Bash
cloider app update blog --compute autoscaling --cpu 0.5 --memory 512 --replicas 2 --max-replicas 6 --wait

Scale up later by changing only the size, every other value is kept:

Bash
cloider app update blog --cpu 1 --memory 1024

A compute change redeploys the running version without a rebuild, and the old replicas keep serving until the new ones are up, so even a vertical change causes no downtime. See Scaling and compute in the docs.

Cloider Kubernetes clusters have no nodes for you to add or resize, so there is no node pool or cluster autoscaler to configure: you scale pods, and Cloider runs the capacity under them. Size pods with their resource requests, the vertical part:

Bash
kubectl set resources deployment web -n shop --requests=cpu=500m,memory=512Mi --limits=memory=512Mi

Then scale them out with a HorizontalPodAutoscaler, which works out of the box because every cluster runs the metrics server:

YAML
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: web
  namespace: shop
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: web
  minReplicas: 2
  maxReplicas: 10
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 70

The 70% is measured against each pod's CPU request, so set requests first. Cloider bills each container for the larger of its request and its use, so requests close to real usage keep both the scaling and the bill accurate. Watch it work with kubectl get hpa web -n shop --watch.

Managed databases scale up through their plans, as shown above, from the dashboard's Resize tab, cloider db resize, the API or Terraform.

A rule of thumb

  • Is it a stateless web app, API or worker? Scale out, with at least two replicas and an autoscaler.

  • Is it a database or another stateful service? Scale up, and try caching, pooling and read replicas before sharding.

  • Does the software use only one core? Scale out with more processes or replicas, not bigger ones.

  • Do you need capacity today and can't change the code yet? Scale up as a stopgap, then make the app stateless.

  • Is the traffic spiky? Scale out with autoscaling, so you pay for the peak only while it lasts.

  • Is a single failure unacceptable? Scale out, whatever the load.

Use both, each where it fits

Vertical and horizontal scaling aren't rivals. The usual setup combines them, sometimes called diagonal scaling: each replica sized from what it really uses, as many replicas as the traffic needs, added and removed automatically, and a database sized up as the data grows, with Redis taking the hot reads.

On Cloider that comes down to a few settings: an app on Horizontal autoscaling from 2 replicas to as many as you need, a managed database you resize as it grows, and a Kubernetes cluster with HorizontalPodAutoscalers and no nodes to manage, all billed for what runs. Read more about Cloider Apps, or create a free account and try it with the welcome credit.

Frequently asked questions

What is the difference between horizontal and vertical scaling?

Vertical scaling gives one instance more CPU, memory or storage. Horizontal scaling runs more identical instances behind a load balancer. Vertical scaling needs no code changes but stops at the largest size and leaves a single point of failure. Horizontal scaling goes much further and survives losing an instance, but the app has to be stateless.

Which is better, horizontal or vertical scaling?

Neither in general. Stateless web apps, APIs and workers scale best horizontally, while databases and other stateful software usually scale vertically first. Most production systems use both: right-sized instances, as many of them as the traffic needs, and a database sized up as the data grows.

Is horizontal scaling more expensive than vertical scaling?

Per vCPU they cost the same on a pay-per-use platform. With autoscaling, horizontal scaling usually costs less for traffic that varies, because replicas are removed in quiet hours, while a vertically scaled instance is sized and billed for the peak all the time. Horizontal scaling does add operational complexity.

Does vertical scaling cause downtime?

For a single instance it usually does: most clouds stop a virtual machine to resize it, and a database restarts to apply its new size. Stateless containers can be resized with a rolling update instead, where new replicas start before the old ones stop, which is how Cloider apps change size without downtime.

Can databases scale horizontally?

Yes, with read replicas, sharding or a distributed database, at a cost in consistency, features and complexity. Relational databases usually scale up first, after tuning queries, caching hot reads and pooling connections. Cloider managed databases scale up by resizing to a larger plan.

What does an app need before it can scale horizontally?

It must be stateless: sessions in Redis, the database or signed cookies, uploads in object storage, locks and counters in a shared store, scheduled tasks run once rather than on every replica, connection pools sized for the replica count, health checks, graceful shutdown on SIGTERM and logs on standard output.

How do I scale an app on Cloider?

Set its compute configuration in the app's Advanced settings, with cloider app update, the API or the MCP tools. Each replica gets up to 2 vCPU and 4096 MB, and an app runs 1 to 100 replicas, either a fixed number or autoscaled between a minimum and a maximum on CPU or memory thresholds.

Written byMarei Morsy

Comments