What is Blue-Green Deployment?
Blue-green deployment is a release strategy that eliminates downtime by running two identical production environments simultaneously. One environment -- call it blue -- serves live traffic. You deploy your new version to the other -- green -- test it thoroughly, then switch the load balancer to route all traffic to green. Blue remains idle as an instant rollback target. If the new version has a critical bug, switching back takes seconds.
The pattern was described by Martin Fowler and has become a standard technique for teams that need zero-downtime releases. It sits alongside rolling deployments and canary releases as one of three main traffic-shifting strategies in modern deployment practice. All three rely on the containerisation and orchestration that Docker and Kubernetes provide.
How the Switch Works
The core mechanism is simple: a load balancer (or DNS, or a Kubernetes Service selector) points to either the blue or green environment. Switching is atomic from the outside -- users do not see a transition state, they see one version or the other.
Here is a blue-green deployment implemented in Kubernetes using two Deployments and a Service that switches between them by changing its selector:
# blue-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-blue
labels:
app: myapp
slot: blue
spec:
replicas: 3
selector:
matchLabels:
app: myapp
slot: blue
template:
metadata:
labels:
app: myapp
slot: blue
spec:
containers:
- name: myapp
image: myapp:1.4.2
ports:
- containerPort: 3000
readinessProbe:
httpGet:
path: /healthz
port: 3000
initialDelaySeconds: 5
periodSeconds: 10
---
# green-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-green
labels:
app: myapp
slot: green
spec:
replicas: 3
selector:
matchLabels:
app: myapp
slot: green
template:
metadata:
labels:
app: myapp
slot: green
spec:
containers:
- name: myapp
image: myapp:1.5.0 # new version
ports:
- containerPort: 3000
readinessProbe:
httpGet:
path: /healthz
port: 3000
initialDelaySeconds: 5
periodSeconds: 10
---
# service.yaml -- switch traffic by changing 'slot: blue' to 'slot: green'
apiVersion: v1
kind: Service
metadata:
name: myapp
spec:
selector:
app: myapp
slot: blue # change to 'green' to switch traffic
ports:
- port: 80
targetPort: 3000
The traffic switch is a single kubectl patch command:
kubectl patch service myapp -p '{"spec":{"selector":{"slot":"green"}}}'
To roll back:
kubectl patch service myapp -p '{"spec":{"selector":{"slot":"blue"}}}'
The readiness probe ensures green pods only receive traffic once they are healthy. Blue pods remain running and can take traffic back instantly.
The Database Migration Problem
Infrastructure switching is the easy part. Databases are the hard part of blue-green deployments.
During the cutover window, both blue and green may need to read and write to the same database. If your new version requires a schema change -- adding a NOT NULL column, renaming a column, changing a data type -- you cannot simply run the migration before switching, because blue is still running with the old schema expectations.
The correct technique is the expand-contract pattern (also called parallel change):
- Expand: run a migration that is backward-compatible with both versions -- add the new column as nullable alongside the old one. Both blue and green can now function.
- Deploy green: switch traffic to the new version, which writes to both columns.
- Contract: after blue is retired and green is stable, run a cleanup migration to remove the old column and enforce the new constraint.
This requires two separate deployment cycles for every breaking schema change, but it is the only safe approach for zero-downtime releases with a shared database.
Blue-Green vs Rolling vs Canary
Blue-green gives the cleanest instant rollback but requires double the infrastructure capacity and cannot partially validate with real traffic before a full switch.
Rolling replaces instances incrementally -- resource-efficient, but rollback means running another rolling update in reverse, which takes time proportional to your fleet size.
Canary sends a fraction of real traffic to the new version first -- ideal for validating behaviour under production conditions before a full rollout. The cost is added complexity in the traffic routing layer.
For most teams, canary deployments via feature flags or weighted routing are the most practical combination of safety and resource efficiency. Blue-green is the right choice when an instant clean rollback is the non-negotiable requirement.
Deployment strategies are core DevOps tools knowledge that CloudPros covers in the Kubernetes and CI/CD modules.
