Eliminate startup delay by suspending the app pool and using Application Initialization

Eliminate startup delay by suspending the app pool and using Application Initialization

By | 2016-12-01T16:37:42+00:00 January 15th, 2014|Tips & Tricks|6 Comments
[2015-10-19] This post was updated to describe changes present in IIS 8.5 (Windows Server 2012 R2): the addition of the Idle Worker Process Page-Out feature and the removal of the Start Automatically setting.

It is well known that ASP.NET applications have a startup delay after they’ve been idle for a while. Every once in a while someone posts in the forum that the gallery is slow to load at first and then is quite fast. That’s because IIS is doing a lot of stuff when that first HTTP request comes in:

  • Spins up an application pool
  • JIT-compiles the code
  • Performs view generation of the EF model
  • Loads HTTP modules
  • Runs initialization code in GSP, which connects to the database and loads application settings and other lookup data.

All that takes a few seconds, which isn’t terrible, but first impressions are important. Wouldn’t it be better if IIS handled this before that first HTTP request comes in?

Well, it can. There are two new features in recent versions of Windows Server that help your gallery be instantly available, even after periods of inactivity that would normally shut down the application pool. They are the Idle Worker Process Page-Out and Application Initialization features.

Idle Worker Process Page-Out (Windows Server 2012 R2 and higher (IIS 8.5+)

This feature was introduced in Windows Server 2012 R2 (IIS 8.5). When enabled, instead of the IIS worker process shutting down after a period of inactivity, the process is suspended and paged out to disk. A suspended process uses little server memory but can be quickly resumed for the next user request.

To set it up, follow these steps:

  1. Open IIS Manager and navigate to the application pool the gallery is running under. Right click it and choose Advanced Settings.
  2. Find the setting Idle Time-out Action and change it to Suspend (instead of Terminate). If you want, you can also change the Idle Time-out Value from the default of 20 minutes.

Idle Time-Out Action setting in IIS 8.5 (Windows Server 2012 R2 and higher)

 

Application Initialization

This feature automatically starts up your app so it’s ready to go by the time a user requests it. It was first supported as an add-on in Windows Server 2008 R2 (IIS 7.5) and is built in to the OS in later versions.

The configuration depends on your OS version, so pick the right one below and read on.

 

Application Initialization in Windows Server 2008 R2 (IIS 7.5)

In Windows Server 2008 R2, use the Microsoft Web Platform Installer to install Application Initialization. Once installed, configure it by editing applicationHost.config, which is at %WINDIR%\system32\inetsrv\config.

  1. Open applicationHost.config in Notepad, being sure to run it with the “Run as Administrator” option.
  2. Find the <applicationPools> configuration section, and then look for the application pool running your gallery. If you don’t know the pool name, look it up in IIS Manager by right-clicking the application node in the left pane and choosing Manage Application > Advanced Settings. Add startMode=”AlwaysRunning”  so that it looks like this:<add name=”.NET v4.5″ startMode=”AlwaysRunning” managedRuntimeVersion=”v4.0″ />
  3. Scroll down a little more in applicationHost.config to the <sites> configuration element. Find the entry for the gallery application and add preloadEnabled=”true” , like this:<application path=”/gallery” preloadEnabled=”true” applicationPool=”.NET v4.5″>
  4. Restart IIS by executing iisreset in a command prompt running as an administrator.

That’s it. Your gallery is now always running in a warmed up state and ready to instantly serve users. You can test it out by temporarily changing the application pool timeout to one minute, then waiting a couple minutes and browsing to your gallery.

 

Application Initialization in Windows Server 2012 and higher (IIS 8+)

In Windows Server 2012 and higher, Application Initialization is included in the OS but it must be enabled in the Add Roles and Feature Wizard:

Application Initialization in Windows Server 2012 R2 and higher (IIS 8.5+)

One installed, you can now configure it with IIS Manager – no need to edit applicationHost.config.

  1. Open IIS Manager and navigate to the application pool the gallery is running under. Right click it and choose Advanced Settings. Verify the Start Mode is set to AlwaysRunning. In Win Server 2012, you also need to set Start Automatically to True (this setting was removed in 2012 R2). Click OK.

    app_init2-25255B4-25255D

  2. In the left pane of IIS Manager, navigate to the gallery web application. Right click the application node and select Manage Application > Advanced Settings. Set Preload Enabled to True.

    app_init3-25255B4-25255D

  3. Restart IIS by executing iisreset in a command prompt running as an administrator.

That’s it. Your gallery is now in an always-on state.

 

Common mistakes

The web is filled with people talking about how they can’t get Application Initialization to work. I’m guessing it’s because they missed one or more of the four critical steps. If you’re having trouble getting things going, make sure to verify the following:

  1. Install Application Initialization (either through Web PI for IIS 7.5 or through the Add Roles and Features Wizard for IIS 8+).
  2. (IIS 8.0 only) Application pool must have Start Automatically = True.
  3. Application pool must have Start Mode = AlwaysRunning.
  4. Web application must have Preload Enabled = True.

About the Author:

Founder and Lead Developer of Gallery Server

6 Comments

  1. KenTi January 15, 2014 at 5:22 pm

    Thanks for this helpful tip… Is there a downside to it?

  2. Roger Martin January 15, 2014 at 7:34 pm

    I haven’t heard about any downsides, other than it requires server resources to always stay alive.

  3. Paulo Natel January 28, 2014 at 4:37 am

    Hi Roger,
    Thanks for the tip, just a couple of questions:
    – What is the effect in the Auto-sync of the gallery?
    – Is it going to eliminate the gap between visits?
    Regards

  4. Roger Martin May 8, 2014 at 12:06 pm

    Paulo – I don’t actually know the answer. It all depends if the feature invokes a request to the application.

  5. Trevor Jones January 18, 2020 at 11:49 am

    Hi im on Windows Server 2019.

    I followed your steps no problem. But After enabling the Application Initialization does the whole Machine require a restart?

    Im asking because i dont see that Start Automatically = True.

    But Always Running was always there even before installing Application Initialization Feature.

    • Roger Martin January 19, 2020 at 4:57 pm

      Sorry, but I don’t have any experience with WS 2019. I suggest consulting the documentation or searching the web, since your question is not specific to Gallery Server.

Leave A Comment