Skip to content
Tutorials Center
My learning

Topics

Lesson 29 of 44 ยท Releases and Deployment

Deployment Strategies

Compare recreate, rolling, blue-green and canary deployments, feature flags, rollbacks, expand/contract database migrations and smoke tests in CI/CD.

Passing tests tell you a change is probably fine. They cannot tell you how it behaves with real traffic, real data and real users. Deploying is the moment of truth, and a deployment strategy is the plan for how a new version replaces the old one: how much downtime you accept, how much risk you take, and how quickly you can go back.

This lesson walks through the main strategies (recreate, rolling, blue-green and canary), then three tools that make any of them safer: feature flags, rollbacks and smoke tests. It also covers the hardest part, the database, with the expand/contract pattern. The concrete deployment steps for servers and clouds follow in Deploying to Servers, Containers and Static Hosting.

The configuration snippets are read-only: no cluster or load balancer exists on this site. The TryIt blocks are JavaScript simulations of a canary rollout and a rolling update, using a fixed pseudo-random generator so output is repeatable.

Recreate

The simplest strategy: stop the old version, start the new one.

flowchart LR
    A["v1 running"] --> B["Stop v1<br/>(downtime)"] --> C["Start v2"]

Recreate has a gap between stopping the old version and the new one becoming ready.

It has downtime, but it is easy to reason about: there is never a moment when two versions run side by side. That makes it a fine choice for internal tools, batch workers, or schema changes that cannot tolerate two versions at once. For anything users depend on around the clock, you want something smoother.

Rolling update

A rolling update replaces instances a few at a time. Kubernetes does this by default for a Deployment:

YAML
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 4
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1          # may run one extra pod during the update
      maxUnavailable: 0    # never drop below 4 ready pods
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: web
          image: ghcr.io/acme/web:2.5.0
          readinessProbe:
            httpGet:
              path: /healthz
              port: 8080

With maxSurge: 1 and maxUnavailable: 0, the cluster starts one new pod, waits for its readiness probe to pass, then stops one old pod, and repeats. Capacity never dips, and the extra cost is a single pod. The readiness probe is the key: without it, traffic is sent to pods that have started but are not ready.

During a rolling update, both versions serve traffic at the same time, so v2 must work with data and APIs written by v1, and the reverse. Keep that in mind; it is the root of the database section below.

JS Simulate a rolling update

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The simulation is simplified (real controllers also handle failures, pausing and several revisions), but you can see the pattern: new pods appear, become ready, then old ones go away. Try maxSurge: 2 to speed up the rollout at the cost of more temporary capacity.

Blue-green

In blue-green, you run two complete environments. Blue is live; green is idle. You deploy the new version to green, test it with no user traffic, then flip the router (load balancer, DNS or Kubernetes Service selector) from blue to green.

flowchart LR
    U["Users"] --> LB["Router"]
    LB -->|"live"| B["Blue: v1"]
    LB -.->|"after the switch"| G["Green: v2"]

The router decides which environment is live; switching back is one change.

The big advantages: you can smoke-test the real new environment before anyone uses it, and rollback is instant (point the router back at blue, which is still running). The costs are double the infrastructure during the switch, and the need to keep the database compatible with both versions. In Kubernetes you can implement it with two Deployments and a Service whose selector you change:

YAML
apiVersion: v1
kind: Service
metadata:
  name: web
spec:
  selector:
    app: web
    track: green      # change from "blue" to switch traffic
  ports:
    - port: 80
      targetPort: 8080

Canary

A canary release sends the new version a small slice of traffic first (say 5%), watches metrics, and increases the share only if it stays healthy. The name comes from the canary in the coal mine: a small group meets the danger first. A bug then affects 5% of users for a few minutes instead of everyone.

The automation compares the canary's error rate and latency with the stable version's. If it crosses a threshold, the rollout stops and traffic returns to stable, with no human involved. Tools that implement this include Argo Rollouts, Flagger and cloud load balancers with weighted routing.

JS Canary traffic shifting with automatic rollback

Press Run (or Ctrl + Enter) to see the console output here.

No output.

In the buggy run the problem is caught at the very first step, when only 5% of requests reach it. Notice the small number of canary requests at 5%: with too little traffic, a statistical fluke can look like a failure or hide one. Real tools wait for a minimum sample size or time window before judging.

Feature flags

