Dev tools
Zero Downtime Deployment: Strategies That Work
How to do zero downtime deployment: rolling, blue green and canary releases, health checks, graceful shutdown, safe database migrations and Kubernetes.
A zero downtime deployment releases a new version of your application without users seeing errors or an outage. Requests keep being served the whole time, because the old version keeps running until the new one is healthy, and traffic moves across gradually or all at once without dropping connections. The main strategies are rolling updates, blue green deployment and canary releases. Strategy alone isn't enough, though. Zero downtime also depends on health checks, graceful shutdown and database changes that both old and new versions can work with.
This guide explains each strategy, the five requirements every one of them depends on, a working Kubernetes setup, how to run a zero downtime database migration with the expand and contract pattern, how to do it without Kubernetes, and the mistakes that cause "zero downtime" deploys to drop requests anyway.
Zero Downtime Deployment Strategies at a Glance
| Strategy | How it works | Extra capacity | Rollback speed | Risk exposure | Good for |
|---|---|---|---|---|---|
| Rolling update | Replace instances a few at a time | Small (a surge of one or a few instances) | Medium: roll back the same way | Old and new run side by side during the rollout | Most stateless web services; the Kubernetes default |
| Blue green | Run a full new environment, then switch all traffic | Double, briefly | Fast: switch back | All users switch at once | Big changes needing instant rollback |
| Canary | Send a small share of traffic to the new version, then increase | Small | Fast: send traffic back | Only a small share of users at first | Risky changes; services with good metrics |
| Feature flags | Deploy code dark, turn features on separately | None | Instant: flip the flag | Controlled per feature and per user | Decoupling releases from deploys |
They combine well. A common setup is rolling deploys for routine changes, feature flags for new features, and canaries for risky infrastructure changes. For the blue green approach in depth, see our guide to blue green deployment.
What Is Rolling Deployment?
What is rolling deployment? It's a strategy that replaces old instances with new ones in small batches. A batch of new instances starts, passes its health checks and joins the load balancer. Then a matching batch of old instances drains its connections and stops. The process repeats until every instance runs the new version. At every moment, enough healthy instances are serving traffic.
Rolling deployment is the default in Kubernetes and most container platforms, because it needs little spare capacity and no extra environment. Its downsides: both versions serve traffic at the same time for a while, and rolling back means rolling forward to the old version, which takes as long as the original rollout.
Canary Deployment vs Rolling Deployment
Canary deployment vs rolling deployment comes down to what decides how far the rollout goes. A rolling update moves forward as soon as new instances are healthy. A canary moves forward only when the new version's behaviour (error rate, latency, business metrics) looks as good as the old version's.
| Rolling | Canary | |
|---|---|---|
| Traffic to new version | Grows with the number of replaced instances | Set explicitly: 1%, 5%, 25%, 100% |
| Gate to continue | Readiness checks pass | Metrics compare well against the old version |
| Detects | Crashes, failed startup | Subtle bugs, regressions, performance problems |
| Needs | Health checks | Traffic splitting and good monitoring |
| Typical tools | Built-in Kubernetes Deployments, cloud instance groups | Argo Rollouts, Flagger, service meshes, cloud load balancer weighting |
A practical rule: if a bad version would fail its health check, a rolling update catches it. If it would start fine but behave badly, such as slower queries or wrong prices, you need a canary.
The Five Requirements for Zero Downtime
Whatever strategy you choose, it only delivers zero downtime if these five things are in place:
- Accurate readiness checks. The platform must know when a new instance can actually serve traffic: dependencies connected, caches warm, migrations finished. A readiness endpoint that returns "OK" the moment the process starts sends users to an instance that isn't ready.
- Graceful shutdown. When an old instance is told to stop, it should stop accepting new requests, finish in-flight ones and then exit. Most frameworks support this when they receive the termination signal, but it must be enabled and given enough time.
- Connection draining. The load balancer has to stop sending new requests to an instance before it shuts down. There's usually a short delay before every load balancer notices, which is why a small pre-stop pause helps.
- Backward-compatible changes. During a rollout, old and new versions run at the same time. Both must work with the same database schema, the same message formats and the same API contracts.
- Enough capacity. Taking instances out of service must not overload the rest. Add new ones before removing old ones.
Most dropped requests during "zero downtime" deploys come from missing items 2 or 3, not from the choice of strategy.
Zero Downtime Deployment Kubernetes Setup
For zero downtime deployment, Kubernetes gives you all the building blocks. A Deployment configured like this adds one new pod at a time and never drops below the desired count:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 4
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
terminationGracePeriodSeconds: 30
containers:
- name: web
image: registry.example.com/web:1.4.2
ports:
- containerPort: 8080
readinessProbe:
httpGet:
path: /healthz/ready
port: 8080
periodSeconds: 5
lifecycle:
preStop:
exec:
command: ["sleep", "10"]What each setting does:
- `maxSurge: 1` and `maxUnavailable: 0` start one extra pod before taking any old pod away. Kubernetes' defaults are 25% for both, which can briefly reduce capacity.
- `readinessProbe` keeps the new pod out of the Service until
/healthz/readysucceeds. - `preStop` with a short sleep gives the endpoints controller and load balancers time to stop sending traffic before the app receives its termination signal.
- `terminationGracePeriodSeconds: 30` (also the default) is the total time Kubernetes waits for the pod to exit after starting shutdown, including the pre-stop hook. Increase it if requests can take longer.
Add a PodDisruptionBudget so voluntary disruptions, such as node upgrades, can't take too many pods down at once:
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: web-pdb
spec:
minAvailable: 3
selector:
matchLabels:
app: webFor canaries, tools such as Argo Rollouts or Flagger replace the Deployment with a resource that shifts traffic in steps and checks metrics between them.
Zero Downtime Database Migration
Databases are where most zero downtime plans break, because a schema change can lock a table or break the version still running. The standard technique for a zero downtime database migration is expand and contract (also called parallel change):
- Expand. Add the new structure without removing the old: a new column, table or index. Old code ignores it.
- Write to both. Deploy code that writes to old and new structures.
- Backfill. Copy existing data into the new structure in small batches.
- Read from the new. Deploy code that reads from the new structure.
- Contract. Once no running version uses the old structure, remove it in a later release.
Example: renaming orders.state to orders.status without downtime takes three or four releases: add status, dual-write, backfill and switch reads, then drop state. That's slower than one ALTER TABLE ... RENAME, but nothing breaks while old and new code run side by side.
Database-specific tips for PostgreSQL:
SET lock_timeout = '5s';
ALTER TABLE orders ADD COLUMN status text;
CREATE INDEX CONCURRENTLY idx_orders_status ON orders (status);- `lock_timeout` makes a migration fail fast instead of queueing behind a long transaction and blocking every query behind it.
- `CREATE INDEX CONCURRENTLY` builds an index without blocking writes. It can't run inside a transaction block, so migration tools need to run it separately.
- Adding a nullable column is quick. Since PostgreSQL 11, adding a column with a constant default is also fast, because the default is stored as metadata instead of rewriting the table.
- Changing a column's type often rewrites the table; use expand and contract instead.
MySQL has online DDL options, and tools like gh-ost and pt-online-schema-change copy tables in the background for changes that would otherwise lock. For the trade-offs between the two databases, see Postgres vs MySQL.
Zero Downtime Without Kubernetes
You don't need Kubernetes for zero downtime deployment. The same principles work with simpler setups:
- Two or more servers behind a load balancer: take one server out of the pool, wait for connections to drain, deploy, check health, put it back, then repeat for the next. That's a manual rolling deployment.
- One server with Docker Compose: start the new container alongside the old one on a different port, switch the reverse proxy's upstream to it and reload the proxy (Nginx reloads gracefully without dropping existing connections), then stop the old container.
- Managed platforms: most app platforms and cloud services offer rolling or blue green deploys with health checks built in. Turn them on and set a real health check path.
The weak point in single-server setups is the server itself: a reboot or crash still means downtime. Zero downtime deploys and high availability are related but different goals.
Release Practices That Reduce Risk
- Small, frequent deploys. A small change is easier to verify and roll back than a month of work.
- Feature flags. Deploy code with new features switched off, then enable them gradually and independently of deployment.
- Automatic rollback. Tie rollbacks to error rate and latency, ideally against your service level objectives. Our guide to SLO vs SLA explains error budgets and burn rate alerts.
- Pre-production checks. Run migrations against a copy of production data size to catch slow, locking changes.
- Deploy when people are around. Deploys are safer when the team can watch metrics and respond.
Common Mistakes
- Readiness equals liveness. Using the same endpoint for both, or a trivial "process is up" check, sends traffic to pods that can't serve it yet.
- No graceful shutdown. The app exits immediately on termination and cuts off in-flight requests.
- Destructive migrations in the same release. Dropping or renaming a column while old code still uses it causes errors mid-rollout.
- Long-running migrations without lock timeouts. One
ALTER TABLEwaiting on a lock can block every query on that table. - Sticky sessions stored in memory. Users lose their session when their instance is replaced. Store sessions in a shared store or use stateless tokens.
- Breaking API or message changes. Clients and consumers still on the old version fail. Version your APIs and keep old fields until everyone has moved.
- Counting only HTTP 5xx. Watch latency, queue depth and business metrics too; a slow deploy can be an outage for users even with no errors.
FAQ
Is zero downtime deployment really zero?
It aims for no user-visible errors or unavailability. In practice, well-built systems drop no requests during routine deploys, but rare edge cases (long-lived connections, very long requests) need extra care.
Which zero downtime strategy should I start with?
Start with rolling updates and good health checks and graceful shutdown. That covers most stateless services. Add feature flags next, then canaries for risky changes once you have reliable metrics.
How do I handle long-lived connections like WebSockets?
Give instances a longer grace period, stop accepting new connections first, and have clients reconnect automatically. Many teams ask clients to reconnect gradually so the new version isn't flooded.
Do I need blue green deployment for zero downtime?
No. Rolling updates achieve zero downtime with less spare capacity. Blue green is valuable when you need an instant, complete rollback or want to test a full environment before switching.
What is the difference between zero downtime and high availability?
Zero downtime deployment is about releasing without interruption. High availability is about staying up despite failures, through redundancy across servers, zones and regions. You usually need both.