Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 37 ยท Scaling and Performance

Scaling Dynos Horizontally and Vertically

Learn how to scale a Heroku app with heroku ps:scale and ps:type, the difference between more dynos and bigger dynos, and how to size capacity with simple math.

When traffic grows, your app needs more capacity. Heroku makes the mechanics easy: you change a number or a size and the platform starts or stops dynos. The hard part is deciding which change helps. This lesson explains the two directions of scaling, the commands, and a simple way to estimate how many dynos you need. The CLI cannot run on this site, so commands are read-only; the calculators are runnable stand-ins.

Horizontal and vertical

  • Horizontal scaling means more dynos of the same size. Requests are spread over them by Heroku's router. It works well for web processes that keep no state in memory, which is how twelve-factor apps are written.
  • Vertical scaling means a bigger dyno type with more memory and CPU share. It helps when a single request needs lots of RAM, when you hit memory errors, or when one process is CPU-bound.
Terminal
heroku ps --app my-app
heroku ps:scale web=3 worker=1 --app my-app
heroku ps:type web=standard-2x --app my-app
heroku ps:scale web=0 --app my-app

ps lists running dynos (web.1, web.2, worker.1). ps:scale sets the count for each process type from your Procfile. ps:type changes the size of a process type. Scaling to zero stops that process type, which is also the way to switch off a worker you do not need.

Scaling is per process type, not per app. web and worker scale independently, and each can have its own size. One dyno runs one process type from your Procfile.

What a request needs

Each web dyno runs your server with a fixed number of workers (processes or threads). One worker handles one request at a time (unless your framework is asynchronous). If requests take 100 ms and a dyno has 4 workers, one dyno can serve about 40 requests per second in the best case. Real capacity is lower because requests wait on the database and on each other.

A rough capacity formula, from queueing theory (Little's law), is: busy workers = requests per second x average seconds per request. If that number is close to your total workers, requests start to queue.

Python Stand-in: how many dynos for a traffic level?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The result is 1, 2, 5 and 13 dynos. The target_utilization of 0.6 means we aim to keep workers busy only 60 percent of the time, so a traffic spike does not immediately cause queues.

Why more dynos can fail to help

Adding dynos moves load to the database. If every dyno opens 20 connections, ten dynos need 200 (connection budget). If the bottleneck is a slow query or an external API, more dynos just wait in parallel. Before you scale, ask:

  1. Is the problem memory (errors R14/R15) or time (router timeouts H12)? See router metrics.
  2. Is the database the bottleneck (high connection count, slow queries)?
  3. Are many requests slow because of one endpoint? Fix that endpoint, cache it, or move the work to a background job.

Moving slow work out of the request

A user does not need to wait for an email to be sent or an image to be resized. Put such work in a worker process type that reads jobs from a queue (a Redis-backed queue is common, see key-value store). The web dyno returns an answer in milliseconds, and the worker catches up at its own pace. You then scale web for request volume and worker for queue depth, independently.

flowchart LR
    U["Browser"] --> R["Router"]
    R --> W1["web.1"]
    R --> W2["web.2"]
    W1 -->|enqueue job| Q[("Queue")]
    W2 -->|enqueue job| Q
    Q --> K["worker.1"]

Web dynos answer quickly and push slow tasks to a queue that workers drain.

Python Stand-in: worker count from queue depth

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

You get 1, 4 and 34 workers. A queue that grows without bound means workers are too few or jobs too slow.

Zero downtime and restarts

When you change the count or the size, Heroku starts new dynos and stops old ones. Web dynos receive SIGTERM and have 30 seconds to finish requests before SIGKILL. Make your server handle SIGTERM gracefully (gunicorn, Puma and Node's server.close() do the right thing when configured). Dynos also restart about once every 24 hours, plus after each deploy or config change. Code must tolerate it.

Costs

Dynos are billed by the second for the time they run, up to a monthly cap per dyno type. Scaling up for a launch day and back down afterwards is normal. Dyno types and autoscaling compares types and shows how autoscaling does that automatically.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which command changes the NUMBER of web dynos?

    ps:scale sets the count of dynos for each process type; ps:type changes their size.
  2. 2. When is vertical scaling the better choice?

    Bigger dynos give more RAM and CPU per process; more dynos only add copies of the same limits.
  3. 3. Why can adding dynos make a database problem worse?

    Connection pools multiply across dynos and can exceed the plan's connection cap.
  4. 4. Where should slow tasks such as sending email run?

    Workers handle slow jobs so web dynos can answer quickly, and each type scales on its own.

Summary

  • ps:scale sets dyno counts per process type; ps:type sets sizes.
  • Scale out for request volume, up for memory or per-process CPU needs.
  • Estimate with busy workers = rps x seconds per request, and keep headroom.
  • Check the database and slow endpoints before adding dynos; move slow work to workers.

Next: Dyno Types, Autoscaling and Cost