> ## Documentation Index
> Fetch the complete documentation index at: https://docs.fly.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Autostop/autostart Machines

Your app can meet peak demand without keeping extra Machines running. Use autostop/autostart to automatically start and stop or suspend existing Machines based on incoming requests. Fly Machines are fast to start and stop, and you [don't pay for their CPU and RAM](/about/pricing#stopped-fly-machines) when they're in a `stopped` or `suspended` state.

Get all the details of [how Fly Proxy autostop/autostart works](/reference/fly-proxy-autostop-autostart).

<h2 id="when-to-use-autostop-autostart">
  When to use autostop/autostart
</h2>

Autostop/autostart works well for apps with highly variable workloads, for smaller apps with low or sporadic traffic, and for most apps that aren't receiving requests continuously. You can reduce resource usage and costs by using autostop/autostart to manage your Fly Machines as demand decreases and increases. You'll never have to run excess Machines to handle peak load; you'll only run, and get charged for, the number of Machines that you need. You can choose to keep one or more Machines running in your primary region.

Autostop/autostart isn't a fit for every workload. The Fly Proxy's stop loop runs every few minutes and stops at most one Machine per region per pass. That's fine for normal apps, but if you're running thousands of Machines in a single app (for example, [per-user dev environments](/blueprints/per-user-dev-environments)), the loop can't keep up. In that case, use [one app per user](/machines/guides-examples/one-app-per-user-why) with [dynamic routing](/networking/dynamic-request-routing), or have your app shut itself down when idle.

<h2 id="configure-autostop-autostart">
  Configure autostop/autostart
</h2>

The autostop/autostart settings are part of each service in an app's `fly.toml` file. See the [\[\[services\]\]](/reference/configuration#the-services-sections) or [\[http\_service\]](/reference/configuration#the-http_service-section) docs for details about service configuration. You can also add services to [private apps](#private-apps).

Autostop/autostart settings:

* **`auto_stop_machines`:** The action, if any, that Fly Proxy should take when the app is idle for several minutes.
  * The options are `"off"` (equivalent to `false`), `"stop"` (equivalent to `true`), or `"suspend"`.
  * If `"off"`, Fly Proxy will never stop Machines. If `"stop"`, Fly Proxy stops Machines when the app has excess capacity. If `"suspend"`, Fly Proxy suspends Machines (if possible) when the app has excess capacity. Starting a Machine from a `suspended` state is faster than starting a Machine from a `stopped` state, but there are [some caveats](https://community.fly.io/t/autosuspend-is-here-machine-suspension-is-enabled-everywhere/20942) that you should know about.
* **`auto_start_machines`:** Whether Fly Proxy should automatically start Machines based on requests and capacity.
* **`min_machines_running`:** The minimum number of Machines to keep running in the primary region when `auto_stop_machines` is set to `"stop"` or `"suspend"`.

Example configuration:

```toml theme={null}
...
[[services]]
  internal_port = 8080
  protocol = "tcp"
  auto_stop_machines = "stop"
  auto_start_machines = true
  min_machines_running = 1
...
```

In the preceding example, Fly Proxy will automatically stop Machines when the app has excess capacity and start them again when needed based on traffic to the app. The app will eventually scale down to one running Machine in the primary region if there's no traffic.

Concurrency limits configured for services in the `fly.toml` file also affect [how autostop/autostart works](/reference/fly-proxy-autostop-autostart): Fly Proxy uses the concurrency [`soft_limit` setting](/reference/configuration#http_service-concurrency) for each Machine to determine if there's excess capacity per region.

<h2 id="autostop-autostart-configuration-recommendations">
  Autostop/autostart configuration recommendations
</h2>

In general, unless your app shuts itself down when idle, we recommend setting `auto_stop_machines` and `auto_start_machines` so that they are both enabled or both disabled, to avoid having Machines that either never start or never stop. For example, if `auto_start_machines = false` and `auto_stop_machines = "stop"`, then Fly Proxy automatically stops your Machines when there's low traffic but doesn't start them again. When all or most of your Machines stop, requests to your app could start failing.

### Minimum number of Machines running

To keep one or more Machines running all the time in your primary region, set `min_machines_running` to `1` or higher. If `min_machines_running = 1` and there's no traffic to your app, then Fly Proxy will stop Machines until eventually there is only one Machine running in your primary region. `min_machines_running` has no effect unless you set `auto_stop_machines` to `"stop"` or `"suspend"`.

<Note>
  **`min_machines_running`:** This setting only maintains the specified minimum number of running Machines in your app's primary region. It has no effect on other regions where your app is deployed. If you need to maintain minimum running Machines in multiple regions, you'll need to manage those separately through other means like [`fly scale count`](/flyctl/cmd/fly_scale_count#main-content-start).
</Note>

### Maximum number of Machines running

There's no "maximum machines running" setting, because the maximum number of Machines is the total number of Machines in your app.

Fly Proxy autostop/autostart never creates or destroys Machines for you. The maximum number of running Machines is the number of Machines created for your app on launch, or using `fly scale count` or `fly machine clone`. For example, if you want to have a maximum of 10 Machines available to service requests, then you need to create 10 Machines for your app. Learn more about [scaling the number of Machines](/launch/scale-count).

### Keep all Machines running continuously

If you need all your app's Machines to run continuously, then you can set `auto_stop_machines` to `"off"` and `auto_start_machines` to `false` to turn off autostop/autostart completely.

If you only need a certain number of your app's Machines to run continuously, then you can set `auto_stop_machines` to `"suspend"` or `"stop"` and `min_machines_running` to `1` or higher. Note that `min_machines_running` only applies to your app's primary region.

### Private apps

Requests to apps without services configured on your private network don't get routed through Fly Proxy and so Machines can't be automatically stopped or started by Fly Proxy. To use Fly Proxy autostart/autostart for private apps, set up services and use a Flycast private IPv6 address. See [Flycast - Private Fly Proxy Services](/networking/flycast) and [Autostop/autostart private apps](/blueprints/autostart-internal-apps).

### Apps that shut down when idle

Setting your app to automatically stop or suspend Machines when there's excess capacity using `auto_stop_machines` can be a substitute for when your app doesn't shut itself down automatically after a period of inactivity. If your app already shuts down when idle, then you can set `auto_start_machines = true` and `auto_stop_machines = "off"` to have Fly Proxy automatically restart the Machines stopped by your app.

If you want a custom shutdown process for your app, then you can code your app to exit when idle.

Here are some examples:

* [Shutting Down a Phoenix App When Idle](https://fly.io/phoenix-files/shut-down-idle-phoenix-app/): a post by Chris McCord on adding a task to an Elixir app's supervision tree that shuts down the Erlang runtime when there are no active connections.
* For Rails apps, the `dockerfile-rails` generator provides a [--max-idle](https://github.com/rubys/dockerfile-rails#addremove-a-feature) option that exits after *n* seconds of inactivity.
* [A Tired Proxy in Go](https://github.com/superfly/tired-proxy) used in [Building an In-Browser IDE the Hard Way](https://fly.io/blog/remote-ide-machines/). [There's a community fork with more recent updates](https://community.fly.io/t/improved-tired-proxy-for-use-with-fly-machines/10584).
* A minimal demo app in TypeScript/Remix: [code](https://github.com/fly-apps/autoscale-to-zero-demo) & [demo](https://autoscale-to-zero-demo.fly.dev/).

Fly Postgres also [supports scaling to zero](https://community.fly.io/t/scale-to-zero-postgres-for-hobby-projects/12212).

## Default settings

The defaults differ for new apps created with `fly launch` and for apps with no settings configured for autostop/autostart.

### Apps created with `fly launch`

When you create a new app using the `fly launch` command, the default settings in `fly.toml` are:

```toml theme={null}
...
[http_service]
  internal_port = 8080
  force_https = true
  auto_stop_machines = "stop"
  auto_start_machines = true
  min_machines_running = 0
...
```

<h3 id="apps-with-no-autostop-autostart-settings">
  Apps with no autostop/autostart settings
</h3>

For apps that don't explicitly specify any autostop/autostart settings in `fly.toml`, Fly Proxy will automatically start stopped Machines when needed, but won't automatically stop or suspend them.

## Related topics

* [How Fly Proxy autostop/autostart works](/reference/fly-proxy-autostop-autostart)
* [Autostop/autostart private apps](/blueprints/autostart-internal-apps)
* [Autoscale based on metrics](/launch/autoscale-by-metric)
