Sub topic 11
Deployment strategies: rolling, blue/green, and canary
This chapter assumes you know what a pipeline, an artifact, and an environment are, and that you can read a simple YAML workflow. A deployment strategy is the method a pipeline uses to replace the running version of an application with a new one. The strategy decides how much risk users see and how fast you can undo a bad release.
The problem a strategy solves
The simplest approach is to stop the old version and start the new one. This causes downtime, and if the new version is broken, every user sees the failure at once. The three strategies below avoid both problems in different ways.
Rolling deployment
A rolling deployment replaces instances of the application in small batches. A load balancer sends traffic only to healthy instances. While the rollout runs, old and new versions serve users at the same time.
- Needs no extra capacity beyond a small buffer.
- Is the default in many platforms, such as Kubernetes Deployments.
- Rollback means rolling forward again with the previous artifact, which takes as long as the original rollout.
Because two versions run together, they must be compatible with the same database schema and the same API contracts.
Blue/green deployment
Blue/green keeps two complete environments. Blue serves live traffic. You deploy the new version to green, test it there, then switch the router so all traffic goes to green. Blue stays idle and ready.
- Switching is nearly instant, and so is rollback: point the router back at blue.
- Users never see a mix of versions.
- It costs roughly double the infrastructure while both environments exist.
- Database changes are the hard part, because both environments usually share one database.
Canary deployment
A canary deployment sends the new version to a small share of traffic first, for example 5%. You watch error rates and latency. If they stay healthy, you raise the share in steps until it reaches 100%. If they degrade, you route everything back to the old version.
Canary limits the damage of a bad release to a few users. It needs good monitoring and a router that can split traffic by percentage, so it is more work to set up.
Comparing the strategies
| Strategy | Extra infrastructure | Rollback speed | Users exposed to a bad release | Main difficulty |
|---|---|---|---|---|
| Rolling | Low | Slow | Grows as the rollout proceeds | Mixed versions run together |
| Blue/green | High (two full environments) | Very fast | All users after the switch | Shared database changes |
| Canary | Low to medium | Fast | A small percentage | Needs metrics and traffic splitting |
A rolling update in a pipeline
The pipeline from the earlier chapters ends by calling a deploy script. The strategy lives inside that step. This Kubernetes example performs a rolling update with controlled batch sizes.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 4
selector:
matchLabels:
app: web
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1
maxSurge: 1
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: registry.example.com/web:1.4.2
readinessProbe:
httpGet:
path: /healthz
port: 8080maxUnavailable: 1 means at most one instance may be down during the update. maxSurge: 1 allows one extra instance above the target count. The readiness probe tells the platform when a new instance can receive traffic, so a broken version stalls the rollout instead of replacing every healthy instance.
Exercise
Your service stores data in one shared database and you need to roll back within seconds if a release fails. Which strategy fits best, and what risk remains?
Show answer
Blue/green fits best, because rollback is a router switch back to the old environment. The remaining risk is the shared database: if the new version changed the schema in a way the old version cannot read, switching back can still fail. Make schema changes backward compatible.
Exercise
In a canary release at 5% traffic, the error rate of the new version doubles compared with the stable version. What should the pipeline do?
Show answer
Stop increasing the share and route all traffic back to the stable version, then investigate. The point of a canary is to limit the damage to a small group of users.