Blue-green deployments: releases you can switch back in seconds
Short answer: Blue-green deployment means running two identical production environments. One, say blue, serves customers while you deploy the new version to the other, green. When green passes its checks, you switch traffic over; if something goes wrong, you switch back. The switch takes seconds, so releases stop needing maintenance windows. The hard parts are the database, background jobs and cost, so plan those before the first switch.
The idea in one paragraph
Martin Fowler's well-known description sets it out clearly: you have two production environments as identical as possible. At any time one is live. You do your final testing on the idle one, then switch the router so all incoming requests go to it, leaving the old one idle. He also notes the main benefit: if anything goes wrong, you switch the router back to the previous environment, which gives you a rapid way to roll back.
Compare that with a typical small-team deploy, where the new code overwrites the old on the same servers. AWS's whitepaper on blue/green deployments points out the weakness: rolling back then means redeploying an earlier version from scratch, which takes time and can leave the application unavailable.
What you need before you start
Blue-green is a pattern, not a product. To use it, you need:
- Two environments you can build the same way. Infrastructure as code or a container platform makes this realistic.
- A switch. A load balancer, a router, DNS or your hosting platform's traffic controls.
- Health checks that tell you whether green really works, beyond "the process started".
- A shared or compatible data layer. Usually both environments use the same database.
Many hosting platforms and container services have blue-green options built in. Check what yours offers before building your own.
Choose how traffic switches
The switch method decides how fast and clean the cut-over is:
| Switch | Speed | Watch out for |
|---|---|---|
| Load balancer target change | Seconds | Open connections need draining |
| Platform traffic controls | Seconds | Platform-specific limits |
| DNS record change | Minutes or more | Caches keep old answers until TTL expires |
DNS is the easiest to understand but the slowest to take effect, because resolvers cache answers. For a fast rollback, a load balancer or platform switch is better.
Whichever you use, drain connections: let in-flight requests on blue finish before you stop it, so customers mid-checkout aren't cut off.
The database is the real challenge
Fowler flags this directly: databases are often the challenge with blue-green, especially when a new version needs a schema change. Both environments usually talk to the same database, so the schema has to work for blue and green at the same time.
The common solution is to separate schema changes from code releases:
- Expand. Add the new column or table in a way the old code ignores.
- Release green. The new code writes to both old and new structures, or reads the new one with a fallback.
- Switch traffic and wait. Keep blue ready for rollback.
- Contract. Once you're sure you won't roll back, remove the old column in a later release.
If a release includes a change that blue can't live with, such as renaming a column the old code reads, you've lost your instant rollback. Catch that in code review.
Don't forget background work
Web traffic is easy to switch. Other things are not:
- Scheduled jobs. Make sure only one environment runs them, or you'll send every reminder email twice.
- Queue workers. Decide whether green's workers start before or after the switch, and make sure jobs created by blue can be processed by green.
- Webhooks and callbacks. Third parties call a fixed URL. It should point at the switch, not at one environment.
- Caches and sessions. Store sessions somewhere shared, so customers aren't logged out at the switch.
What to check before switching
Run a short, written checklist on green every time:
- Health endpoint returns healthy for every instance.
- A smoke test runs key journeys: home page, sign-in, add to basket, a test payment in sandbox mode.
- Error tracking shows nothing new.
- Database migrations for this release have run and blue still works against them.
Then switch, watch error rates and response times for a set period, and keep blue running until you're confident. Only then reuse blue for the next release.
Is it worth the cost?
Running two production environments can double your infrastructure bill while both are up. Many teams reduce that by keeping the idle environment small or scaled down, or by creating green only for the release and removing blue afterwards. Work out what a bad release currently costs you in downtime and stress, and compare that with the extra hours of hosting. Check current prices with your provider.
If you release rarely and can tolerate a few minutes of downtime, a well-practised rolling deploy with a tested rollback may be enough.
A worked example
Take a hypothetical Brisbane ticketing app that deployed by overwriting code on two servers. A bad release on a busy Friday took twenty minutes to roll back. The team moved to two container environments behind one load balancer, with migrations split into expand and contract steps and the scheduled-job runner pinned to whichever environment was live. Their next faulty release was reversed by switching the load balancer target back, and they debugged the broken version on the idle environment afterwards.
FAQ
Is blue-green the same as a canary release?
No. Blue-green switches all traffic at once. A canary sends a small share of traffic to the new version first and increases it gradually.
Can I do blue-green with a single server?
You can run two copies of the app on one server behind a local proxy, but you don't get protection against the server itself failing.
How long should I keep the old environment?
Until you're confident the release is good and no rollback-blocking database change is pending. For many teams that's hours, not days.
Does blue-green remove the need for testing?
No. It makes rollback fast, but customers still see a bad release until you switch back.
If you want releases you can undo in seconds, tell us how you deploy today.