Sub topic 11
Deployment strategies: rolling, blue/green, and canary
Prerequisites and scope
This chapter assumes you know what a pipeline, an artifact, and an environment are. It also assumes you have read the chapters on continuous delivery and continuous deployment. You should be comfortable reading simple YAML and know what a load balancer is: a component that spreads incoming requests across several copies of an application.
A deployment strategy is the method a pipeline uses to replace the running version of an application with a new one. The deploy step in the earlier ci-cd workflow calls ./scripts/deploy.sh dist/. This chapter is about what such a script does, and why the choice matters.
The problem: replacing software that is in use
The simplest deployment stops the old version, copies the new version into place, and starts it. This is called a recreate deployment. It is easy to reason about, but users see an outage between the stop and the start. It is acceptable for internal tools or batch jobs. It is rarely acceptable for a service people use all day.
The three strategies below avoid that outage by running old and new code side by side for some period. They differ in how much extra capacity they need, how fast they switch, and how much risk reaches users before you notice a problem.
| Strategy | Extra capacity | Switch speed | Users exposed to a bad release | Rollback speed |
|---|---|---|---|---|
| Recreate | None | Instant, with downtime | All | Slow (redeploy) |
| Rolling | Small | Gradual | Growing share | Moderate |
| Blue/green | A full second environment | Instant | All, after the switch | Very fast |
| Canary | Small | Gradual, in steps | A small share first | Fast |
Rolling deployment
A rolling deployment replaces instances of the application a few at a time. Suppose you run six instances. The platform removes two from the load balancer, updates them, checks that they are healthy, adds them back, and then moves on to the next two. At every moment, some instances serve traffic.
Key properties:
- No extra environment. You need only a little spare capacity, often one or two instances.
- Mixed versions. For a while, version 1 and version 2 serve requests at the same time. Your code must tolerate this.
- Health checks decide progress. The rollout continues only if new instances report healthy. Without a good health check, a rolling deployment happily replaces every working instance with a broken one.
- Rollback is another rollout. Going back means rolling the old version out again, which takes as long as the original deployment.
Kubernetes uses this strategy by default. The settings maxUnavailable and maxSurge control how many instances may be down and how many extra may be created at once.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 6
selector:
matchLabels:
app: web
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1
maxSurge: 2
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: registry.example.com/web:2.0.0
ports:
- containerPort: 8080
readinessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 5
periodSeconds: 5The readinessProbe is what stops a bad version from spreading. Kubernetes sends traffic to a new instance only after the probe succeeds, and it pauses the rollout if new instances never become ready.
The mixed-version problem
Because two versions run at once, a change that breaks compatibility can cause errors even though each version works alone. The usual case is a database schema change. If version 2 renames a column and version 1 still reads the old name, requests handled by version 1 fail mid-rollout.
The standard remedy is the expand and contract pattern:
- Expand. Deploy a schema change that only adds things, such as a new column, and keep the old one.
- Migrate. Deploy code that writes to both and reads from the new one.
- Contract. After no old code remains, remove the old column in a later release.
This applies to blue/green and canary releases too. Any strategy that overlaps versions needs backward-compatible changes.
Blue/green deployment
A blue/green deployment keeps two complete production environments. One, say blue, serves all traffic. The other, green, is idle. You deploy the new version to green, test it, and then switch the router or load balancer so that all traffic goes to green. Blue stays up, unused, as a fallback.
The diagram below shows the flow.
Key properties:
- Fast, clean switch. Users move from old to new in one step, so there is no long period of mixed versions at the router level.
- Fast rollback. Pointing the router back at blue takes seconds, because blue is still running.
- Pre-switch testing. You can run smoke tests against green with real production configuration before any user reaches it.
- Cost. You pay for two full environments during the deployment. Many teams shut the idle one down afterward, or reuse it as the next target.
- All-or-nothing exposure. After the switch, every user hits the new version. A bug that only appears under real load affects everyone until you roll back.
Shared state is the hard part. Both environments usually share one database, so the schema must work with both versions, as in the rolling case. Sessions, caches, and queued jobs also need thought. A job queued by green may be picked up by blue after a rollback.
Canary deployment
A canary deployment sends the new version to a small share of traffic first, watches how it behaves, and increases the share only if it looks good. The name comes from the canary that miners carried to warn of bad air: a small, early signal of danger.
A typical progression is 1%, 5%, 25%, 50%, then 100%, with a pause at each step. The canary instances run alongside the stable ones, and the router splits traffic by weight.
Key properties:
- Limited blast radius. If the new version has a defect, only the canary share of users meets it.
- Real-traffic evidence. You learn how the release behaves under production load and data, which staging rarely reproduces.
- Needs good metrics. The decision to proceed depends on comparing the canary against the stable version on error rate, latency, and business signals. Without monitoring, a canary is only a slow rolling deployment.
- More machinery. You need weighted routing, per-version metrics, and ideally automated analysis. Tools such as Argo Rollouts, Flagger, and service meshes provide this.
- Sample size. At 1% of traffic, a rare error may not show up within the pause. Choose step durations long enough to collect meaningful data.
Sticky routing is another detail. If a user bounces between versions on successive requests, they may see inconsistent behavior. Routing by user ID or session keeps each user on one version.
Wiring a strategy into the pipeline
The pipeline's job is to run the deployment steps in order and stop on failure. A rolling update in the deploy job can be as short as this addition to the earlier workflow, which applies the manifest and waits for the rollout to finish:
deploy:
needs: build
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v4
- run: kubectl apply -f k8s/deployment.yaml
- run: kubectl rollout status deployment/web --timeout=300s
- name: Roll back if the rollout failed
if: failure()
run: kubectl rollout undo deployment/webThis assumes the runner already has cluster credentials configured. The rollout status command exits with an error if the rollout does not complete in time. The final step then runs because of if: failure() and reverts to the previous version.
Note that this reuses the artifact built earlier. The strategy changes how the artifact reaches users, not what the artifact is.
Choosing a strategy
- Choose rolling when you want low cost and simplicity, your changes are backward compatible, and a few minutes of mixed versions is fine.
- Choose blue/green when you need an instant switch and instant rollback, can afford duplicate capacity, and want to test the full environment before exposure.
- Choose canary when failures are expensive, traffic is high enough to give signal quickly, and you have the monitoring to judge the result.
These can be combined. Many teams use blue/green for the environment switch and a canary-style traffic ramp on top of it. Feature flags, covered in the next chapter, add a further layer by separating deploying code from turning it on.
Common mistakes
- No health checks. A rolling update without a readiness probe has no way to detect a bad version.
- Breaking schema changes. Overlapping versions share a database. Use expand and contract.
- Unwatched canaries. Sending 5% of traffic to a version nobody monitors only delays the failure.
- Untested rollback. A rollback path that has never been run often fails when needed, for example because the old version cannot read data the new version wrote.
- Leaving the idle environment stale. In blue/green, the idle side drifts from production configuration unless the pipeline redeploys it.
Exercise
Your team runs a service on eight instances. A release renames a database column. You use a rolling deployment, and during the rollout users see intermittent errors. Explain why, and describe how to ship the rename safely.
Show answer
During a rolling deployment, old and new versions run at the same time against the same database. Instances still on the old version read the old column name, which no longer exists, so some requests fail. The safe approach is expand and contract. First add the new column while keeping the old one. Then deploy code that writes to both columns and reads from the new one. Once no old version is running, remove the old column in a later release.
Exercise
A payment service handles high traffic and a defect would cost money. You have strong error-rate and latency monitoring, and a router that supports weighted traffic. Which strategy fits best, and what would you check at each step?
Show answer
A canary deployment fits best. It exposes only a small share of users to a defect first, and the monitoring lets you judge the new version against the stable one. At each step, compare error rate, latency, and payment success rate for the canary and the stable version, and wait long enough to collect enough requests. If any metric is worse than the stable version beyond an agreed threshold, send all traffic back to the stable version.