Deployment strategies explained: rolling vs blue-green vs canary, and feature flags

Most outages start with a change. A good deployment strategy limits how many users a bad release can hurt and lets you undo it in seconds. Learn rolling, blue-green and canary deployments, why deploy is not the same as release, and how to change a database without downtime.

A railway junction seen from above. A blue train runs on the main track, labelled version 1. A green train waits on a second track, labelled version 2. A signal operator stands at a lever that can switch the points between the two tracks. A banner at the top says 'Ship small. Watch closely. Undo fast.'
Fig. 1 — A good deployment is like a railway junction: the new train is ready on its own track, and one lever moves traffic over, or back again.

Here’s the old way to ship software. Friday, 5 p.m.: stop the server, copy the new version, start it again, and hope. The site is down for ten minutes. The new version has a bug, so you do it all again in reverse. Twenty-five minutes of downtime, a lot of lost sales, and a ruined weekend.

Here’s the modern way. Tuesday, 2 p.m.: start the new version next to the old one and send it 5% of traffic. Watch the error rate. Two minutes later you spot a bug, so you move that 5% back. Nobody else noticed. Zero downtime.

Most outages are caused by a change: new code, new config, a new database migration. So how you ship matters as much as what you ship.

“A good deployment limits who a bad release can hurt, and lets you undo it in seconds.”

Every strategy below is trying to give you four things:

  • Zero downtime: users never see a “we’re updating” page.
  • A small blast radius: if it’s broken, only a few users notice.
  • Fast rollback: undo in seconds, not a redeploy.
  • Early warning: catch problems from real traffic before everyone gets them.

This is part of the Observability, security & operations thread. It leans on the previous article on observability: you can’t deploy safely if you can’t see what the new version is doing.

Three ways to roll out new code

Three panels. Rolling: ten servers are updated two at a time from version 1 in blue to version 2 in green, so old and new run side by side for a while. Blue-green: two full environments, blue on version 1 and green on version 2; a router sends all traffic to blue, then flips all of it to green in one step, and can flip back. Canary: most traffic goes to version 1, and a small slice, 5%, goes to version 2; arrows show the slice growing to 25%, 50% and 100% as long as the metrics stay healthy.
Rolling replaces servers a few at a time. Blue-green flips all traffic at once. Canary moves a small slice first, then grows it.

Rolling deployment

Replace servers a few at a time. With ten servers, update two, check they’re healthy, then the next two, and so on, until all ten run the new version.

This is the default in Kubernetes, and it needs no extra servers:

# Kubernetes · update 2 pods at a time, never drop below 8 running
strategy:
  type: RollingUpdate
  rollingUpdate:
    maxUnavailable: 2   # at most 2 pods down at once
    maxSurge: 2         # at most 2 extra pods during the update

If something goes wrong, kubectl rollout undo rolls back the same way. The downsides: for a while, old and new versions run side by side, so both must work with the same database and the same API. And a rollback is another rollout, so it takes minutes, not seconds.

Blue-green deployment

Run two full copies of production. Blue is live on the current version. You deploy the new version to green, test it properly while it gets no real traffic, then switch the router so 100% of traffic goes to green in one step.

If anything looks wrong, switch the router back to blue. That’s the big win: rollback is instant, because the old version is still running and warm.

The costs: you pay for double the servers during the switch, and both versions share the same database, so schema changes need care (more on that below). And because everyone moves at once, a bug that only shows up under real traffic hits all users, if only briefly.

Canary deployment

Named after the canaries miners took underground: if the bird got sick, the miners got out before the gas reached them.

Deploy the new version and send it a small slice of real traffic, say 5%. Compare its error rate and latency with the old version. If it’s healthy, grow the slice: 5% → 25% → 50% → 100%, pausing at each step. If it gets worse at any step, send everything back to the old version.

The best setups do this automatically: a tool such as Argo Rollouts, Flagger or Spinnaker watches the metrics and stops or rolls back the release by itself. Many companies start even smaller, with their own staff, then 1% of users, then one country, then everyone.

Which one should you use?

Strategy Rollback speed Extra cost Who’s hurt by a bad release Complexity
All at once (stop, deploy, start) Slow: redeploy None Everyone, with downtime Lowest
Rolling Minutes Little A growing share during the rollout Low
Blue-green Instant About double while switching Everyone, until you flip back Medium
Canary Fast A little Only the canary slice, e.g. 5% Higher: needs good metrics

A sensible default: rolling for low-risk services, canary for anything important and user-facing. Blue-green when you need instant, all-or-nothing rollback, for example for a big infrastructure change.

Feature flags: deploy is not release

All three strategies move code. But often what you really want to control is a feature.

A feature flag is an if around new code that you can switch on or off at runtime, without a deploy:

// Java · new checkout ships "off", turned on from a dashboard
if (flags.isEnabled("new-checkout", user)) {
    return newCheckout.process(cart);
}
return oldCheckout.process(cart);

This separates two things people usually mix up:

  • Deploy: the new code is on the servers.
  • Release: users can actually use the feature.

With flags, you can deploy on Tuesday with the feature off, turn it on for staff on Wednesday, for 5% of users on Thursday, and for everyone next week. If it breaks, you turn the flag off in seconds, with no rollback and no redeploy. Tools like LaunchDarkly, Unleash or a simple flags table in your database all work.

One warning: flags are temporary. Once a feature is fully on, delete the flag and the old code path. Hundreds of forgotten flags make the code hard to read and test.

Changing the database without breaking anything

Code can be swapped in seconds. A database can’t. And during a rolling, blue-green or canary deploy, old and new code run at the same time against the same database. So every schema change must work with both versions.

Say you want to rename customer_name to full_name. If you just rename the column, every old server still running breaks the moment the migration runs. And you can’t roll back the code either, because old code expects the old column.

The safe way is called expand and contract. It takes several small deploys instead of one big one:

Four steps from left to right. Step 1, expand: add the new column full_name next to customer_name; old code still works. Step 2, write both: deploy code that writes to both columns and copy old rows across in small batches. Step 3, switch reads: deploy code that reads from full_name only; you can still roll back because customer_name is still being kept up to date. Step 4, contract: once nothing uses customer_name, drop it. A note says every step works with both the old and the new code.
Expand and contract: add, copy, switch, then remove. Each step is safe to roll back.
  1. Expand: add the new column. Nothing uses it yet, so nothing breaks.
  2. Write both: deploy code that writes to both columns. Copy existing rows across in small batches.
  3. Switch reads: deploy code that reads the new column. The old one is still kept up to date, so you can still roll back.
  4. Contract: when no code uses the old column, drop it.

Two more habits help:

  • Watch out for locks. On a big table, some ALTER TABLE commands lock the table while they run, which blocks every query. Check what your database does before running a change on millions of rows, and use online tools where needed (for example gh-ost or pt-online-schema-change for MySQL).
  • Backfill in small batches, each in its own transaction, so you don’t lock a huge number of rows at once.

What breaks, and how to fix it

What breaks What you see The fix
Rollback fails You roll the code back, but the old code can’t read the new schema Expand and contract: every schema change must work with the old and new code
Deploy at peak time A rolling update takes 20% of servers out at the busiest hour, and the rest are overloaded Deploy in quieter hours, or add extra servers first (maxSurge) so capacity never drops
Mixed versions confuse each other New code writes cache entries or messages the old code can’t read Keep formats backward compatible, or put a version in cache keys and message schemas
Nobody’s watching A bad canary is promoted because no one checked the graphs Automated checks on error rate and p99 latency that stop the rollout on their own
Forgotten feature flags Code full of dead branches that nobody dares delete Give every flag an owner and an expiry date

Puzzle 1: the rollback that didn’t fix it

The servers rolled back, but the users’ browsers didn’t. They still have version 2’s JavaScript and CSS cached, and maybe a service worker serving it too. So they keep running the broken front-end against the old back-end.

The fix: give every static file a unique name per build, such as app.3f9a2c.js (most build tools do this), and serve the HTML page with a short cache time so browsers always pick up the latest file list. Then rolling back the HTML rolls back everything it points to.

Puzzle 2: the canary that looked perfect

The canary was too small to show the problem. A few things can hide at 5%:

  • Shared limits: the new version opens 50 database connections per server instead of 10. One canary server’s 50 is fine. All 20 servers × 50 = 1,000 connections, and the database’s limit is 500.
  • Warm vs cold: the canary ran with caches already warmed by the old version. At 100%, many caches were cold at once.
  • Unusual traffic: the canary didn’t receive the heaviest kind of request, such as bulk exports.

The fix: grow in several steps (5% → 25% → 50% → 100%) with a pause at each one, and watch shared resources too: database connections, queue depth, downstream error rates, not just the new version’s own numbers.

Key takeaways

  1. Most outages come from changes, so how you deploy is part of your design.
  2. Rolling is simple and cheap; blue-green gives instant rollback at double cost; canary limits the damage to a small slice.
  3. Automate canary checks on error rate and latency, and roll back without waiting for a human.
  4. Feature flags separate deploy from release: ship code switched off, then turn it on gradually.
  5. Change databases with expand and contract, so old and new code both work at every step.
  6. Every deploy needs a tested rollback plan.

Next in the Observability, security & operations thread: security in system design, including authentication vs authorization, JWT and OAuth, and the common attacks every design must defend against.

Comments

LEAVE A COMMENT
No sign-up. Comments appear after a quick review.