Prompt
How do I set up blue/green deployments with automated rollback?
Latest observation
Blue/green deployments with automated rollback are usually built around two identical environments:
- Blue = current production
- Green = new version being prepared
You deploy to green, run checks, then switch traffic from blue to green. If health checks fail after the switch, you automatically switch back.
Basic flow
-
Provision two identical stacks
- Same app versioning, config, dependencies, network setup
- One is live (blue), one is idle (green)
-
Deploy the new release to green
- Build immutable artifacts
- Run database migrations carefully
- Start app and warm caches if needed
-
Validate green before cutover
- Smoke tests
- Health checks
- Integration tests
- Optional canary-like internal traffic tests
-
Switch production traffic to green
- Via load balancer, ingress, DNS, service mesh, or router
-
Monitor post-cutover
- Error rate, latency, saturation, business metrics
- Compare against baseline
-
Automated rollback if unhealthy
- If thresholds are breached within a watch window, route traffic back to blue
- Keep blue intact until confidence is high
What you need for automated rollback
1) A traffic switch you can reverse quickly
Common options:
- Load balancer target groups (AWS ALB/NLB)
- Kubernetes Service selector / ingress
- Service mesh routing (Istio, Linkerd, App Mesh)
- DNS is possible, but rollback can be slower due to caching
Best practice: use something with fast, deterministic failback.
2) Health and business checks
Don’t rely only on “process is up.”
Use:
- Liveness: process running
- Readiness: app can serve traffic
- Smoke tests: login, checkout, core API calls
- Metrics:
- 5xx rate
- p95/p99 latency
- timeout rate
- saturation
- key business KPIs
3) A deployment controller/pipeline
Your CI/CD pipeline should:
- deploy to green
- test green
- flip traffic
- watch metrics for a fixed period
- rollback automatically if thresholds fail
4) Safe database strategy
Rollback is often blocked by bad schema changes. To make rollback possible:
- Use backward-compatible migrations
- Expand/contract pattern:
- add new columns/tables first
- deploy code that reads/writes both if needed
- later remove old columns
- Avoid destructive schema changes during the cutover window
Example architecture
Kubernetes example
- Two deployments:
app-blueapp-green
- One service points to active version
- Ingress or service selector switches traffic
- CI/CD pipeline:
- Deploy
app-green - Run readiness + smoke tests
- Update service selector to green
- Observe metrics for 10 minutes
- If error budget breached, revert selector to blue
- Deploy
AWS example
- Two target groups behind an ALB
- Blue target group serves current traffic
- Green target group receives new version
- Use weighted forwarding or direct switch
- CloudWatch alarms monitor:
- 5xx errors
- target response time
- unhealthy hosts
- Alarm triggers rollback Lambda or pipeline step
Rollback logic
Define clear rollback conditions, for example:
- 5xx rate > 2% for 3 consecutive minutes
- p95 latency > 2x baseline for 5 minutes
- checkout failures exceed threshold
- readiness probe failures on new version
- synthetic test fails after cutover
Avoid vague criteria. Make the trigger objective and automated.
Important pitfalls
Database migrations
Most rollbacks fail here. Make schema changes reversible or backward compatible.
Sticky sessions
If your app uses sessions, ensure:
- shared session store
- or session affinity handled safely
- otherwise switching traffic may log users out
Cache and background jobs
- Warm caches on green before switching
- Prevent duplicate job consumers from processing the same tasks
- Pause/coordinate background workers during cutover if needed
Config drift
Blue and green must be truly identical except for version. Use IaC and immutable artifacts.
Observability
You need:
- logs
- metrics
- traces
- deployment annotations in dashboards
Without observability, automated rollback becomes guesswork.
A practical implementation pattern
- Build and publish versioned artifact
- Deploy to green environment
- Run pre-cutover tests
- Switch traffic
- Start a watch timer
- Evaluate metrics continuously
- If unhealthy, route back to blue and page an operator
- If healthy after the observation window, mark green as new baseline
- Tear down old blue only after a retention period
Minimal pseudo-workflow
deploy green
if precheck fails:
abort
shift traffic to green
start monitoring window
if any rollback condition met:
shift traffic back to blue
alert
else:
promote green to blue
Tools that can help
- Kubernetes: Argo Rollouts, Flagger
- AWS: CodeDeploy blue/green, ALB target groups, CloudWatch alarms
- GitHub Actions / GitLab CI / Jenkins: pipeline orchestration
- Service mesh: Istio for traffic shifting and metrics-based rollback
If you want, I can give you:
- a Kubernetes/Argo Rollouts example,
- an AWS ALB/CodeDeploy example, or
- a generic CI/CD pipeline template with rollback logic.