Run two identical production environments and switch traffic between them all at once, for releases with zero downtime and instant rollback.
Replacing the running version of a service in place means a window where it's unavailable or serving a half-updated mix of instances. If the new version has a serious problem, rolling back means another in-place deploy — under pressure, with the same risk the original deploy had.
"Blue" is the currently live environment; "green" is an idle, fully deployed copy running the new version. Once green is verified healthy, the router switches all traffic to it instantly. Blue stays running, idle, ready to receive traffic back immediately if a problem appears.
`switchTraffic` is the entire release — an instant cutover, and just as instant to reverse by switching back.
const environments = {
blue: { version: "v1", handle: () => "response from v1" },
green: { version: "v2", handle: () => "response from v2" },
};
let live = "blue";
function route(request) {
return environments[live].handle();
}
function switchTraffic(target) {
console.log(`router: verifying ${target} is healthy...`);
live = target; // instant cutover — no partial rollout, no in-place replace
console.log(`router: all traffic now routed to ${target} (${environments[target].version})`);
}
console.log(route()); // "response from v1"
switchTraffic("green");
console.log(route()); // "response from v2"
// Rollback, if green has a problem, is exactly as instant:
switchTraffic("blue");