DevOps & Containers
Blue-Green Deployment: Switch Releases Instantly and Roll Back Safely
By NileVPS Team · Published · 4 min read
اقرأ المقال بالعربيDeploying a new version directly over the running one is risky: for a few seconds or minutes the application is half old and half new, and if something goes wrong you have to rebuild the previous state under pressure. Blue-green deployment avoids this by keeping two complete, identical environments. One serves real users while the other receives the new release. When the new release is ready and verified, traffic is switched over in one step, and the old environment stays intact as an instant fallback.
How blue-green deployment works
The two environments are traditionally called blue and green. At any moment one of them is live.
- Blue is live. All user traffic goes to blue, which runs version 1.
- Deploy to green. Version 2 is installed on green, which receives no public traffic.
- Test green. Run smoke tests, health checks and a few manual checks against green directly, using an internal hostname or port.
- Switch traffic. The router, load balancer or reverse proxy is pointed at green. Users now get version 2.
- Keep blue on standby. If errors appear, point traffic back to blue. Rollback is a configuration change, not a redeploy.
- Next release. Blue becomes the staging target for version 3, and the roles swap again.
The key property is that the switch is atomic from the user's point of view: requests go either to the old version or to the new one, never to a half-installed mix.
Where the switch happens
| Switch point | How it works | Notes |
|---|---|---|
| Reverse proxy (Nginx, HAProxy, Caddy) | Change the upstream and reload | Fast and simple on a single VPS |
| Load balancer | Move the target group or pool | Common with multiple servers |
| DNS | Change the record to a new IP | Slow and unpredictable because of caching; avoid as the main mechanism |
| Container orchestrator | Change a Kubernetes Service selector | Instant inside the cluster |
A simple example on one VPS with Nginx
You can practice blue-green deployment on a single server by running two copies of the application on different local ports, for example as two containers or two systemd services.
# /etc/nginx/conf.d/app-upstream.conf
upstream app_live {
server 127.0.0.1:8001; # blue
# server 127.0.0.1:8002; # green
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://app_live;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}A deploy script then follows this sequence:
# 1. Start the new version on the idle port (green = 8002)
docker run -d --name app-green -p 127.0.0.1:8002:8000 myapp:2.0.0
# 2. Wait for it to report healthy
curl -fsS http://127.0.0.1:8002/health
# 3. Point Nginx at green, validate the config and reload gracefully
sudo sed -i 's/127.0.0.1:8001/127.0.0.1:8002/' /etc/nginx/conf.d/app-upstream.conf
sudo nginx -t && sudo systemctl reload nginxnginx -s reload and systemctl reload nginx start new worker processes with the new configuration while old workers finish their in-flight requests, so users do not see dropped connections. Keep the blue container running until you are confident in green.
The hard part: databases and state
Application servers are easy to duplicate. Data is not. Both environments usually share one database, which means the schema must work with both versions during the switch.
Use the expand and contract pattern:
- Expand: add new columns or tables in a backward-compatible way. Old code ignores them.
- Deploy and switch: the new version starts using the new structure.
- Contract: only after blue is retired, remove old columns in a later release.
Never rename or drop a column in the same release that stops using it; rollback would break immediately.
Other state to plan for:
- Sessions: store them in Redis or the database, not in local memory or files, so users stay logged in after the switch.
- Uploaded files: use shared or object storage.
- Background jobs and cron: make sure only the live environment runs scheduled tasks, or that jobs are idempotent.
- Caches: consider warming the green environment before the switch.
Pros and cons
Advantages
- Near-instant rollback.
- Full testing of the new version in a production-like environment before users see it.
- Simple mental model for the whole team.
Drawbacks
- Double the resources during the release window.
- Database changes need discipline.
- All users move at once, so a bug that only appears under real load affects everyone. A canary deployment reduces that risk by moving traffic gradually.
Checklist before your first blue-green release
- A health endpoint that checks the app and its dependencies.
- A scripted, repeatable switch, not manual edits.
- Shared sessions and file storage.
- Backward-compatible migrations.
- Monitoring of error rate and latency right after the switch.
- A documented rollback command, tested at least once.
Blue-green is one of several techniques for zero-downtime deployment. On a VPS with snapshots you can also take a snapshot before a risky release as an extra safety net.
FAQ
Do I need two servers for blue-green deployment?
No. Two application instances on one VPS behind a reverse proxy are enough to get the main benefit. Separate servers add protection against host-level failures but cost more.
How long should I keep the old environment running?
Long enough to be confident in the new release, often from a few minutes to a day depending on traffic and risk. Keep it until you have seen normal traffic patterns, then shut it down or reuse it for the next deploy.
What is the difference between blue-green and canary deployment?
Blue-green switches all traffic at once between two full environments. Canary sends a small percentage of traffic to the new version first and increases it step by step while watching metrics.