Skip to content
Tutorials Center
My learning

Topics

Lesson 40 of 57 ยท Infrastructure as Code and DevOps

Deployment Slots, Blue-Green and Canary

Learn safe release strategies on Azure: App Service slots and swap, blue-green, canary rollouts, rollback, feature flags and database migrations, with simulators.

A deployment pipeline (see CI/CD with GitHub Actions and Azure DevOps) can put new code on a server in minutes. The harder question is how to release it without users noticing a thing, and how to undo it in seconds when something goes wrong. Copying files over a running site and hoping for the best is a strategy too, but it is the one with the most downtime and the slowest recovery.

This lesson covers three release patterns (slots with swap, blue-green and canary), then the two things that make any of them hard: database changes and rollback. The Azure commands are read-only because they need a subscription. The runnable examples are JavaScript stand-ins that mimic the logic: a slot swap with a warm-up check, and a canary rollout that rolls back by itself.

Deploy is not release

Two words are worth separating:

  • Deploy means getting a new version running somewhere.
  • Release means sending real users to it.

All safe strategies work by splitting these two steps. You deploy first, check that the new version is healthy while nobody depends on it, and only then release. If the check fails, you never release, and users never knew.

App Service deployment slots

On App Service (see App Service and Web Apps), a deployment slot is a second, fully running copy of your app with its own host name, for example myapp-staging.azurewebsites.net. Slots are available on Standard tier plans and above. They share the same App Service plan, so they use the same VM capacity.

The workflow looks like this:

  1. Deploy the new build to the staging slot. Production is untouched.
  2. Test staging at its own URL.
  3. Swap the slots. Azure warms up the staging instances, then switches the routing so staging becomes production and production becomes staging.

Because the new code is already running and warm when the swap happens, users see no cold start. Because the old version is now sitting in the staging slot, rollback is just another swap.

Terminal
# Create a slot and deploy to it (read-only example)
az webapp deployment slot create \
  --resource-group rg-demo --name myapp --slot staging

az webapp deploy \
  --resource-group rg-demo --name myapp --slot staging \
  --src-path app.zip --type zip

# Swap staging into production
az webapp deployment slot swap \
  --resource-group rg-demo --name myapp \
  --slot staging --target-slot production

A few details matter in practice:

  • Slot-specific settings. Some settings should stay with the slot instead of travelling with the code, such as a staging database connection string. Mark them as "deployment slot setting" (sticky). Everything else moves with the swap.
  • Swap with preview. A multi-phase swap applies the production settings to the staging slot first, so you can test the new code with real production configuration before routing traffic.
  • Warm-up. You can point Azure at a path to hit before it switches, so caches and connection pools are ready.
  • Auto swap. A slot can swap itself into production after each deployment, which is convenient but removes your manual check.

You can also send a percentage of production traffic to a slot (az webapp traffic-routing set), which is a simple form of canary release on App Service.

The first stand-in models a swap. It is not App Service; it shows the idea that a swap exchanges roles after a warm-up check, and that a broken build is stopped before it reaches users.

JS A slot swap with a warm-up check

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

No output.

Notice the rollback: it is the same function as the release. That symmetry is why swaps are loved.

Blue-green deployment

Blue-green is the general pattern behind slots. You keep two complete environments. "Blue" is live. You build "green" next to it with the new version, test it, then move all traffic from blue to green in one step by changing a router (a load balancer rule, a DNS record or a slot swap). Blue stays alive for a while, ready to take the traffic back.

flowchart LR
    U["Users"] --> R["Router<br/>(Front Door or Traffic Manager)"]
    R -->|"100% before the switch"| B["Blue<br/>version 1"]
    R -.->|"100% after the switch"| G["Green<br/>version 2"]
    B --- D[("Shared database")]
    G --- D

Both environments exist at once; the router decides which one gets traffic.

Blue-green gives you instant switching and instant rollback. Its costs are real:

  • You pay for two environments while both run.
  • The switch is all-or-nothing, so a bug that only shows under real load hits everyone at once.
  • Both versions usually share the same database, which leads to the migration problem below.

With Azure you can implement the router with Azure Front Door or Application Gateway (see Load Balancer, Application Gateway and Front Door), with Traffic Manager (see DNS and Traffic Manager), or with slots. DNS-based switching is the slowest because resolvers cache answers for the record's TTL.

Canary releases

A canary release sends a small share of real traffic to the new version first. If metrics stay healthy, you raise the share in steps (for example 5, 25, 50 and 100 percent). If they get worse, you send everything back to the old version. The name comes from the canary in a coal mine: a small, early warning.

Canaries limit the blast radius. A bug that breaks one request in ten only hurts the 5 percent of users who reached the canary. They need more machinery than blue-green: you must route by percentage, and you must have good metrics to compare (error rate, latency, failed logins, business numbers). Without those, a canary is just a slow deployment. See Azure Monitor, Log Analytics and Application Insights for the tools that supply them.

