-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
162 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
--- | ||
title: Autostop/autostart Machines | ||
layout: docs | ||
nav: apps | ||
redirect_from: | ||
- /docs/apps/autostart-stop/ | ||
- /docs/launch/autostart-stop/ | ||
--- | ||
|
||
For Fly Apps with a service configured, [Fly Proxy](/docs/reference/fly-proxy/) can automatically start and stop or suspend existing Machines based on incoming requests, so that your app can meet demand without keeping extra Machines running. Fly Machines are fast to start and stop, and you don't pay for their CPU and RAM when they're in a `stopped` or `suspended` state. | ||
|
||
Learn more about exactly how [Fly Proxy autostops and autostarts Machines](/docs/reference/fly-proxy-autostop-autostart/). | ||
|
||
## When to use autostop/autostart | ||
|
||
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. Autostart/autostop works well for apps with highly variable workloads, for smaller apps with low or sporadic traffic, and for most apps that aren't accepting requests continuously. If you need to, you can keep one or more Machines running in your primary region. | ||
|
||
## Configure autostop/autostart | ||
|
||
The autostop/autostart settings apply per service in an app's `fly.toml` file. See the [[[services]]](/docs/reference/configuration/#the-services-sections) or [[http_service]](/docs/reference/configuration/#the-http_service-section) docs for details about services. | ||
|
||
Autostop/autostart settings: | ||
|
||
* **`auto_stop_machines`:** The action, if any, that Fly Proxy should take when the app is idle for several minutes. Options are `"off"`, `"stop"`, 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). | ||
* **`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 | ||
... | ||
[[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 automatic starts and stops work](#how-it-works). | ||
|
||
## Recommendations for autostop/autostart configuration | ||
|
||
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"`. | ||
|
||
### 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. | ||
|
||
The autostop/autostart feature 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](/docs/apps/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`. | ||
|
||
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. | ||
|
||
### 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+external) option that exits after _n_ seconds of inactivity. | ||
* [A Tired Proxy in Go](https://github.com/superfly/tired-proxy+external) 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+external) & [demo](https://autoscale-to-zero-demo.fly.dev/+external). | ||
|
||
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 | ||
... | ||
[http_service] | ||
internal_port = 8080 | ||
force_https = true | ||
auto_stop_machines = "stop" | ||
auto_start_machines = true | ||
min_machines_running = 0 | ||
... | ||
``` | ||
|
||
### Apps with no autostop/autostart settings | ||
|
||
For apps that don't explicitly specify any autostop/autostart settings in `fly.toml`, the Fly Proxy will automatically start stopped Machines when needed, but won't automatically stop or suspend them. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.