Keep alive for IIS sites

IIS has numerous configurations to help the hosting providers to use their hosting box effectively.

One such configuration is "Set Idle Timeout" - This feature lets the hosting providers to easily configure on how long a website can stay idle (in memory) without serving any visitors or requests. The default value is around 20 minutes after which your website will go to deep sleep mode (just like it doesn't exist at all) allowing the server to effectively serve more requests for other sites in the same box.

This is truly a blessing in disguise for hosting providers, who provides shared hosting space for multiple webmasters. They can stuff in more website to a box. This advantage rather brought in many greedy shared hosting providers who throttle this TimeOut further to a lesser value and move most of their new or idle website out of memory as early as possible.

Looking from the webmasters perspective:

  1. Starting a new website from a scratch is a challenge.
  2. Driving in new traffic is another major task that he should plan off, at-least this should continue till he can gain a better visibility in the web space.

So through all pain and hard work, he would charm few visitor to land-up in the website's homepage (That is a huge milestone in webmasters life - looks like a start of something great to come).

But just like in movies, how Big shots take out their newbie opponent out of business., the newly hosted site would have been harassed and thrown out of memory. The visitor who lands on the site will witness a delay, caused by ASP.Net warmup phase, page compilation & data load from remote server so on. This is a huge turn off for any visitors, who would have browsed millions of websites that would load up within a second. There is more than 90% of chance that visitor would press a back button or close the browser tab.

Everything seems lost!!!

Solution

Many frameworks would come-up with a default way to keep the website occupied, so that it wouldn't be chunked out of memory.

If you are building your own solution and find this IIS feature as a frustrating behavior. Then we can fix it through following ways.

1) Website monitoring service:
There are plenty of free website monitoring services that can ping and watch your website status. e.g., pingometer.com
This ping can help your website to stay alive.

But the challenge using this way, is that the service would have brief idle time before each ping. If the application timeout is lesser than that. Then there is a greater chance that the your website will be swapped out of memory.

2) Writing a console application to ping:

There are plenty of application available to do this job. but that would force you to keep your home connection always on to keep you site on ( :) sounds like a perfect rhym). If you are planning on opting for this method, then I would suggest an alternative to get a Static IP and configure a DNS to your IP and host your website from your home system.

3) Write your own ping service from your web application :

If you run a asp.net website, you could do following through code in your Global.ascx file. Create a worker thread that would keep on pinging your website after fixed duration, say 5 mins and keeps your site busy & warmed up for a visitor.

protected void Application_Start()
{
    _SetupRefreshJob();
}

private static void _pingSite()
{
  using (WebClient refresh = new WebClient())
  {
     try
     {
         refresh.DownloadString(ConfigurationManager.AppSettings["SiteUrl"]);
     }
     catch (Exception ex)
     {
         Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(ex));
     }
  }
}

private static void _SetupRefreshJob()
{
   if (HttpContext.Current == null) return;
   
   //remove a previous job
   Action remove = HttpContext.Current.Cache["Refresh"] as Action;
   if (remove is Action)
   {
      HttpContext.Current.Cache.Remove("Refresh");
      remove.EndInvoke(null);
   }

   //get the worker
   Action work = () =>
        {
           while (true)
           {
             Thread.Sleep(600000);
             _pingSite();
           }
       };
    work.BeginInvoke(null, null);

   //add this job to the cache
   HttpContext.Current.Cache.Add("Refresh",
                work,
                null,
                Cache.NoAbsoluteExpiration,
                Cache.NoSlidingExpiration,
                CacheItemPriority.Normal,
                (s, o, r) => { _SetupRefreshJob(); }
     );
}

private void Application_End(object sender, EventArgs e)
{
     // Force the App to be restarted immediately
     _pingSite();
}

Apparently, this above method would keep your site from the idle timeouts but not from the other factors or measures that your wicked hosting providers would have :)

4) Application Initialization Module for IIS

If you have a dedicated hosting box for your company or your hosting provider does hear out your concerns, then this will be the authentic way to warm up and keep your websites alive.

This module is available for IIS 7.5 & above through which website administrators can improve the responsiveness of their Web sites by loading the Web applications before the first request arrives. Your website application pool (worker process) will be set as always running mode. So even when timeouts or application memory recycling happens, your website would be automatically reloaded without any external interference. This would give visitors a breeze e load feeling.

You can read more about the module in this page on application initialization module.

That's all for now. If you have reached here and still reading this statement, kindly do share this article wink.