Where Azure helps:

  • App Service slots with traffic routing percentages.
  • Container Apps revisions with traffic weights (see Azure Container Apps).
  • AKS with a service mesh or an ingress controller that supports weights (see Azure Kubernetes Service).
  • Front Door weighted origins.

The stand-in below runs a canary with fixed stages. At each stage it routes a share of 2,000 simulated requests to the new version, measures the canary's error rate and compares it with a limit of baseline plus 2 percentage points. The random numbers use a fixed seed, so the output is the same on every run. Two releases are tried: a healthy one and a buggy one.

JS Canary rollout with automatic rollback

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

No output.

Look at the 5 percent stage: only about 90 requests reach the canary, so the measured rate (2.2 percent) is noisy even though the true rate is 1 percent. Small samples need wider limits or longer observation windows. Real systems use statistical tests and wait for enough traffic before judging. Try changing 0.09 to 0.03: a true rate exactly at the limit is rolled back at the first stage because the small sample happened to read 4.3 percent. The decision depends on the limit you chose and on luck.

Rollback and feature flags

Every strategy needs an answer to "how do we undo this?" Prepare it before the release, not during an incident:

  • Keep the previous version deployable. Keep old images in the registry (see Azure Container Registry) and old slot contents until the new version has proven itself.
  • Automate the decision where you can, as the canary stand-in does, with alerts and health checks that trigger the rollback.
  • Rehearse it. A rollback you have never run is a guess.

Feature flags separate deploy from release even further. The new code ships switched off, hidden behind a flag, and you turn it on for staff, then 1 percent of users, then everyone. Turning it off again takes seconds and needs no deployment. Azure App Configuration has a feature management service for this. The cost is flag debt: old flags must be removed, or the code fills with dead branches.

The database is the hard part

Slots, blue-green and canaries all have a moment when two versions run at once against one database. If version 2 renames a column, version 1 breaks immediately, and rolling back does not bring the old column back. The standard solution is the expand and contract pattern (also called parallel change):

  1. Expand. Add the new column or table. Keep the old one. Both versions work.
  2. Migrate. Deploy code that writes to both and reads the new one. Backfill old rows.
  3. Contract. Once no running version uses the old column, remove it in a later release.
SQL
-- Release 1 (expand): add, never rename or drop
ALTER TABLE customers ADD full_name NVARCHAR(200) NULL;

-- Release 2: code writes both columns, reads full_name; backfill old rows
UPDATE customers SET full_name = first_name + ' ' + last_name WHERE full_name IS NULL;

-- Release 3 (contract): only after every version uses full_name
ALTER TABLE customers DROP COLUMN first_name, last_name;

SQL Server syntax, shown read-only. The principle is the same in every database.

This is slower than one big change, but it turns a risky all-or-nothing event into three small, reversible ones. The rule of thumb: every database change must work with both the old and the new code.

Choosing a strategy

Situation Good fit
Simple web app on App Service, wants zero-downtime releases Slots with swap
Need an instant, complete switch and instant undo Blue-green
Large user base, risky change, good metrics Canary
Want to release a feature separately from the deploy Feature flags
Schema change Expand and contract, whichever strategy you use

You can combine them: deploy to a slot, canary a small percentage, then swap.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why is rollback easy after an App Service slot swap?

    A swap exchanges the roles of the two slots. The old version sits in the other slot, still warm, so the same swap operation goes back.
  2. 2. What is the main trade-off of blue-green compared with a canary release?

    Blue-green gives an instant switch and instant undo, but traffic moves in one step. A canary exposes only a small share of users first.
  3. 3. A new version renames a database column. Which approach is safest during a canary or blue-green release?

    Two versions run against one database at the same time, so every schema change must work with both the old and the new code. Expand and contract keeps that true.
  4. 4. What does a feature flag let you do?

    Feature flags separate deploying code from releasing a feature. Turning a flag off is a configuration change, which takes seconds.
  5. 5. In the canary stand-in, why can the measured error rate at 5 percent traffic be misleading?

    With few requests, one or two errors change the percentage a lot. Real canaries wait for enough traffic or use statistical tests.

Summary

  • Separate deploy (code is running) from release (users reach it); all safe strategies rely on that split.
  • App Service slots let you deploy, test and swap with a warm-up; rollback is another swap. Mark settings that must not travel as slot settings.
  • Blue-green switches all traffic between two full environments; canary moves traffic in stages and judges metrics at each step.
  • Automate rollback decisions, keep the previous version available, and rehearse the undo.
  • Feature flags release features independently of deployments.
  • Use expand and contract for database changes so old and new code both work at every step.

Next: Key Vault.