OnCallReadyStart learning

Azure Lesson 33.21 · Azure V: Compute, App Hosting & the AZ-104 exam · 19 min read

Deployment slots: release by swapping, roll back by swapping again

In plain words

A deployment slot is a second stage behind the curtain. You set up the new show on the back stage (staging), check it, then swap the stages: the audience now sees the new show, and the old one is behind the curtain, ready to come back if something goes wrong.

Some props must stay on their own stage - the production database address stays with production. You mark those as slot settings.

Deployment slots: release by swapping, roll back by swapping again

A deployment that overwrites the running app is a small outage every time: the app restarts, the first requests wait for it to warm up, and if the new version is broken you redeploy the old one while users watch. Deployment slots fix that. A slot is a second live copy of the app - its own hostname, its own settings - on the same App Service plan. You deploy to staging, test it at https://<app>-staging.azurewebsites.net, then swap: Azure warms the staging instances up and switches the routing, so production gets the new version with no restart and no cold start. If it is wrong, swap again. The catch, which this lesson and an incident later in this chapter are about: some settings travel with the swap and some stay with the slot, and choosing wrong ships the wrong configuration to production.

Need to know: slots need Standard (5 slots) or Premium (20); they share the plan's instances. A swap moves the content and every app setting and connection string that is not marked as a deployment slot setting; settings marked as slot settings ("sticky") stay with the slot. Slot-specific configuration never moves: custom domains, TLS bindings, scale, access restrictions, Always On, diagnostics, VNet integration, managed identity. Swap with preview first applies the target's sticky settings to the source so you can check it, then completes or resets. Traffic routing sends a percentage of production traffic to a slot (testing in production).

A staging slot

$ cd ~/oncall-lab/labs/4b-azure/az5/slots
$ curl -s https://shop-slots-5.azurewebsites.net/
<h1>shop v1 - env=prod db=db-prod.internal</h1>
$ az webapp config appsettings list -g rg-az5-slots -n shop-slots-5 -o table
Name     SlotSetting  Value
-------  -----------  ----------------
APP_ENV  False        prod
DB_HOST  False        db-prod.internal
$ az webapp deployment slot create -g rg-az5-slots -n shop-slots-5 --slot staging --configuration-source shop-slots-5 -o none
$ az webapp deployment slot list -g rg-az5-slots -n shop-slots-5 -o table
Name     Location    State    ResourceGroup  DefaultHostName
-------  ----------  -------  -------------  --------------------------------------
staging  westeurope  Running  rg-az5-slots   shop-slots-5-staging.azurewebsites.net

--configuration-source copies production's app settings into the new slot (otherwise it starts empty). The page shows two of the app's settings, APP_ENV and DB_HOST - the lab's stand-in for "which database does this copy talk to". Copied as they are, staging would point at the production database:

$ az webapp config appsettings list -g rg-az5-slots -n shop-slots-5 --slot staging -o table
Name     SlotSetting  Value
-------  -----------  ----------------
APP_ENV  False        prod
DB_HOST  False        db-prod.internal

Which settings move

Settings that describe where the copy runs must stay with the slot: the database, the environment name, keys for the staging instance of a payment provider. Mark them as slot settings - in both slots, with each slot's own value:

$ az webapp config appsettings set -g rg-az5-slots -n shop-slots-5 --slot staging --slot-settings APP_ENV=staging DB_HOST=db-staging.internal -o none
App settings have been redacted. Use `az webapp/logicapp/functionapp config appsettings list` to view.
$ az webapp config appsettings set -g rg-az5-slots -n shop-slots-5 --slot-settings APP_ENV=prod DB_HOST=db-prod.internal -o none
App settings have been redacted. Use `az webapp/logicapp/functionapp config appsettings list` to view.
$ az webapp config appsettings list -g rg-az5-slots -n shop-slots-5 -o table
Name     SlotSetting  Value
-------  -----------  ----------------
APP_ENV  True         prod
DB_HOST  True         db-prod.internal

SlotSetting: True = sticky. Settings that describe the version of the code - a feature flag the new release needs, a new config key - must move with the content: leave them unmarked.

moves with the swapstays with the slot
the deployed contentpublishing endpoints, the slot's hostname
app settings and connection strings not marked as slot settingsapp settings and connection strings marked slot setting
general settings: stack and version, 32/64-bit, web sockets, HTTP versioncustom domains, TLS/SSL bindings and certificates
handler mappings, public certificatesscale settings, Always On, access restrictions
WebJobs contentWebJobs schedulers, diagnostic settings, CORS, VNet integration, managed identity

Connection strings (az webapp config connection-string set ... --slot-settings NAME=...) follow the same rule as app settings.

Deploy to staging, check, swap

$ az webapp deploy -g rg-az5-slots -n shop-slots-5 --slot staging --src-path v2.html --type static -o none 2>/dev/null
$ curl -s https://shop-slots-5-staging.azurewebsites.net/
<h1>shop v2 - env=staging db=db-staging.internal</h1>
$ curl -s https://shop-slots-5.azurewebsites.net/
<h1>shop v1 - env=prod db=db-prod.internal</h1>

