Blue-Green Deployment — Zero-Downtime Deployments Guide
In this tutorial, you'll learn about Blue. We cover key concepts, practical examples, and best practices.
Blue-green deployment is a release strategy that maintains two identical production environments (blue and green) to enable zero-downtime deployments and instant rollback capability.
What You'll Learn
You'll learn how blue-green deployment works, how to set it up with Docker and load balancers, and when to choose it over rolling updates or canary releases. This guide includes a working mini project you can run locally.
Why This Matters
Traditional deployment methods cause downtime. Stop the old version, start the new version — users get error pages, dropped connections, and frustration. For a site generating $100k/hour in revenue, even 5 minutes of downtime costs over $8,000. Blue-green deployment eliminates this by keeping both versions live and switching traffic atomically.
Real-World Use
Netflix uses blue-green deployment combined with canary analysis to push changes to their streaming platform. A new version (green) runs alongside production (blue) for hours while metrics are monitored. If error rates spike, the load balancer sends traffic back to blue instantly. Doda Browser uses a similar zero-downtime strategy for its extension update pipeline.
System Design Learning Path
flowchart LR
A[CI/CD Pipeline] --> B[Blue-Green Deployment]
B --> C{You Are Here}
C --> D[Canary Deployment]
C --> E[Immutable Infrastructure]
style C fill:#f90,color:#fff
How Blue-Green Deployment Works
You maintain two separate but identical environments, each behind a load balancer:
- Blue — currently serving all production traffic
- Green — idle, running the same application version
When deploying a new release, you deploy to the idle environment (green), run smoke tests, then switch the load balancer to send all traffic to green. Blue becomes idle and serves as your immediate rollback target.
flowchart LR
subgraph Before["Before Deploy"]
LB1[Load Balancer] --> B1[Blue - Live]
LB1 -.- G1[Green - Idle]
end
subgraph During["Deploy to Green"]
LB2[Load Balancer] --> B2[Blue - Live]
LB2 -.-> G2[Green - Deploying]
end
subgraph After["Switch Traffic"]
LB3[Load Balancer] -.-> B3[Blue - Idle]
LB3 --> G3[Green - Live]
end
style B1 fill:#4a90d9,color:#fff
style B2 fill:#4a90d9,color:#fff
style G2 fill:#e67e22,color:#fff
style G3 fill:#27ae60,color:#fff
Key Components
1. Load Balancer Traffic Switch
The load balancer is the single control point for the cutover. A DNS-level switch (changing A records) is slower and less reliable than an application-level load balancer like NGINX or AWS ALB.
# NGINX blue-green switch example
upstream app {
server blue.example.com:8080 weight=100; # Blue gets 100% traffic
server green.example.com:8080 weight=0; # Green gets 0% traffic
}
# After switch:
upstream app {
server blue.example.com:8080 weight=0;
server green.example.com:8080 weight=100; # Green now live
}
2. Database Compatibility
The database is the hardest part of blue-green deployment. Both versions must work with the same database schema. Use backward-compatible migrations: add columns before they're used, remove columns after all traffic is migrated.
-- Safe migration: add column first, deploy, then remove old
ALTER TABLE users ADD COLUMN display_name VARCHAR(100);
-- After green is live and verified:
-- ALTER TABLE users DROP COLUMN old_name;
3. State and Sessions
If your app stores sessions in memory, switching traffic to green loses all active sessions. Use external session stores (Redis, Memcached) or sticky sessions during the transition.
Blue-Green vs Other Deployment Strategies
| Strategy | Downtime | Rollback Speed | Cost | Complexity |
|---|---|---|---|---|
| Blue-Green | Zero | Instant (switch back) | 2x infra | Medium |
| Rolling Update | Minimal | Slow | Same infra | Low |
| Canary | Zero | Moderate | Partial 2x | High |
| Recreate | Full deploy time | Full redeploy | Same infra | None |
Common Mistakes
1. Shared Database Write Problems
Both environments share the same database. If the green deploy triggers schema migrations that break the blue version, switching green back becomes impossible.
2. Not Testing the Idle Environment
Deploying to green without running smoke tests is just optimism. Always verify the green environment serves correct responses before switching traffic.
3. Sticky Sessions Without Timeout
Sticky sessions keep users pinned to one environment. If you switch traffic without draining sticky sessions, some users stay on the old version indefinitely.
4. Ignoring Background Jobs and Workers
Your web servers may switch to green, but background workers (Celery, Sidekiq) might still run on blue. Both environments need all process types.
5. Premature Blue Cleanup
Deleting the blue environment immediately after switching leaves no rollback path. Keep blue running for at least 24–48 hours until the new version is validated in production.
6. Inconsistent Configuration
Environment-specific configuration (API keys, feature flags) must be identical between blue and green. Use a config management system, not environment variables set differently per environment.
7. Forgetting DNS TTL
If you use DNS for traffic switching, low TTL (60 seconds) is essential. High TTL values (300+ seconds) mean users stay on the old IP long after you switch.
Practice Questions
1. What is the main disadvantage of blue-green deployment?
It requires double the infrastructure — you pay for two production environments, doubling your compute costs. For large-scale systems, this is significant.
2. How do you handle database migrations with blue-green?
Use backward-compatible migrations. Add new columns/tables before they're needed. Only drop old columns after verifying the new version works and can be rolled back safely.
3. What happens to in-memory sessions during a traffic switch?
They're lost unless you use an external session store (Redis, database). Consider using sticky sessions temporarily during the cutover period.
4. When should you use canary instead of blue-green?
Canary releases suit risky deployments where you want to test with 1–5% of traffic before full rollout. Blue-green is better when instant rollback and 100% traffic testing matter more than cost.
5. Challenge: Automate the full blue-green pipeline.
Write a CI/CD script that deploys to the idle environment, runs health checks, switches traffic, monitors for 30 minutes, and rolls back automatically if error rates exceed 1%.
Mini Project: Blue-Green with Docker Compose
# docker-compose.blue.yml
version: '3'
services:
web:
image: myapp:blue
ports:
- "8080:80"
environment:
- APP_ENV=blue
db:
image: postgres:15
volumes:
- db_data:/var/lib/postgresql/data
volumes:
db_data:
# docker-compose.green.yml
version: '3'
services:
web:
image: myapp:green
ports:
- "8081:80"
environment:
- APP_ENV=green
db:
image: postgres:15
volumes:
- db_data:/var/lib/postgresql/data
# switch.sh - atomically switch traffic
NGINX_CONFIG="/etc/nginx/sites-enabled/app"
cp "$NGINX_CONFIG.blue" "$NGINX_CONFIG"
nginx -s reload
echo "Switched to BLUE"
# To switch to green:
cp "$NGINX_CONFIG.green" "$NGINX_CONFIG"
nginx -s reload
echo "Switched to GREEN"
Expected output:
Switched to BLUE
FAQ
What's Next
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro