Course contents · Lesson 8 of 16

Deployment Strategies: Rolling, Blue-Green, Canary

Continuous Delivery · Lesson 8 of 16 · 11 min Engineer

Version 1.5.0 of your shop's app has passed every check, and customers are on the site. You cannot close the shop while you swap the code, and if 1.5.0 hides a bug, you want a few people to meet it, not everyone. Deployment strategies are the plans for that swap: which copies change when, who sees the new version first, and how you go back. This lesson compares four and shows how to choose.

Recreate: stop everything, then start

To deploy is to put a new version of your app onto the machines that run it, usually as several instances, identical running copies such as containers, behind a load balancer that spreads requests across them (lesson 3). A deployment strategy is the order in which you replace old instances and move traffic to new ones.

The simplest is recreate: stop every old instance, then start the new ones. Only one version ever runs, and you need no spare machines. The price is downtime, a gap when nothing answers until the first new instance is ready, and a rollback costs another gap.

Recreate suits test environments, internal tools with a maintenance window, and apps whose two versions must never run at once. In Kubernetes, a Deployment's strategy.type picks Recreate or the default, RollingUpdate.

Rolling update: a few at a time

The other three start new instances before the old ones are all gone. In the preview, notice where v1, the running version, and v2, the new one, serve users together: for a while in a rolling update, almost never in blue-green, and on purpose in a canary.

Three panels. Rolling update: four copies shown at four moments in time turn from v1 to v2 one at a time, with a warning that both versions serve users until the last copy flips. Blue-green: a load balancer sends 100 percent of traffic to the green stack of v2 copies and 0 percent to the blue stack of v1 copies, which stands by so you can flip back in seconds. Canary: the load balancer sends 95 percent of traffic to the stable v1 copies and 5 percent to one v2 canary, whose numbers you watch before sending it more. The split is illustrative
All three start v2 before v1 is gone, but move users across differently.

A rolling update replaces instances in small batches. The platform starts a v2 instance and waits until it passes its health check, a request asking "are you ready to serve?". Only then does it add the instance to the load balancer and stop a v1 instance, repeating until no v1 is left.

The catch: both versions serve users at once, so they must work side by side. v2 must handle data shaped by v1, and v1 must survive whatever v2 writes. If v2 renames a database column, every v1 instance still running fails. Rolling back is another rollout, batch by batch, so it is not quick.

This excerpt of a Kubernetes Deployment, which rolls by default, replaces 4 pods, Kubernetes's wrapper around one or more containers, one at a time:

spec:
  replicas: 4
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1          # at most 1 pod above the 4 during the update
      maxUnavailable: 0    # never fewer than 4 available pods

Kubernetes adds one v2 pod, waits until it is ready, then removes one v1 pod. Without a readiness probe, its health check, a broken pod counts as ready once it starts. Try it hands-on in Kubernetes Rolling Updates and Rollbacks.

Blue-green: a full new shift, one switch

In a blue-green deployment, blue is the full copy of production that users reach now. You deploy v2 as green, an identical copy beside it, and test it while no user can reach it, for example with smoke tests, a few quick checks of the main pages. Then you point the load balancer at green, and all traffic moves at once.

Flowchart of six cards, each showing a load balancer above a blue stack running v1 and a green stack running v2. Step 1, deploy green: v2 beside v1, no users yet. Step 2, test green: smoke tests on a private address while users stay on blue. Step 3, flip the switch: all new requests go to green. Step 4, watch green's error rate and latency. Step 5, keep blue ready: a dashed arrow shows you can flip back if numbers worsen. Step 6, retire blue after the watch period. A note says both stacks usually share one database, so each must work with its schema
Blue keeps running after the flip, so going back is one more flip.

The aha is step 5: blue keeps running. If green's numbers worsen, you flip back in seconds, because nothing needs to start. The flip can be a load balancer setting or a Kubernetes Service's label selector, which picks the pods it sends traffic to.

The costs: double capacity while both stacks stand, a bug that reaches every user at once, and time for in-flight requests to finish on blue, called draining. The database is usually shared, so if green writes data that blue cannot read, flipping back breaks blue.

Canary: one table first

A canary release sends a small share of traffic, for example 5 percent, to v2 while the rest stays on the stable v1. Coal miners once carried canaries, which showed the effects of poisonous gas before people did; here, only the canary's users meet a bad v2.

You compare v2 with v1 over the same minutes. The error rate is the share of requests that fail. Latency is how long requests take, read at a high percentile such as p99, the time within which 99 percent of requests finish (see the golden signals). If they match, raise the share; if not, send everything back. In the flowchart, follow the pink arrows: every check has a way out.

Flowchart. Deploy the v2 canary beside stable v1, then send it 5 percent of traffic. A check compares canary and stable on error rate and latency for a set time. On pass, traffic to v2 rises to 25 percent, another check, then 50 percent, another check, then 100 percent, where v2 is the new stable version. From each of the three checks a pink dashed fail arrow leads to roll back: all traffic to stable v1, so only the current share ever saw v2. Shares and steps are illustrative
Each step raises v2's share only after a check, and every check can roll back.

A canary needs a router that splits traffic by weight, such as a load balancer, a Kubernetes ingress controller or a service mesh, a layer of proxies between services. A plain Service spreads connections evenly across pods, so 1 canary pod beside 19 stable ones gets about a twentieth.

It also needs metrics per version, and enough traffic: 5 percent of 100 requests an hour is five requests, too few to judge. Both versions run at once, so the rolling-update compatibility rule applies. Lesson 10 automates the steps and lets metrics decide.

How to choose a deployment strategy

The table compares the four side by side.

StrategyDowntimeExtra capacityRolling backA bad release reaches
RecreateYes, while v2 startsNoneRedeploy v1, another gapEveryone
Rolling updateNoneOne batchRoll v1 out, batch by batchA growing share
Blue-greenNoneDouble, for a whileFlip back in secondsEveryone, until the flip back
CanaryNoneA few instancesSend the share back to v1Only the canary share
  • Risk. How bad is a bug? A payment service deserves a canary; an internal report page can roll, or even recreate.
  • Cost. Blue-green doubles capacity during the overlap: cheap for an hour on a small service, costly for a large fleet.
  • Database compatibility. Every strategy except recreate runs two versions against one database, so make schema changes backward compatible (lesson 10 shows how) or accept a planned recreate.
  • Detection speed. A canary helps only if per-version metrics and enough traffic reveal trouble within its watch time.

Many teams roll by default, add canaries for busy, risky services, and use blue-green when users must never meet mixed versions.

Check yourself

Your v2 renames the database column name to full_name when it starts, and you ship it with a rolling update. What breaks during the rollout, and why would blue-green not save you?

The v1 instances still serving query name, which no longer exists, so their requests fail. Blue and green share the database, so flipping back hits the same missing column; add the new column first instead.

A service handles about 30 requests an hour. Is a 5 percent canary watched for 15 minutes a good plan?

No. In 15 minutes v2 would see fewer than one request, so its numbers tell you nothing. Watch longer, send a bigger share, or use blue-green with smoke tests.

Green went live ten minutes ago and its error rate has doubled. Why is undoing blue-green faster than undoing a rolling update?

Blue is still running, so you only flip the load balancer back, in seconds. A rolling update has already stopped the old instances, so going back means starting them again, batch by batch.