Your tests pass and the new version is ready. How do you actually replace the old version running on the servers, while thousands of people are using it? Switch it off and on again, and everyone sees an error page. Switch everything at once, and if the new version has a bug, every user hits it. A deployment strategy is the plan for swapping versions safely: with no downtime, limited risk, and a quick way back if something goes wrong.
This lesson explains the four main strategies (recreate, rolling, blue-green and canary), plus feature flags, database changes and rollbacks, with when to use each.
Recreate: stop the old, start the new
The simplest approach: stop all old servers, then start the new version.
- Pros: very simple; old and new never run at the same time.
- Cons: downtime while the new version starts; every user hits any bug at once.
- Use for: internal tools, development environments, or apps where a few minutes offline at night is acceptable.
Rolling deployment
Replace servers a few at a time. With 10 servers: take one out of the load balancer, update it, check it's healthy, put it back, then move to the next.
Start: v1 v1 v1 v1 v1
Step 1: v2 v1 v1 v1 v1
Step 2: v2 v2 v1 v1 v1
...
Done: v2 v2 v2 v2 v2
- Pros: no downtime; no extra servers needed; it's the default in Kubernetes and many cloud platforms.
- Cons: for a while, both versions run together, so they must be compatible with each other and with the database; rolling back means rolling through all the servers again.
- Use for: most everyday deployments of stateless web apps.
Settings usually include how many servers to update at once, and how many can be unavailable during the update.
Blue-green deployment
Run two complete, identical environments. "Blue" is live. Deploy the new version to "green", test it there with real infrastructure, then switch all traffic from blue to green at the load balancer or DNS in one step.
Before: users ──> BLUE (v1) GREEN (v2, being tested)
After: users ──> GREEN (v2) BLUE (v1, kept on standby)
- Pros: no downtime; the new version is fully tested before users see it; rollback is instant: switch traffic back to blue.
- Cons: needs double the servers during the switch, which costs more; the database is usually shared, so schema changes still need care; long-running connections may be cut at the switch.
- Use for: releases where fast, reliable rollback matters a lot.
Canary deployment
Release the new version to a small slice of traffic first, say 5%, while 95% stays on the old version. Watch the canary's error rate and latency closely. If it looks healthy, increase to 25%, 50% and 100%. If not, send everyone back to the old version.
The name comes from the canaries miners once carried: if the bird got ill, the miners knew the air was dangerous before it hurt them.
- Pros: a bad release only affects a few users; it tests with real traffic and real data, which catches problems staging never shows; rollback is quick.
- Cons: needs good monitoring to tell whether the canary is healthy, and traffic-splitting support (weighted load balancing or a service mesh); slower to complete.
- Use for: large user bases and risky changes. Big tech companies release almost everything this way.
Canary analysis can be automated: if the canary's error rate rises clearly above the old version's, the pipeline rolls back on its own.
Comparing the strategies
| Strategy | Downtime | Extra cost | Rollback speed | Users exposed to a bug | Complexity |
|---|---|---|---|---|---|
| Recreate | Yes | None | Slow (redeploy) | All | Very low |
| Rolling | No | Little | Medium | Growing share | Low |
| Blue-green | No | Double during switch | Instant | All, after the switch | Medium |
| Canary | No | Little | Fast | A small share first | High |
Feature flags: separate deploy from release
A feature flag is a switch in the code that turns a feature on or off without deploying:
if flags.is_enabled("new_checkout", user_id=user.id):
return new_checkout(cart)
return old_checkout(cart)
This separates deploying code (putting it on servers) from releasing it (letting users see it). You can deploy unfinished work safely, switch a feature on for staff first, then 10% of users, then everyone, and switch it off instantly if it causes problems, with no redeploy. It's canary-style control at the level of individual features.
Remove old flags once a feature is fully released, or the code fills up with dead branches.
Database changes: the hard part
In rolling, blue-green and canary deployments, old and new code run at the same time against the same database. A change like renaming a column breaks the old code the moment it happens. The safe pattern is expand and contract, done over several deployments:
- Expand: add the new column, keeping the old one. Deploy code that writes to both.
- Migrate: copy existing data into the new column.
- Switch: deploy code that reads from the new column.
- Contract: once no running code uses the old column, remove it.
Every step is backwards-compatible, so you can roll back the code at any point. It's slower, but it's how teams change databases without downtime.
Rollbacks and roll-forwards
Every strategy needs a tested way back:
- Roll back: redeploy the previous version. Keep old builds (such as tagged Docker images) available, so this takes minutes.
- Roll forward: fix the bug and deploy again quickly. Fine for small issues when your pipeline is fast.
- Automate the trigger: health checks and error-rate alerts after a deploy should stop or reverse it automatically.
Practise rollbacks before you need them. A rollback process you've never tested is a guess.
Which should you choose?
- A student project or small app: rolling deploys from your CI/CD pipeline, or recreate if a moment of downtime is fine.
- A production web app: rolling by default, plus feature flags for risky features.
- When rollback must be instant: blue-green.
- Large scale or high risk: canary, with automated monitoring.
They also combine: many teams do rolling or canary deploys of code, with feature flags controlling what users see.
Practice
- You rename the column
phonetomobileand deploy with a rolling update. Users start seeing errors. Why?
Show the answer
During the rolling update, old servers still query phone, which no longer exists. Use expand and contract: add mobile, write to both, backfill, switch reads, and remove phone in a later release.
- A payments team wants to test a new version on real traffic but limit the damage if it's wrong. Which strategy?
Show the answer
Canary: send a small percentage of traffic (say 1 to 5%) to the new version, monitor error rates and latency, and increase gradually or roll back automatically.
- What's the main cost of blue-green deployment?
Show the answer
Running two full production environments, at least during the switch, roughly doubling infrastructure for that time. Shared databases also still need backwards-compatible changes.
Interview questions
Compare blue-green and canary deployments.
Blue-green runs two full environments and switches all traffic at once, giving instant rollback but exposing everyone after the switch and costing double capacity. Canary shifts a small share of traffic gradually while monitoring, limiting the impact of a bad release but needing traffic splitting and good observability.
What is a rolling deployment?
Updating instances in small batches, each taken out of rotation, updated, health-checked and returned, so the service stays up. Old and new versions run side by side during the rollout, so they must be compatible.
How do you handle database schema changes with zero-downtime deployments?
Make changes backwards-compatible using expand and contract: add new structures first, write to both, migrate data, switch reads, and remove the old structures only after no running version needs them.
What are feature flags and why use them?
Runtime switches that enable or disable features without redeploying. They decouple deployment from release, allow gradual rollouts and quick kill switches, and let unfinished code be merged safely.
What to learn next
- Revisit how changes get tested before deploying in run tests and linters automatically in CI.
- See how load balancers make these strategies possible in load balancers explained.