Fly.io
Overview
Fly.io runs applications as Firecracker microVMs (Fly Machines) in the regions you choose (17 at the time of the last check; see fly platform regions), with Fly Proxy routing traffic to the nearest healthy machine. It supports scale-to-zero machines, persistent volumes, Tigris object storage, and private WireGuard networking (6PN) between apps. LiteFS (multi-region SQLite replication) still exists but Fly states it offers no support for it, so treat it as a community tool. The CLI is fly (alias flyctl); checked against flyctl v0.4.120.
Instructions
- When deploying applications, use
fly launchto auto-detect the framework, generate a Dockerfile if needed and writefly.toml, thenfly deploy. The default--strategyisrolling; the others arecanary,bluegreenandimmediate.--wait-timeoutdefaults to 5m and--ha(default true) creates a spare machine. - When configuring scaling, set
auto_stop_machines("off"default,"stop"or"suspend"; old booleantrueis legacy),auto_start_machines(boolean, default true) andmin_machines_running(default 0, primary region only) under[http_service]to scale to zero when idle and wake on incoming requests. Size machines with[[vm]](size,memory,cpus). - When managing multi-region deployments, use
fly scale count <n> --region a,b(n is the total across the listed regions, balanced between them; for a fixed count per region run one command per region),fly-replayheader to route writes to the primary region, and LiteFS (unsupported by Fly, and it must not be combined with autostop/autostart) for SQLite read replicas; managed Postgres is the supported route for relational data. - When handling persistent data, attach volumes for durable storage (machines are ephemeral), use LiteFS for multi-region SQLite, or Tigris for S3-compatible object storage.
- When connecting services, use
<app>.internal(or<region>.<app>.internal) DNS for private communication over 6PN. Services must listen on an IPv6-capable address (fly-local-6pnor::). 6PN bypasses Fly Proxy, so autostart does not apply and stopped machines are not returned; use Flycast for proxied private access. Do not give internal apps a public[http_service]. - When managing secrets, use
fly secrets set STRIPE_API_KEY=...for encrypted secrets exposed as environment variables. By default this triggers a deploy; add--stageto queue it for the nextfly deploy. - When troubleshooting, use
fly logsfor real-time streaming,fly ssh consoleto access running machines, andfly proxy 5433:5432 -a orders-dbto tunnel to internal services.
Examples
Example 1: Deploy a multi-region web application
User request: "Deploy my app globally with Fly.io in US, Europe, and Asia"
Actions:
- Initialize with
fly launchand configure Dockerfile - Add machines in three regions, one command per region so each gets its own count:
fly scale count 2 --region iad, thencdg, thennrt - Use managed Postgres (or LiteFS if you accept it is unsupported) for data
- Return a
fly-replay: region=iadresponse header on writes so Fly Proxy re-runs them in the primary region
fly launch --name storefront-web --region iad
fly scale count 2 --region iad
fly scale count 2 --region cdg
fly scale count 2 --region nrt
fly scale show
Output: fly scale show lists machines in iad, cdg and nrt; reads are served locally and writes are replayed to iad.
Example 2: Configure a cost-efficient staging environment
User request: "Set up a Fly.io staging environment that scales to zero when not in use"
Actions:
- Create a staging app with
fly launch - In
fly.tomlunder[http_service]setauto_stop_machines = "stop",auto_start_machines = true,min_machines_running = 0 - Attach a volume for persistent database storage
- Set health checks with appropriate timeouts for routing
[http_service]
internal_port = 8080
auto_stop_machines = "stop"
auto_start_machines = true
min_machines_running = 0
[[mounts]]
source = "data"
destination = "/data"
Output: A staging environment that stops idle machines and wakes on the next request.
Example 3: Canary deployment with auto-rollback
User request: "Deploy my app to Fly.io but test on one machine first. If the health check fails, roll back."
Actions:
- Define a health check (
[[http_service.checks]]withpath = "/api/health",grace_period,interval) fly deploy --strategy canaryboots one new machine, waits for its checks, then rolls the rest automatically; if checks fail the deploy aborts and the old machines keep serving- Fly has no
fly releases rollbackcommand. To go back to an earlier build, find its image withfly releases --imageand runfly deploy --image registry.fly.io/orders-api:deployment-01JABCDEF - A rollback reuses current
fly.tomland secrets and does not undo database migrations
# deploy-canary.sh: canary deploy; on failure, redeploy the last known-good image
set -euo pipefail
APP="${1:?usage: deploy-canary.sh orders-api registry.fly.io/orders-api:deployment-01JABCDEF}"
GOOD_IMAGE="${2:?find it with: fly releases --app $APP --image}"
if fly deploy --app "$APP" --strategy canary --wait-timeout 5m; then
echo "canary passed its health checks, all machines updated"
else
echo "deploy failed, redeploying $GOOD_IMAGE"
fly deploy --app "$APP" --image "$GOOD_IMAGE" --strategy immediate
exit 1
fi
Guidelines
- Use
auto_stop_machines = "stop"(or"suspend"for faster wake) for dev/staging to save costs. Autostart is on by default and only applies to requests through Fly Proxy. - Use
.internalDNS for service-to-service calls; never expose internal services publicly. - Store persistent data on volumes, not the machine filesystem, since machines are ephemeral.
- LiteFS is unsupported by Fly (pre-1.0, "use with caution") and does not work with autostop/autostart; prefer Postgres for production.
- Volumes are tied to one machine and region; a machine cannot move to another region with its data, so snapshot and restore instead.
- Set health checks with realistic timeouts; Fly Proxy uses them for routing, not just monitoring.
- Use
fly-replayheader for write operations in multi-region setups to route to the primary region.