Architectural · Deployment · DWG NO. 056

Blue-Green Deployment

Run two identical production environments and switch traffic between them all at once, for releases with zero downtime and instant rollback.

TypeDeployment
ScopeWhole environment
ComplexityMedium
Common inZero-downtime releases, environments where instant rollback matters

Deploying a new version in place risks downtime and makes rollback slow

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.

Two full environments, one router deciding which is live

"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.

Router(all traffic)live nowidle, readyBluev1 — liveGreenv2 — standing by

A router switching all traffic to the new environment at once

`switchTraffic` is the entire release — an instant cutover, and just as instant to reverse by switching back.

blue-green-router.js
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");

Trade-offs