~/writing

Zero-downtime deploys on one server

Start the new version, wait for it to be healthy, then swap. A small pattern that covers most single-box apps.

1 min readdeploys, linux

You don’t need a cluster to deploy without dropping requests. You need two copies of your app for about thirty seconds and a proxy that can switch between them.

The steps

  1. Start the new release on a spare port.
  2. Poll its health check until it answers 200, or give up after a minute.
  3. Point the proxy at the new port and reload it. Reloads don’t drop open connections.
  4. Wait for the old process to finish its requests, then stop it.
new_port=$(next_free_port)
start_release "$release" "$new_port"
wait_healthy "http://127.0.0.1:$new_port/health" 60 || { stop_release "$release"; exit 1; }
point_proxy_at "$new_port" && reload_proxy
drain_and_stop "$old_release"

The part people skip is the last one. Stopping the old process the moment the proxy switches cuts off whatever it was still answering.