How to Keep Deploys From Breaking Your Site Mid-Traffic

The only deployment method that truly guarantees zero downtime is one where the new version is fully built and verified before any visitor is routed to it, and the switch between old and new is a single atomic operation. Everything else, including rolling restarts and symlink swaps, is a way of getting close to that guarantee, and how close you get depends entirely on what your host's stack actually lets you control.

Most "my deploy broke the site" incidents are not code bugs. They are routing bugs: a request arriving in the window when half the files are new and half are old, an opcode cache still holding compiled versions of files that no longer exist, or a health check passing on a process that has not finished warming up. Zero downtime deployment strategies are mostly about shrinking or eliminating that window, and then proving to yourself that it is gone.

What your host actually gives you

Before choosing a strategy, find out which primitives you have. The four that matter are an atomic rename or symlink swap on the document root, the ability to run more than one application process or release directory at once, control over the web server's reload behaviour, and a health endpoint you can poll. If you have all four, blue-green is on the table. If you have only the first, atomic symlink swaps are your realistic ceiling. If you have none, you are limited to maintenance windows, and you should say so honestly rather than pretending otherwise.

Ask your host concrete questions rather than "do you support zero downtime deploys". Ask whether the document root can be a symlink, whether mod_rewrite or the equivalent follows that symlink without a restart, whether PHP-FPM pools can be reloaded gracefully with USR2, and whether there is a shared filesystem between release directories. On shared hosting the answers are usually no, no, no, and yes, which tells you a great deal.

Atomic symlink swaps and the opcache trap

The classic pattern is a releases/ directory holding timestamped builds and a current symlink pointing at the live one. You build into a fresh directory, run your checks, then swap the link with a single rename() call, which is atomic on any sane filesystem. Requests already in flight continue reading through the old path they resolved; new requests resolve the new one.

ln -sfn /var/www/app/releases/20240115T101500 /var/www/app/current.tmp
mv -T /var/www/app/current.tmp /var/www/app/current
readlink /var/www/app/current
/var/www/app/releases/20240115T101500

The trap is the opcode cache. PHP's realpath cache and opcache both key on resolved paths, and a symlink swap changes what a path resolves to without changing the path string. If opcache.validate_timestamps is off, workers keep executing the old compiled files indefinitely, and you get a site that serves a mix of old and new code depending on which worker answers. Either call opcache_reset() as part of the swap, or set opcache.revalidate_path=1 and give the realpath cache a short TTL:

opcache.enable=1
opcache.validate_timestamps=1
opcache.revalidate_freq=2
opcache.revalidate_path=1
realpath_cache_ttl=120

Note that resetting opcache on every request is not the fix; resetting it once, after the swap, from a script that runs outside the request path, is.

Rolling restarts, blue-green and maintenance windows compared

A rolling restart replaces workers one at a time behind a load balancer, draining connections from each before terminating it. It works well when your application is stateless and the balancer honours connection draining. The failure mode is a worker that passes its port check but has not finished loading its configuration, so make the health endpoint check something real, such as a database round trip and a cache read, not just a TCP connect.

Blue-green runs two complete environments and flips traffic at the balancer or DNS layer. It is the cleanest option for schema changes, because you can run the new code against the old database and the old code against the new one during the overlap, provided your migrations are backward compatible. That constraint is the hard part. Additive migrations, new columns with defaults, and dual writes are fine; renaming or dropping a column in the same release that stops using it is not.

Maintenance windows are not a deployment strategy so much as an admission that you have none. If you must use one, make it explicit: return a 503 with a Retry-After header rather than a connection reset, so crawlers and clients back off politely instead of hammering you.

HTTP/1.1 503 Service Unavailable
Retry-After: 120
Cache-Control: no-store

You can serve that from a static file in the web root while the application is down, which is far better than a blank page or a stack trace.

Proving it works before you trust it

Once you have picked a strategy, test it under load rather than on a quiet afternoon. Run a load generator against the site, trigger a deploy, and watch for non-2xx responses and for responses whose body does not match either the old or the new build. A simple loop that records status codes and a content hash is enough:

while true; do
  curl -s -o /tmp/body -w "%{http_code} " https://example.com/
  sha256sum /tmp/body | cut -c1-12
  sleep 0.2
done

You want a stream of 200 codes and exactly two distinct hashes, with one clean transition between them. Any 500, any 502, any third hash, and your deploy is not zero downtime yet. Repeat the test with the deploy script as it will actually run, including the cache clear and the symlink swap, because that ordering is where most of the bugs live.

What to do next

Open a support ticket with your host and ask the four questions above in writing, then run the load test against a staging copy with a deliberately slow deploy. If the answers rule out anything better than a symlink swap, tighten that swap, add the opcache reset, and make the health check meaningful. If they rule out even that, document your maintenance window and serve a proper 503 during it, and start planning a move to a stack that gives you the primitives you need.

Related articles

Subscribe to our newsletter

Get the latest hosting tips, performance insights, and industry news.