What Is Blue-Green Deployment?
Blue-green deployment maintains two identical production environments. At any time, one is live (blue) and one is idle (green). New releases deploy to the idle environment, then traffic switches over.
How It Works
- Blue is currently serving production traffic
- Deploy new version to Green
- Run tests against Green
- Switch the load balancer to point to Green
- Green is now live; Blue becomes idle
- If problems occur, switch back to Blue instantly
Implementation with Nginx
# /etc/nginx/conf.d/upstream.conf
upstream app {
server 127.0.0.1:3001; # Blue
# server 127.0.0.1:3002; # Green (uncomment to switch)
}Scripted Switchover
#!/bin/bash
CURRENT=$(grep "server 127" /etc/nginx/conf.d/upstream.conf | grep -v "#" | awk '{print $2}' | tr -d ';')
if [[ "$CURRENT" == "127.0.0.1:3001" ]]; then
NEW_PORT=3002
NEW_ENV="green"
else
NEW_PORT=3001
NEW_ENV="blue"
fi
echo "Deploying to $NEW_ENV (port $NEW_PORT)..."
# Deploy application to the new port
# Run health checks
curl -f http://127.0.0.1:$NEW_PORT/health || exit 1
# Switch traffic
sed -i "s/server 127.0.0.1:.*/server 127.0.0.1:$NEW_PORT;/" /etc/nginx/conf.d/upstream.conf
nginx -t && systemctl reload nginx
echo "Traffic switched to $NEW_ENV"Benefits
- Zero downtime — no gap between old and new version
- Instant rollback — switch back in seconds
- Test in production — verify the new version before switching
Considerations
- Requires 2x the server resources (two environments)
- Database migrations need careful handling
- Session state may need external storage (Redis)