Skip to main content

Scale to zero

Scale to zero stops your server while nobody is using your app and starts it again when the next request arrives. That makes it a good fit for production apps with quiet hours, and for staging projects, demos, internal tools, and apps before launch.

Which projects scale to zero​

Whether a project scales to zero depends on its plan:

PlanScale to zeroTo change it
StarterOnMove the project to Growth to turn it off
GrowthOff by defaultSet Min podlets to 0 to turn it on

To see whether your project scales to zero, open its Settings in the Serverpod Cloud console. The Compute card in the Plan section shows the range of podlets the project can run, such as 0-1 PODLETS. Podlets are the running copies of your server. If the range starts at 0, the project scales to zero.

From the terminal, run serverpod cloud project show in your project's directory. The Compute row ends with the same range, such as 0-1 podlets.

When to use scale to zero​

Scale to zero suits projects with quiet periods.

It doesn't suit:

  • Apps where every request must be fast: The first request after a quiet period waits a few seconds while the server starts.
  • Servers that use future calls: Future calls aren't supported with scale to zero. A call that's due while the project is idle runs late, once the next request wakes the server.

If one of these describes your project, turn off scale to zero.

You pay for the time your podlets run, so a project with zero podlets adds nothing to your podlet costs. The monthly plan fee still applies.

How scale to zero works​

About 5 minutes after the last request ends, the project scales down to zero podlets. The console then shows its status as Idling.

Only requests that reach your server wake it or keep it running. Here are common activities and whether each one does:

ActivityWakes it or keeps it running?
A request to any of your project's addresses, including your custom domainsYes
An uptime monitor that checks your project more often than every 5 minutesYes, all the time
A connection your app keeps open for streaming methodsYes, while it's open. If your app always keeps one open, the server keeps running while anyone has the app open.
The console's Sessions tab, which lists the calls to your serverYes, on a project with a database, while the tab is open with Auto-fetch on (the default)
Work your server does on its own, such as a timerNo
A file the CDN (content delivery network) serves from its cacheNo, unless the CDN checks with your server first
A deployNo

During a deploy, Serverpod Cloud runs the new version briefly to apply database migrations and check that it starts. An idling project then stays idle until the next request, which starts the new version. A new project's first deploy starts a podlet. If no requests arrive, the project scales down to zero about 5 minutes later.

While the project is idling, the data in your database and your stored files stay as they are. Anything your server keeps in memory, such as a cache, starts empty after a wake-up.

Handle the first request to an idling project​

The first request to an idling project wakes the server. A wake-up usually takes 4 to 6 seconds. Serverpod Cloud holds the request for up to 30 seconds, then passes it on to the server once it is ready.

In rare cases where a wake-up takes longer than 30 seconds, the request fails with a timeout error (HTTP 504), but the server keeps starting.

By default, your app waits 20 seconds for an endpoint call and 5 seconds for a new streaming connection. The 20 seconds covers a typical wake-up, but the 5 seconds can be too short. To make your app wait longer, pass connectionTimeout and streamingConnectionTimeout when you create your Client. Also make your app retry a call or a streaming connection that fails while the server wakes up.

Check whether your project is idling​

In the console, look under STATUS at the top of the project's page:

The top of a project's page in the Serverpod Cloud console, where the status bar above the tabs shows a moon icon and Idling under STATUS.

From the terminal, run this command in your project's directory:

serverpod cloud status live

From another directory, pass -p your-project-id.

The console status and the Status row of the command's output use these labels:

What's happeningConsoleCloud CLI
The server is runningOnline● Running
The server has stoppedIdling☾ Idling
The server is waking upStarting◐ Starting
The server is still waking up after 30 secondsUnavailable✖ Down

While a project is idling, its web and API links stay available. When you open one, the server wakes up.

info

Cloud CLI versions before 1.3.0 show an error for an idling project. To see which version you have, run serverpod cloud version. To update, run dart install serverpod_cloud_cli.

Configure scale to zero​

Scale to zero is on when Min podlets, the number of podlets your project always keeps running, is 0. You set it in the console, except on Starter, where it's always 0.

info

Only the project's owner can change Min podlets. For anyone else, Edit plan is dimmed, and hovering it shows who the owner is.

Turn on scale to zero​

  1. In the Serverpod Cloud console, open your project's Settings and select Edit plan.

  2. In the Compute section, under Autoscaling range, set Min podlets to 0.

    The Compute section of the console's plan panel, with the Autoscaling range below Podlet size, Min podlets set to 0, and "scales to zero when idle" below it.

  3. Select Save.

The change usually applies within a minute or two, and your server restarts once. The project then scales down to zero after about 5 minutes without requests.

Turn off scale to zero​

Starter projects always scale to zero, and the console shows Upgrade plan instead of Edit plan. To keep your server running, move the project to Growth, which keeps one podlet running by default.

On other plans, set Min podlets to 1 or more in the console:

  1. In the Serverpod Cloud console, open your project's Settings and select Edit plan.
  2. In the Compute section, under Autoscaling range, set Min podlets to 1 or more.
  3. Select Save.

The change usually applies within a minute or two. Your server then starts, or restarts once if it was already running.