Saturday, October 2, 2010

ASP.NET Slow on first access

Problem
Response is really slow when a page of ASP.NET/MVC web(site or app) is accessed first time.
It takes around(depending?) 4/5 seconds to sometimes a minute for the page to load.

But, interestingly, every following request is fast; as it should be.

The above process(first request slow, and consecutive fast) gets repeated approximately after every few hours of inactivity; and especially on the day start.

Reason?
1. The first slow response is related to the JIT compilation of the service assemblies.
2. The IIS application pool is shut down after 30 minutes of inactivity.

After that, when a request is made, IIS has to start the website up again, which leads to this behavior.

Workarounds?

Workaround 1: Warmup script
Write some code in the Application_Start event of the Global.asax file that calls the main pages or you web.

Here is a well known app startup script available on internet.

Alternatively, you can program it as a separate app and run this script as a scheduled task in Windows Scheduler.

Workaround 2: Remove the recycle option entirely
This could be the recycle worker processes setting on the application pool, check the value for that and either turn it off or make it longer.

You can also configure the shutdown worker process setting, after being idle under performance for the application pool.
  • Type inetmgr.exe in Start-->Run and press enter
  • Select Application Pools; right click and select properties
  • Select Performance tab
  • Uncheck Shutdown worker processes after being idle for (time in minutes): or set it to a higher value or uncheck it entirely.

Workaround 3: Windows service
Create a windows service[http://www.yasarmurat.com/Blog/33/asp-net-application-warm-up-by-using-windows-service.aspx] that calls the pages after certain itervals of time.

Workaround 4: AutoStart Feature in .NET4.0
If you are using .NET 4.0, worry not; because there is an Auto-Start feature of the 4.0 framework. This article might interest you.

Workaround 5: IIS7 WarmUp Feature
If you using IIS7, use Application Warmup feature, that does the job for you.

Workaround 6: Use uptime robot?

If this is a production server then why not try adding a website monitor; such as up time robot. It basically asks for your websites headers and gets status codes like 200-OK, 404-NOT FOUND (etc) every scheduled(5, for instance) minutes.

This way your site is always spun up and does not impact log files/analytics as only headers are requested.

Plus, its free for up to 50 sites!

Helpful Resources

No comments:

Post a Comment

Related Posts

Popular Posts