With autoscaling turned on on an app service, Azure will add instances as needed based on set rules. I always start from a minimum of 2 instances. I'd like to make sure that traffic is not directed to the new app service instance until the application code has fully initialized. How can I do this? IS it possible to add a timeout? Or is it done automatically somehow?
2 Answers
You want to use Application Initialization in the web.config file for your app.
you'll need to add something like this:
<system.webServer>
<applicationInitialization >
<add initializationPage="/page-you-want-to-warm-up.php" hostName="your-app.azurewebsites.net"/>
</applicationInitialization>
<system.webServer>
Every time your application starts, this can be because of a new worker coming online (horizontal scaling) or even just a cold start caused by a new deployment, config change etc... the ApplicationInitialization will be executed to warm up the site before accepting requests on that worker.
You can read more about this here: http://ruslany.net/2015/09/how-to-warm-up-azure-web-app-during-deployment-slots-swap/
Even though that post is talking about using this for warming up a site when doing a swap operation it also applies to cold starts, and scale outs.

- 1,172
- 6
- 16
-
1There's an unanswered question on the site you linked: if you supply the host name like this, how can you be sure it warms up the correct instance in a multi-instance setup (e.g. autoscaling). – Josh Mouch Jan 17 '18 at 15:41
-
the app init logic is execute on the instance where the app is starting, in other words the moment the app gets assigned to a new machine, that instace knows it's got to load the app, that is part of the app assignment process when that happens app init logic gets executed, its all internal to the instance and does not get routed through dns where you would hit the "what instance should I warm up ?" problem – Byron Tardif Jan 19 '18 at 20:36
-
@ByronTardif so what happens if you miss out the hostName? – Rob Sedgwick Feb 06 '18 at 12:28
-
I think host-name is a required property – Byron Tardif Feb 08 '18 at 00:43
-
2@ByronTardif i don't think that this works... at least not as I expected it to because after swapping, if I hit any page, i get a huge load time, as if no warm-up was done... – Leonardo Apr 04 '18 at 13:58
-
1I know this is an old topic. But, was there ever a resolution? I'm seeing the same behavior as describe in @ByronTardif's comment. The new instance starts receiving requests before my app init page has loaded. – Bob May 29 '19 at 21:47
How can I do this? IS it possible to add a timeout? Or is it done automatically somehow?
If you use horizontal scaling, also called scaling out and in, azure will keep your application continues running without interruption as new resources are provisioned.
Azure will automatically warm up the new instance's application and add a Load Balance to distribute the requests between them automatically. You don't need to configure the load balance separately by yourself.
More details about how azure auto scale work, you could refer to this article and this article.
"Azure will automatically warm up the new instance's application" - the links you provided do not say this - can you provide other references? I essentially don't want the instance added to a load balancer until a specific URL is accessible with 200 OK or after a hard timeout of 2 minutes.
After the web app scale out to the 2 instance, if the new request are send to the default instance web site , azure will warm up the new instance's web app.
You could write a test as below:
Add below config codes in web.config's webserver tag to trace all the request:
<tracing>
<traceFailedRequests>
<clear/>
<add path="*">
<traceAreas>
<add provider="WWW Server" areas="Authentication,Security,Filter,StaticFile,CGI,Compression,Cache,RequestNotifications,Module,Rewrite,iisnode" verbosity="Verbose" />
</traceAreas>
<failureDefinitions statusCodes="200-600" />
</add>
</traceFailedRequests>
</tracing>
Then if your site scale out to 2 instance, after you access the web app. Load balance will not redirect the request to the second instance since the instance's process doesn't start. Azure will auto warm up the second instance's web app.
You could find the log as below image shows:
The log result:
fr00030.xml(you could find the process is 5860 old instance):
fr00031.xml(you could find the process is 8164 new instance and takes 4015 msec)
Besides, as Byron Tardif says, if you want to enable custom warm up(warm up all the pages), you could use Application Initialization Module.
It will also be called before the new request access your second web app instance.

- 22,586
- 6
- 37
- 65
-
2Thanks but this does not answer my question. I understand how auto-scaling generally works with App Services. What I need to know is how Azure determines when an application is warmed up so it can serve traffic. You say "Azure will automatically warm up the new instance's application" - the links you provided do not say this - can you provide other references? I essentially don't want the instance added to a load balancer until a specific URL is accessible with 200 OK or after a hard timeout of 2 minutes. – Ennis Jun 15 '17 at 16:02
-
Awesome answer! Thanks for sharing the link to Application Initialization Module. That will help us solve this for swaps as well!! – Ennis Jun 16 '17 at 17:55
-
There's an unanswered question on the site you linked for the Application Initialization Module: if you supply the host name like this, how can you be sure it warms up the correct instance in a multi-instance setup (e.g. autoscaling). – Josh Mouch Jan 17 '18 at 15:42
-
This answer is juste FALSE. Azure will spin out a new scale out instance and just do "start it". There is abolutelly no time allowed for warm up. Ex : WebForms app will not have time to precompile and prerender views before trafic hits. – Jurion Apr 04 '22 at 13:27