# 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.

URL: https://levelrail.com/learn/deploy-operate/zero-downtime-deploys/
Updated: 2026-11-27

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](https://levelrail.com/learn/deploy-operate/what-is-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](https://levelrail.com/learn/glossary/#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](https://levelrail.com/learn/deploy-operate/blue-green-vs-rolling-deploys/)), 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](https://levelrail.com/learn/server-basics/add-swap-space-on-ubuntu/)).
- **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

| Symptom | Likely cause | Fix |
| --- | --- | --- |
| Brief `502` during each deploy | Old stopped before new was ready | Wait for the health check before stopping the old copy |
| New version never becomes healthy | Wrong health path, port or missing environment variable | Check `docker logs` and curl the health URL from the host |
| `Bind for 127.0.0.1:3000 failed: port is already allocated` | Both copies use the same host port | Give the new copy another port or use a network |
| Users logged out after deploy | Sessions stored in app memory | Move sessions to a shared store |
| Errors only on some requests after deploy | Migration not compatible with the old version | Make the change backward compatible in two releases |
| Deploy hangs for a long time on stop | App ignores SIGTERM | Handle the signal and shorten the stop timeout |
| Container shows `unhealthy` but the site works | Health check command wrong or too strict | Fix the check, test it by hand with `docker exec` |
| Old version keeps getting traffic | Proxy config not reloaded | Reload 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](https://levelrail.com/blog/restart-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
```

| Step | Failure means |
| --- | --- |
| New copy starts | Image or config problem |
| Health check passes | App not ready, remove it |
| Traffic switched | Proxy config problem |
| Old copy stopped | Grace 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](https://levelrail.com/learn/deploy-operate/rollbacks/))).

## Related lessons

- [What is a reverse proxy](https://levelrail.com/learn/deploy-operate/what-is-a-reverse-proxy/)): the switch behind the swap.
- [Docker healthchecks](https://levelrail.com/learn/docker/docker-healthchecks/)): how to write a check that means something.
- [Blue-green vs rolling deploys](https://levelrail.com/learn/deploy-operate/blue-green-vs-rolling-deploys/)): two ways to arrange the swap.
- [Update a container to a new image](https://levelrail.com/learn/docker/update-a-container-to-a-new-image/)): the simpler version with a short gap.

## Next

If the new release misbehaves after the switch, read [Rollbacks](https://levelrail.com/learn/deploy-operate/rollbacks/)).

## 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.
