Blue-Green Deployment Strategy
Two-environment switch with instant rollback, traffic shifting, and database migration safety.
Why
Blue-green keeps two identical production environments and shifts 100% of traffic atomically. New version (green) is staged and smoke-tested while blue serves live traffic; the switch is instant and rollback is just flipping back.
Zero downtime, no partial-rollout inconsistency.
When
Use blue-green for stateless web apps where you need instant rollback and can afford 2x infrastructure. Avoid for apps with long-running WebSockets (connections break on switch) or without a load balancer that supports target swapping.
How
Example
Common Problems
- ⚠Database migration breaks blue — green runs a destructive migration while blue still serves traffic
- ⚠Sticky sessions break on switch — users logged into blue get 401 on green (different session store)
- ⚠No monitoring window — switch happens, team walks away, error spike detected 30 min later in prod
- ⚠2x infra cost always on — green stays provisioned 24/7 even between deploys, wasting budget
Best Practices
- ✓Use expand/contract DB migrations — green adds (expand), switch, then remove old (contract) in next deploy
- ✓Share session store (Redis) between blue and green — sticky sessions survive the switch
- ✓Automate the 10-min monitoring window — script rolls back on error-rate threshold, no human needed
- ✓Scale green to 0 between deploys — spin up only during deploy, saves 50% infra cost
FAQ
How is blue-green different from a rolling update?
Blue-green switches all traffic at once to the new (green) environment, keeping the old (blue) idle for instant rollback. Rolling updates replace instances gradually — slower but need only spare capacity, not a full duplicate environment.
How do I handle database migrations in blue-green?
Migrations must be backward-compatible — both blue and green run against the same DB during the switch. Apply additive migrations (new columns) before deploying, destructive ones (drop columns) only after blue is decommissioned.
What if the green environment fails health checks?
Traffic stays on blue — the switch never happens. Monitor green's health endpoint before routing traffic. Set an auto-rollback: if green error rate exceeds 1% in the first 5 min, route back to blue automatically.
How do I manage two environments cost-effectively?
Keep blue at full capacity (serves traffic) and green at minimum scale (1 instance) until health checks pass, then scale green up and blue down. This halves the cost vs. running both at full capacity permanently.
When is blue-green a bad choice?
When your app uses WebSockets or long-lived connections — the instant switch drops all active sessions. Use rolling updates or a canary strategy instead. Blue-green is best for stateless HTTP request/response apps.