A feature flag (or toggle) separates deploying code from releasing a feature. The new code ships turned off. You enable it later for staff only, then 1% of users, then everyone, without another deployment:

JavaScript
if (flags.isEnabled("new-checkout", { userId })) {
  return renderNewCheckout();
}
return renderOldCheckout();

Flags make rollback instant (flip the flag off), allow testing in production safely, and help trunk-based development because unfinished work can merge to main while hidden. The costs are extra code paths to test and flag debt: remove flags once a feature is fully released, or the code fills with dead branches.

Rollbacks

Plan the way back before you go forward. Common mechanisms:

  • Redeploy the previous artifact. Because you tagged images by version or commit SHA, you can deploy the old one again. In Kubernetes: kubectl rollout undo deployment/web.
  • Switch the router back (blue-green) or set canary weight to 0.
  • Turn off the flag.
  • Roll forward: ship a quick fix. Sometimes faster than rolling back, especially when a migration has already run.

A rollback only works if old code can still run against the current data, which brings us to the database.

Database migrations: expand and contract

Application code and database schema do not change in the same instant. During a rolling, blue-green or canary release, old and new versions share one database. A migration that renames or drops a column breaks the old version immediately.

The expand/contract (parallel change) pattern splits a risky change into compatible steps. Example: rename users.name to users.full_name.

  1. Expand. Add the new column full_name (nullable). Both versions still work, because nothing was removed.
  2. Deploy v2 that writes both columns and reads the new one (falling back to the old).
  3. Backfill existing rows into full_name.
  4. Deploy v3 that uses only full_name.
  5. Contract. Drop the old name column in a later release, once no running version needs it.

Each step is safely deployable and safely revertible. It takes more releases, but it never forces downtime or a risky all-at-once switch. Run migrations as a separate, earlier pipeline step rather than inside application startup, so a slow migration cannot take down every instance at once.

Smoke tests

A smoke test is a few quick checks run against the freshly deployed environment: does the health endpoint answer, can the home page load, can a test user log in? It is the pipeline's "did it catch fire?" check, and a failure triggers rollback.

YAML
- name: Smoke test
  run: |
    curl --fail --silent --show-error --retry 10 --retry-delay 3 --retry-all-errors \
      "$STAGING_URL/healthz"
    curl --fail --silent --show-error "$STAGING_URL/" | grep -q "<title>Acme</title>"
  env:
    STAGING_URL: ${{ vars.STAGING_URL }}

--fail makes curl exit non-zero on HTTP errors (so the step fails), and --retry-all-errors helps while the new version warms up. Keep smoke tests short and independent of test data; the deeper suite belongs earlier in the pipeline (Test Stages in a Pipeline).

Choosing a strategy

Strategy Downtime Extra cost Rollback speed Good for
Recreate yes none redeploy (slow) internal tools, breaking schema
Rolling none small redeploy (medium) most stateless services
Blue-green none double during switch instant critical, easily switched services
Canary none small instant (weight 0) high traffic, measurable metrics

Strategies combine: a canary rollout behind a feature flag, with expand/contract migrations and smoke tests at each step, is a mature setup.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which strategy keeps the old environment running so you can roll back by flipping a router?

    In blue-green the previous environment stays up until you are confident, so switching back is one change.
  2. 2. In a Kubernetes rolling update with maxSurge 1 and maxUnavailable 0, what is guaranteed?

    A new pod must become ready before an old one is stopped, so capacity does not dip.
  3. 3. Why is a canary release safer than switching all traffic at once?

    A small slice of traffic limits the blast radius, and automated metric checks can stop the rollout.
  4. 4. What is the first step to safely rename a database column with expand/contract?

    Expanding first keeps old and new application versions working. The old column is dropped only at the end.
  5. 5. What does a feature flag separate?

    Code can be deployed turned off and enabled later for chosen users, without another deployment.

Summary

  • Recreate is simple with downtime; rolling updates replace instances gradually; blue-green switches between two full environments; canary shifts traffic in steps with metric checks.
  • Readiness probes are what make rolling updates safe.
  • Feature flags decouple deploying from releasing, but must be cleaned up.
  • Always plan the rollback: previous artifact, router switch, flag off, or roll forward.
  • Use expand/contract so database changes work with both old and new application versions.
  • Finish every deployment with a quick smoke test that can trigger a rollback.

Now see how these ideas look on real targets in Deploying to Servers, Containers and Static Hosting.