v2 runs in staging against the staging database; production is untouched. When you swap, Azure:

  1. applies the target's (production's) slot settings to the source slot's instances - which restarts them;
  2. warms them up - waits until they answer (optionally on custom warm-up paths: WEBSITE_SWAP_WARMUP_PING_PATH, or the app's applicationInitialization);
  3. switches the routing: production's hostname now points at the warmed-up instances, and the old production instances become the staging slot.

Step 1 is the dangerous one: v2 meets the production settings for the first time during the swap. Swap with preview stops after step 2, so you can test v2 with production's configuration on the staging URL before any user sees it:

$ az webapp deployment slot swap -g rg-az5-slots -n shop-slots-5 --slot staging --action preview
$ curl -s https://shop-slots-5-staging.azurewebsites.net/
<h1>shop v2 - env=prod db=db-prod.internal</h1>
$ az webapp deployment slot swap -g rg-az5-slots -n shop-slots-5 --slot staging --action swap
$ curl -s https://shop-slots-5.azurewebsites.net/
<h1>shop v2 - env=prod db=db-prod.internal</h1>
$ curl -s https://shop-slots-5-staging.azurewebsites.net/
<h1>shop v1 - env=staging db=db-staging.internal</h1>

During the preview, staging showed v2 with env=prod db=db-prod.internal: the sticky production settings applied. --action reset would have put staging's own settings back and cancelled. After the swap, production runs v2 on its own database, and staging holds v1 with the staging settings - which is your rollback: swap again and v1 is back in production within a minute, warmed up.

Auto swap (az webapp deployment slot auto-swap --slot staging) swaps automatically after every successful deployment to the slot - convenient for a pre-production app, and a way to skip your own checks in production. Swaps are logged in the activity log (Microsoft.Web/sites/slots/slotsswap/action).

Testing in production: a share of the traffic

Instead of all or nothing, route a percentage of production's visitors to a slot:

$ az webapp traffic-routing set -g rg-az5-slots -n shop-slots-5 --distribution staging=25 -o table
ActionHostName                          Name     ReroutePercentage
--------------------------------------  -------  -----------------
shop-slots-5-staging.azurewebsites.net  staging  25
$ for i in $(seq 20); do curl -s https://shop-slots-5.azurewebsites.net/; done | sort | uniq -c
      5 <h1>shop v1 - env=staging db=db-staging.internal</h1>
     15 <h1>shop v2 - env=prod db=db-prod.internal</h1>
$ az webapp traffic-routing clear -g rg-az5-slots -n shop-slots-5 -o none

A visitor routed to the slot gets a cookie, x-ms-routing-name=staging, and stays there; anyone can opt in with ?x-ms-routing-name=staging. Note what the share sees here: the old v1 still in staging, with staging's database - routing sends real users to the slot's own configuration, so only route to a slot that is safe for production traffic.

On the exam (AZ-104 3.4.8): "keep each slot's connection string" = mark it as a deployment slot setting; "validate with production settings before the swap" = swap with preview; "send 10% of users to the new version" = traffic routing (Traffic %); "roll back" = swap again; slots need Standard or above; the custom domain and TLS stay with production.

In an interview: "How do you deploy a web app with zero downtime on App Service?" - "Deploy to a staging slot, run smoke tests against it, swap with preview so it warms up with the production settings, complete the swap. Environment-specific settings are slot settings so they never move. Rollback is a second swap."

Why it helps

Slots are how App Service releases without downtime, and the swap is where releases go wrong: a setting that should have stayed with production moves with the code. AZ-104 asks which settings swap and how to keep a connection string per slot.

This chapter's incident is exactly that, so the lesson spends time on the table of what moves and what stays, and on swap with preview and traffic routing for safer releases.

Commands in this lesson

cd curl az

FAQ

Do slots cost extra?

No, slots run on the same plan instances as the production app, so they cost nothing extra - but they use the same CPU and memory. Standard gives 5 slots, Premium v3 20. Basic and Free have none.

What exactly happens during a swap?

Azure applies the target slot's settings to the source slot's instances and restarts them, waits for them to warm up (the root path or the applicationInitialization paths), then switches routing. If warm-up fails, the swap stops and production is untouched.

What is swap with preview?

A two-phase swap: phase one applies production's settings to staging and stops, so you can test staging with the production configuration. Then you complete the swap or reset it. az webapp deployment slot swap --action preview, then --action swap or reset.

How do I send a share of real traffic to staging?

az webapp traffic-routing set --distribution staging=10 sends 10% of production's requests to the staging slot; a cookie keeps each user on the same slot. Clear it after testing. It is a canary without a second app.

How do I roll back?

Swap again. After a swap, the staging slot holds the previous production content, so a second swap puts it back - with its settings, if those were marked correctly. Keep the staging slot untouched until you are sure the release is good, or the rollback target is gone.

In an interview Mid

How do you deploy a web app with zero downtime on App Service?

Deploy to a staging slot, run smoke tests against it, swap with preview so it warms up with the production settings, then complete the swap. Environment-specific settings are slot settings (--slot-settings) so they never move. Rollback is a second swap, because staging now holds the previous release.

Also asked: Which settings stay with a slot during a swap? · How would you test a new release with 10 percent of real users on App Service? · What do you do if a swap moved the wrong connection string to production?

Practise this lesson in the terminal Free, in your browser - a real Ubuntu terminal to try it in, with missions that check your work.