TExcelTAD
Home › CI/CD Pipelines & Pipeline Security › Blue/Green Deployments

Blue/Green Deployments

A blue/green deployment runs two complete production environments. One (say blue) serves all traffic; the new version is deployed to the idle one (green), tested there with production configuration, and then traffic is switched in one step. Rolling back is switching back.

           before                       switch                       after
users ──► router ──► blue  v1     users ──► router ──► green v2    (blue v1 kept idle for rollback)
                     green v2 (testing)
Rolling Blue/green
Versions serving at once both, during the rollout one (the switch is atomic)
Extra capacity maxSurge pods a full second environment during the release
Rollback another rollout (minutes) flip the router back (seconds)
Testing before users see it no yes: smoke tests against green before the switch
Database shared, must be compatible usually shared too, so the same compatibility rule applies

Where the switch happens

Layer How you switch
DNS change a record's target; slow and uneven because of caching (TTL), rarely used alone
Load balancer move the listener or target group (AWS ALB target groups, an Nginx upstream, a HAProxy backend)
Kubernetes Service change the Service's selector from version: blue to version: green
Gateway API / service mesh change the backendRefs weights from 100/0 to 0/100
Platform feature AWS CodeDeploy blue/green, Azure App Service deployment slots, Argo Rollouts blueGreen strategy

In Kubernetes, the simplest form is two Deployments and one Service:

yaml
apiVersion: v1
kind: Service
metadata:
  name: shop
spec:
  selector:
    app: shop
    slot: blue           # the pipeline patches this to "green" to switch
  ports:
    - port: 80
      targetPort: 8080
bash
# the switch step of a pipeline (sample session: needs a cluster)
kubectl -n shop rollout status deployment/shop-green --timeout=5m
./ci/smoke-test.sh http://shop-green.shop.svc.cluster.local
kubectl -n shop patch service shop -p '{"spec":{"selector":{"app":"shop","slot":"green"}}}'
deployment "shop-green" successfully rolled out
smoke test: 12/12 checks passed
service/shop patched

Argo Rollouts automates exactly this (activeService, previewService, autoPromotionEnabled); it is covered in the GitOps course.

A simulation: switch and switch back

The model sends a fixed stream of requests through a router while a release happens. Green has a bug that fails 30 % of one endpoint's requests (seeded random, so the run is repeatable). The smoke test against green before the switch only covers some endpoints, so the bug gets through; the error rate after the switch triggers a switch back:

python · try it yourself
import random

rng = random.Random(42)
ENDPOINTS = ["/", "/cart", "/checkout", "/search"]

def handle(version, path):
    if version == "green" and path == "/checkout":
        return 500 if rng.random() < 0.30 else 200
    return 200

def smoke_test(version, paths):
    return all(handle(version, p) == 200 for p in paths)

live, idle = "blue", "green"
print("smoke test green on / and /cart:", "pass" if smoke_test(idle, ["/", "/cart"]) else "fail")
live, idle = idle, live
print(f"switch: live={live}")

window = []
for minute in range(1, 7):
    errors = sum(handle(live, rng.choice(ENDPOINTS)) == 500 for _ in range(200))
    window.append(errors / 200)
    print(f"  minute {minute}: live={live:<5} error rate {errors / 200:5.1%}")
    if live == "green" and len(window) >= 2 and min(window[-2:]) > 0.02:
        live, idle = idle, live
        window.clear()
        print(f"  -> error rate above 2% for 2 minutes: switch back, live={live}")
smoke test green on / and /cart: pass
switch: live=green
  minute 1: live=green error rate  7.0%
  minute 2: live=green error rate  9.5%
  -> error rate above 2% for 2 minutes: switch back, live=blue
  minute 3: live=blue  error rate  0.0%
  minute 4: live=blue  error rate  0.0%
  minute 5: live=blue  error rate  0.0%
  minute 6: live=blue  error rate  0.0%

The switch back took seconds and needed no redeploy, because blue was still running v1. That is blue/green's main selling point. Its weaknesses show in the same run: every user hit the bug for two minutes, because the switch is all-or-nothing. A canary would have exposed only a few percent of users (Canary Releases).

Practical points

  • Keep blue until you're sure. Tear down (or scale down) the old environment only after the new one has run a full traffic cycle; some teams keep it until the next release.
  • Warm up green. Caches, JIT compilers and connection pools are cold; send synthetic traffic before the switch.
  • Long-lived connections (WebSockets, gRPC streams) stay on blue after the switch until they reconnect; drain them deliberately.
  • Cost: two full environments during the release. On Kubernetes, green can be scaled from zero, which reduces the cost to the duration of the release.
  • Databases are shared by both colours, so schema changes must work with both versions (Rollback & Database Changes).

Test yourself

What makes blue/green rollback fast?

  1. Images are cached
  2. The previous version is still running, so rollback is just switching the router back
  3. Kubernetes rolls back automatically
  4. DNS updates instantly

What is blue/green's main weakness compared with a canary?

  1. It needs more tests
  2. The switch is all-or-nothing, so a bug the smoke tests missed hits every user at once
  3. It can't roll back
  4. It requires DNS

Why is switching only by DNS rarely enough?

  1. DNS can't point to two servers
  2. Clients and resolvers cache records for the TTL, so traffic moves slowly and unevenly
  3. DNS is insecure
  4. It's too fast