Deployment Strategies: Rolling, Blue-Green, Canary
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.
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.
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.
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.
| Strategy | Downtime | Extra capacity | Rolling back | A bad release reaches |
|---|---|---|---|---|
| Recreate | Yes, while v2 starts | None | Redeploy v1, another gap | Everyone |
| Rolling update | None | One batch | Roll v1 out, batch by batch | A growing share |
| Blue-green | None | Double, for a while | Flip back in seconds | Everyone, until the flip back |
| Canary | None | A few instances | Send the share back to v1 | Only 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.