How to Implement Blue-Green Deployment
Deploy with blue-green: run two identical environments, switch the traffic, and keep the old for a fast rollback.
On this page
Introduction
A deploy that replaces the running version in place risks a failed version serving traffic with no fast way back. Blue-green deployment removes that risk: two identical environments, blue and green, each able to serve the whole traffic. The new version is deployed to the idle one, verified, and the traffic is switched to it at the router. If the new version misbehaves, the traffic switches back to the old, which is still running.
This tutorial covers running two environments, deploying to the idle and switching, keeping the old for a fast rollback, and handling the state and the sessions. The examples use a traffic switch, and the pattern applies to any deployment with two environments.
Run two identical environments
Blue-green needs two environments, each a complete copy of the service, able to serve the whole traffic. One is live and serves the traffic; the other is idle and ready for the next deploy. They should be as identical as possible, so the verification of the idle one is meaningful: the same instance types, the same configuration, the same dependencies.
The idle environment is where the new version is deployed and verified without affecting the live users. Verify it with the smoke tests and the readiness checks before the switch, so the switch only happens when the new environment is proven to serve. The two environments both have the capacity to serve the whole traffic, so the switch does not overload the new one.
# two environments, one live and one idle
blue (live, serving all traffic)
green (idle, ready for the next deploy)
# the router points the clients at the live one, and the switch changes the pointer
Deploy to the idle and switch the traffic
Deploy the new version to the idle environment. Run the migrations that are compatible with both versions, then the smoke tests and the readiness checks on the idle. When it is verified, switch the traffic at the router, so the clients now reach the new environment. The switch is a single routing change, which is fast and atomic.
The switch should be a change at the router, not a change to the clients, so the clients are unaffected and the switch is instant. A load balancer or a DNS record or a gateway config points at the live environment, and the switch changes that pointer. The new environment’s readiness gates the switch, so a not-ready environment is not switched to.
# deploy to the idle, verify, then switch the router to it
deploy green --version v2
run-smoke-tests green
switch-traffic --to green # the router now points at green
# blue is still running for the rollback
Keep the old environment for a fast rollback
After the switch, keep the previous environment running for a period, so a problem is handled by switching the traffic back rather than a redeploy. The rollback is a single routing change, which is fast and does not require a build or a deploy. This is the property that makes blue-green attractive: the rollback is as fast as the switch.
Keep the old environment warm, not idle, so it can serve the traffic immediately if the switch back happens. Rolling it down too soon removes the rollback; keeping it too long costs the double capacity. The window is a trade: long enough to catch a problem the verification missed, short enough to limit the cost. The old environment is also the target of the next deploy, which alternates the roles.
Handle the shared state and the sessions
If the two environments share a database, the schema must be compatible with both versions during the switch, so both can serve. Use expand-and-contract migrations: add the new columns in a release that both versions tolerate, deploy the code that uses them, then remove the old columns in a later release. This keeps both versions working across the switch and avoids a schema mismatch.
Sessions need handling: a session created on the old environment must survive on the new. Store the sessions in a shared store, so any environment can read them, or make the switch tolerate the loss by using a stateless token. Drain the old environment’s in-flight requests before rolling it down, so the switch does not cut off a request that is mid-flight.
-- expand: add the new column, both versions tolerate it
ALTER TABLE users ADD COLUMN locale text; -- nullable, old code ignores it
-- contract (a later release): remove the old column once both versions use the new
-- ALTER TABLE users DROP COLUMN language;
Test the switch and the rollback
Test that the traffic switches to the idle environment when it is ready and stays on the old when it is not, by making the idle not ready and asserting the switch does not happen. Test that a rollback switches the traffic back and the old environment serves, within the rollback window. Test that the in-flight requests on the old environment complete during the drain, so the switch does not cut them off.
# switch, then roll back, and assert the old serves again
switch-traffic --to green
assert-serves green v2
switch-traffic --to blue # the rollback
assert-serves blue v1 # the old environment is still serving
Common pitfalls
In-place deploy with no second environment so there is no fast rollback, the two environments not identical so the verification is meaningless, an incompatible schema so one version breaks on the switch, sessions not shared so the switch loses them, and rolling down the old environment too soon so the rollback is removed are the recurring mistakes. Two identical environments, a router switch, an expand-and-contract migration, shared sessions, and a rollback window address them.
Conclusion
Blue-green deploys by switching the traffic between two environments. Run two identical environments, deploy to the idle and switch at the router, keep the old for a fast rollback, and handle the shared state and the sessions with an expand-and-contract migration. Self-check by switching to a misbehaving environment and rolling back within the window. Then a deploy is a switch, and a rollback is a switch back.
Sources: Martin Fowler: BlueGreenDeployment, Microsoft: Blue-green deployment, AWS: Blue/green deployments.
FAQ
- How is blue-green different from canary?
- Blue-green switches all the traffic at once between two environments, so the rollback is a single switch. A canary sends a small fraction to the new version and grows it, so a problem affects few users. Blue-green is a fast, clean switch; a canary is a gradual, low-blast-radius one.
- What about the database?
- The two environments usually share the database, so the schema must be compatible with both versions during the switch. Use expand-and-contract migrations: add the new columns, deploy the code that uses them, then remove the old columns, so both versions work across the switch.
Report a problem with this articleOpens a pre-filled issue on GitHub.