In this post we are going to explore a feature provided by Azure App Service called “Testing in production” that allows you to direct a portion of live user traffic to one or more deployment slots of your web app before swapping this deployment slot to production. You can, thus, analyze your app, check for exceptions or errors, and make certain everything works properly, before your app changes hit the production site.
First of all, let’s say a few words about deployment slots.
What is a deployment slot?
Deployment slots are actually live web apps with their own hostnames, hosted on the same application server as your main app, which keeps the “production slot”.
Below you may find some key points about deployment slots, and in particular that they:
- are available in Standard and Premium pricing tier, offering 5 and 20 deployment slots respectively;
- can have the same configuration options as the web app on the production slot or have slight changes based on the purpose of each deployment slot;
- can swap web app content and configurations elements between two deployment slots, including the production slot;
- cannot scale, as the only slot that can be scaled is the production slot;
- allow the validation of web app changes in a staging deployment slot before swapping it with the production slot.
Add a beta deployment slot
Adding a deployment slot is a very simple process which can be done through a few clicks in Azure portal.
From your web app’s blade:
- search for Deployment slots under App Deployment menu,
- click on the plus sign at the top of the blade,
- enter the name beta or anything else you like,
- choose whether or not you want this slot to have the same settings as your production slot,
- and click OK.
Now you have two different web apps under these urls:
- `slot-demo.azurewebsites.net` and
- `slot-demo-beta.azurewebsites.net`
Specify different application settings for each slot
Each deployment slot may share the same application settings with the main production slot or have different ones.
In our example, we are going to have the same application setting key, but with different values:
- in the production slot, the app setting `SLOT_TITLE` will have the value `Production`
- in the beta deployment slot, it will have the value `Beta`
In production slot
In beta slot
Note: Checking Slot setting means that when you swap one deployment slot with another, that setting sticks to the slot.
Deploy an ASP.NET Core MVC web app
Let’s create a simple ASP.NET Core MVC web app using Visual Studio:
- click on New Project…,
- select ASP.NET Core Web Application,
- then Web Application and
- click OK.
Read Application Settings
In order to read Application Settings in ASP.NET Core, you need to follow the steps below:
Add IConfigurationRoot
in ConfigureServices
in Statup.cs
to be able to get access to AppSettings through any Controller.
public IConfigurationRoot Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddSingleton(Configuration); // Add framework services. services.AddMvc(); }
And change HomeController to read App Setting SlotTitle:
using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; namespace SlotsDemo.Controllers { public class HomeController : Controller { private readonly IConfigurationRoot _configuration; public HomeController(IConfigurationRoot configuration) { _configuration = configuration; } public IActionResult Index() { ViewData["SlotTitle"] = _configuration["SLOT_TITLE"]; return View(); } ///.... } }
In _Layout.cshtml add `ViewData[“SlotTitle”]` to better identify in which slot you are:
<a asp-area="" asp-controller="Home" asp-action="Index" class="navbar-brand">SlotsDemo @ViewData["SlotTitle"]</a>
Deploy to Azure
To deploy to Azure you can publish your project through Visual Studio using Web Deploy, once in main production slot and then in Beta deployment slot:
When your deployment finishes successfully you will have two web sites, one in `slot-demo.azurewebsites.net` and the other in `slot-demo-beta.azurewebsites.net`, with different titles only, just to easily recognize later in which slot you are.
Testing in Production
Let’s assume now that you want the web app hosted on Beta deployment slot to be swapped with the app on the production slot, but only after making certain that everything works properly.
To do so you can use a feature in Azure App Service called Traffic Routing, which allows you to direct a portion of live user traffic to the deployment slot you want to test, and after you have no errors or exceptions, you can simply swap the deployment slot to production.
Traffic routing allows you to control how traffic is distributed between your production and other slots. It is very useful when you want to try out a new change with a small percentage of requests and then gradually increase the percentage of requests that get the new behavior.
To direct a small amount of traffic to beta
deployment slot before deciding whether to swap it to production or not, you need to:
- click on Testing in Production under Development Tools section on the
slot-demo
web app, - from dropdown menu choose deployment slot, select beta and set traffic to 25%,
- automatically the traffic in production slot will switch to 75%, and
- click the save button at the top of the blade
Therefore, out of all the traffic hitting `slot-demo.azurewebsites.net`, 25% will be directed to the app hosted on the beta
deployment slot, and the other 75% will be directed to the app on the production slot. Subsequently, when you feel confident that everything works as expected, you can swap the beta slot with the production one.
You can find more info on traffic routing, here.
thank for good post
Hi There,
Great blog. Cleared lot of questions about webapps.
I am new to Azure and have a question about traffic routing.
If 25% of traffic (As in example above) is diverted to beta slot, does it not mean a data loss on production database which is connected to production slot?
May be I am missing a key point about deployment slots, but I hope to get it clarified with your answer.
Thanks in advance.
Regards
Praveen
If your beta slot points to a different db than your production slot then surely you ‘ll have issues with your data but usually, the web app in your beta slot points to the same database as your production web app, considering the fact that when you are trying to switch from beta to production, you have already applied the necessary changes to your production db.
It is common that when you develop something new, this works perfectly with a small number of users, but when releasing it to all your customers things get messy.
To avoid that, traffic routing helps you test something with many a percentage of your usual traffic and make you feel confident that when you finally release it everything will work as expeceted.