Fly.io Scale-to-Zero Configuration
Overview
Section titled “Overview”Scale-to-zero is a Fly.io feature that automatically shuts down VMs after a period of inactivity to save costs in non-production environments. VMs automatically restart when new traffic arrives.
Activity is measured as: Traffic via the Fly proxy (flycast)
Key behaviors:
- VMs shut down after configured inactivity period
- VMs auto-restart when requests arrive
- First request after wake-up will be slower (cold start)
- Ongoing requests after wake-up should perform normally
Configuration
Section titled “Configuration”Scale-to-zero is configured differently for generic applications vs. Postgres databases.
Generic Applications
Section titled “Generic Applications”Add these settings to your fly.toml:
[http_service]auto_stop_machines = trueauto_start_machines = truemin_machines_running = 0What this means:
auto_stop_machines = true- Fly will automatically stop idle machinesauto_start_machines = true- Fly will automatically start stopped machines when traffic arrivesmin_machines_running = 0- Allow all machines to stop when idle
Postgres Applications
Section titled “Postgres Applications”Add these settings to your Postgres app fly.toml:
[env]FLY_SCALE_TO_ZERO = '1h'
[[services]]protocol = 'tcp'internal_port = 5432auto_stop_machines = trueauto_start_machines = trueWhat this means:
FLY_SCALE_TO_ZERO = '1h'- Shut down after 1 hour of inactivity- Service configuration includes the same auto-start/stop settings as generic apps
Common Issues
Section titled “Common Issues”Wake-from-Zero Reliability
Section titled “Wake-from-Zero Reliability”Scale-to-zero wake-up doesn’t always work reliably. If you experience issues:
- Check machine status:
fly status --all -a your-app-name - Manually restart if needed:
fly apps restart your-app-name
Inconsistent Shutdown Timing
Section titled “Inconsistent Shutdown Timing”Postgres instances may shut down faster than the configured FLY_SCALE_TO_ZERO timeout. This appears to be a Fly.io platform issue.
Disabling Scale-to-Zero
Section titled “Disabling Scale-to-Zero”To disable scale-to-zero for a specific machine:
fly machines update $machine_id --env FLY_SCALE_TO_ZERO=""When to Use Scale-to-Zero
Section titled “When to Use Scale-to-Zero”Good for:
- Staging environments
- Demo environments
- Development environments
- Any environment with intermittent usage
Not recommended for:
- Production environments
- Applications requiring consistent response times
- High-availability scenarios
Troubleshooting Commands
Section titled “Troubleshooting Commands”# Check app healthfly checks list -a your-app-name
# View all machines and their statusfly status --all -a your-app-name
# List machinesfly machines list -a your-app-name
# Manually start a specific machinefly machines start machine_id