Chapter 4 · Lesson 2 of 18
Deploy and operate

Zero-downtime deploys explained (health checks and traffic switch)

A zero-downtime deploy starts the new version next to the old one and moves traffic over only after the new one reports healthy.

By the end you will

Describe how a deploy swaps versions without dropping requests, and what can still cause downtime.

Time
18 min
Level
Beginner
Check
4 questions

A zero-downtime rolling deploy

In a rolling deploy the new version starts next to the old one, traffic moves only after the health check passes, and the old version is stopped last.
  1. The old version keeps serving every request.
  2. The new version starts next to the old one, with no traffic yet.
  3. A health check asks the new version whether it can serve requests.
  4. Once the check passes, the proxy knows the new version is ready.
  5. The proxy moves traffic to the new version.
  6. Only then is the old version stopped, so nobody saw downtime.
Static diagram
  1. ProxytrafficOld version
  2. ProxystartNew version
  3. New versionready?Health check
  4. Health checkhealthyProxy
  5. Proxyswitch trafficNew version
  6. Old versionstoppedProxy

A zero-downtime deploy replaces the running version of your app without dropping requests: you start the new version beside the old one, wait until it reports healthy, move traffic to it, and only then stop the old one. If the new version never gets healthy, users never see it.

How it works#

The idea relies on two things: a second copy of the app, and a reverse proxy that decides which copy gets traffic. The proxy is the stable front door. The apps behind it can change.

A worked example with Docker. Version 1.0 runs as app-blue on port 3001 and the proxy sends everything to it. You deploy 1.1:

  1. Start app-green from the 1.1 image on port 3002. The proxy still sends all traffic to app-blue.
  2. Poll http://127.0.0.1:3002/health every second or two. Until it returns success, nothing changes for users.
  3. When the check passes, update the proxy so new requests go to port 3002.
  4. Give app-blue a grace period. Requests already in progress finish. New ones no longer arrive.
  5. Send app-blue a stop signal (SIGTERM). A well-behaved app finishes work and exits. Remove the container.

A health check should test what matters: the process is up, it can reach its database, and it has finished starting. A check that returns 200 the moment the web server binds to its port says very little.

This pattern is a rolling or blue-green deploy, depending on how many copies you swap at once. Full details of tools differ, but the shape is the same.

If the health check never passes#

The deploy stops after its timeout, the new version is removed and the old one keeps serving. Users never see the broken release. This is the main safety gain over stopping the old container first. It only holds if the deploy really does wait for the check and your checks are honest.

What can still cause downtime#

  • A health check that is too weak, such as one that returns success before the app has connected to its database.
  • A migration that breaks the old version, since both versions run at once for a while.
  • Apps that need exclusive use of a resource, such as a fixed host port or a file lock.
  • Stopping the old version without a drain period or signal handling, which cuts live requests.
  • Long-lived connections, such as websockets, which end when the old version stops and must reconnect.

Check it worked#

Run a loop that requests your site while you deploy, and watch for anything other than success:

bash
while true; do curl -s -o /dev/null -w "%{http_code}\n" https://example.com/health; sleep 0.5; done

During a clean deploy you should see only 200. A few 502 or 000 lines in the middle mean there was a gap: the old version stopped before the new one was serving, or the proxy switched too early. Stop the loop with Ctrl+C.

Edge cases and gotchas#

  • Two copies cannot share a host port. Both cannot bind 127.0.0.1:3000. Give each copy its own port, or put them on a Docker network and let the proxy use service names.
  • Migrations run in the middle. If the new version needs a column the old one does not know, add the column first and ship code that uses it later. Dropping a column the old version reads causes errors during overlap.
  • Single node memory. Two copies at once need double the memory briefly. On a small VPS this can trigger the out-of-memory killer. Check with free -h and consider adding swap.
  • Sessions held in memory are lost. Users logged in on the old copy may be logged out when traffic moves. Store sessions in a shared store or the database.
  • Background workers run twice. During overlap, two copies may both run a scheduled job or consume the same queue. Make jobs safe to repeat, or stop old workers first.
  • SIGTERM is ignored. Some runtimes ignore it, so the stop waits its full timeout and then kills the app. Handle the signal in the app and finish requests.
  • Docker health checks and the proxy are separate. A container marked healthy by Docker does not move traffic by itself. Something must tell the proxy.

Troubleshooting#

SymptomLikely causeFix
Brief 502 during each deployOld stopped before new was readyWait for the health check before stopping the old copy
New version never becomes healthyWrong health path, port or missing environment variableCheck docker logs and curl the health URL from the host
Bind for 127.0.0.1:3000 failed: port is already allocatedBoth copies use the same host portGive the new copy another port or use a network
Users logged out after deploySessions stored in app memoryMove sessions to a shared store
Errors only on some requests after deployMigration not compatible with the old versionMake the change backward compatible in two releases
Deploy hangs for a long time on stopApp ignores SIGTERMHandle the signal and shorten the stop timeout
Container shows unhealthy but the site worksHealth check command wrong or too strictFix the check, test it by hand with docker exec
Old version keeps getting trafficProxy config not reloadedReload the proxy and verify with curl

Real-world scenario#

You run a small shop on one VPS. Releases used to mean docker compose up -d, which meant ten seconds of 502 errors, and one of them landed during a flash sale.

You change the process. The Compose file now names the app service, and the deploy script does this: pulls the new image, starts a second copy on port 3002, loops over curl -fsS http://127.0.0.1:3002/health for up to 60 seconds, and only then rewrites the proxy upstream and reloads it. It waits 15 seconds, stops the old copy with docker stop -t 30 and removes it.

The first run fails the check: the app needs a secret that was missing from the new container’s environment. The script prints the log, removes the new copy and leaves the old one serving. Nobody noticed the failed deploy except you. You fix the variable and redeploy. During the second run, a curl loop shows only 200 responses. See the blog guide on restarting a Docker app without downtime for a scripted version.

Quick reference#

bash
docker run -d --name app-green -p 127.0.0.1:3002:3000 myapp:1.1.0   # start new copy
curl -fsS http://127.0.0.1:3002/health                                # wait for success
# switch the proxy upstream to 3002, reload it
docker stop -t 30 app-blue && docker rm app-blue                      # drain and remove old
StepFailure means
New copy startsImage or config problem
Health check passesApp not ready, remove it
Traffic switchedProxy config problem
Old copy stoppedGrace period too short if errors appear

Security and safety notes#

  • Test the deploy path on a staging copy before relying on it.
  • Keep the old image until the new release has run cleanly, so you can go back (see rollbacks).

Next#

If the new release misbehaves after the switch, read Rollbacks.

Check what you learned4 questions
1When does the proxy start sending traffic to the new version?
2The new version's health check never passes. What happens to users?
3Which is a way a zero-downtime deploy can still cause downtime?
4The old container is stopped the instant traffic switches, and some users get dropped connections. What was missing?

Frequently asked questions

What is a health check?

A small request, often to a path like /health, that returns success only when the app can serve traffic. The deploy waits for it before switching.

What about database changes during a zero-downtime deploy?

The old and new versions run side by side briefly, so schema changes should work for both. Add columns first, remove them in a later release.

Why do I still get a few errors during a deploy?

Usually the old version was stopped while it still had requests in flight, or the new one was added before it was ready. Wait for a real health check and give the old version time to finish.

Can I do zero downtime with a single container and docker compose up -d?

Not reliably. Compose stops the old container and starts a new one, so there is a gap. You need a second copy running behind a proxy, or a tool that does the swap.

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios