Deployment strategies: rolling, blue-green and canary

Intermediate · 10 min

Replace a running version without downtime: recreate, rolling updates, blue-green switches and canaries, what each costs, and what each needs from your app.

Before thisBuild once, promote through environmentsContinuous delivery vs continuous deployment

ByteByteGo · 10 min

Deployment strategies: rolling, blue-green and canary

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by ByteByteGoReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Recreate stops the old version and starts the new one, which is simple but causes downtime and makes rollback a full redeploy.
  • A rolling update replaces instances a few at a time, so there is no downtime and no extra capacity, but old and new versions serve traffic side by side during the rollout.
  • Blue-green runs the new version on a full second environment and switches all traffic at once, which makes rollback an instant switch back, at the cost of double capacity.
  • A canary sends a small share of real traffic to the new version first, so a bad release hurts a few users while you compare its metrics with the old version.
  • Every strategy except recreate runs two versions at once, so the app must tolerate mixed versions, especially in database schemas and APIs.

The problem: swapping the engine in flight

You have a tested artifact, promoted and ready for production. Production is serving users right now. How do you replace the old version with the new one?

The naive answer is to stop the old version, start the new one, and accept a few seconds or minutes of errors. For a hobby project that’s fine. For a shop taking orders, every deploy becomes an outage, so the team deploys less often, which brings back the big risky batches from the first lesson.

The strategies below all answer the same question differently: how much capacity, risk and complexity will you trade for no downtime and a fast way back?

The demo setup

To compare them on real requests, I ran eight tiny Python HTTP servers on my machine, four answering v1 and four answering v2, and a 13-line Python proxy in front that picks a backend for each request by weight from a routes.json file it re-reads on every request. Changing that file is my “load balancer config change”. A hit.sh script sent requests with curl and counted the answers. The proxy picks at random, so the counts are approximate.

Recreate

Stop all v1, start all v2. Downtime equals start-up time. Rollback means doing it again in reverse. It is the right choice in only a few cases, such as when two versions truly can’t run together and a maintenance window is acceptable. I didn’t bother demonstrating the gap.

Rolling update

Replace instances a few at a time. With four instances, swap one, wait for it to pass its health check, swap the next. I changed one route at a time from a v1 port to a v2 port and sent 100 requests after each step:

-- step 1: {"9201": 1, "9102": 1, "9103": 1, "9104": 1}
  74 v1 200
  26 v2 200
-- step 2: {"9201": 1, "9202": 1, "9103": 1, "9104": 1}
  44 v1 200
  56 v2 200
-- step 3: {"9201": 1, "9202": 1, "9203": 1, "9104": 1}
  27 v1 200
  73 v2 200
-- step 4: {"9201": 1, "9202": 1, "9203": 1, "9204": 1}
 100 v2 200

No downtime, and no extra servers beyond perhaps one spare. This is Kubernetes’s default for a Deployment. The catch is visible in steps 1 to 3: both versions serve users at once, and one user can bounce between them. Rollback is another rolling update, back the other way, which takes as long as the rollout.

Rolling updates depend on two things from the app: a health check that tells the load balancer when a new instance is ready, and graceful shutdown so an old instance finishes in-flight requests before it exits. Health checks and graceful shutdown covers both.

Blue-green

Run the new version as a complete second environment, “green”, next to the live “blue”. Test green privately. Then switch the router so all traffic goes to green at once. Keep blue running for a while:

== blue-green
  50 v1 200
-- switched to green
  50 v2 200
-- switched back
  50 v1 200

The “switched back” step is the selling point: rollback is a config change that takes effect on the next request. The cost is a second full environment’s worth of capacity, at least during the switch. Two subtleties: both colours usually share one database, so the schema must work for both, and long-lived connections such as WebSockets don’t move on their own when you switch.

Canary

Named after the canary miners took underground as an early warning. Send a small share of real traffic to the new version, watch it, and widen if it looks healthy. At a 9:1 weight:

== canary 10%
 175 v1 200
  25 v2 200

That came out at 12.5%, close to the 10% asked for. Here both versions returned 200. The point of a canary is to compare the new version’s error rate and latency against the old version on the same live traffic, while only a few users are exposed. If it looks bad, set its weight to zero. Making that comparison and decision automatic is the next lesson.

Canaries can be selected by percentage, or by user group, such as staff first, then one region. A related technique, shadowing, copies live traffic to the new version and throws its responses away, which tests load and errors without anyone seeing the results.

The rule that applies to all of them

Every strategy except recreate runs two versions together. So every change must be compatible with the version before it:

  • APIs: v2 must still accept v1’s requests, and new fields must be optional.
  • Database schemas: a column v1 reads can’t be dropped in the same deploy. Use expand and contract over several releases, covered in zero-downtime deploys and schema changes.
  • Messages and caches: data written by v2 may be read by v1.

The same strategies apply to ML models, with extra twists for comparing model quality, in MLOps deployment strategies.

About the video

ByteByteGo’s “Top 5 Most-Used Deployment Strategies” (2023, ten minutes, animated) has chapters on rolling, blue-green and canary deployments and finishes with feature toggles. Its description is promotional and doesn’t list the five, so going by the chapter titles, the remaining two fall in the introduction or the last chapter. It is short, visual and from a reputable systems channel. I had no transcript, so this is from the title, description and chapters. English was judged from the same metadata. Viktor Farcic’s longer 2020 walkthrough on DevOps & AI Toolkit, which includes recreate and runs on Kubernetes, is under deeper.

Where this fits

A canary only helps if someone is watching. The next lesson automates the watching and the rollback: progressive delivery and automated rollback.

Check yourself

  1. 01Which strategy gives the fastest rollback, and why?

    Blue-green. The old version is still running on its own environment, so rolling back is a single switch of the router or load balancer, with no new deploy.

  2. 02During a rolling update, a user's first request hits v2 and their next hits v1. What could break?

    Anything that assumes a single version: a v2 page calling an API that v1 doesn't have, data written in a new format that v1 can't read, or a client cached against v2. Both versions must be compatible while the rollout runs.

  3. 03Why is a canary more informative than testing in staging?

    It runs on real production traffic, data and infrastructure, which staging can only approximate. Comparing the canary's error rate and latency with the old version shows the release's real effect while only a small share of users is exposed.

  4. 04What does blue-green cost that a rolling update does not?

    Capacity. You need a second full environment able to take all traffic, at least for the duration of the switch, which roughly doubles infrastructure during deploys.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark