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 swap | stays with the slot |
|---|---|
| the deployed content | publishing endpoints, the slot's hostname |
| app settings and connection strings not marked as slot settings | app settings and connection strings marked slot setting |
| general settings: stack and version, 32/64-bit, web sockets, HTTP version | custom domains, TLS/SSL bindings and certificates |
| handler mappings, public certificates | scale settings, Always On, access restrictions |
| WebJobs content | WebJobs 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:
- applies the target's (production's) slot settings to the source slot's instances - which restarts them;
- warms them up - waits until they answer (optionally on custom warm-up paths:
WEBSITE_SWAP_WARMUP_PING_PATH, or the app'sapplicationInitialization); - 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."