Tuesday, 16 December 2014

How Many Web Applications Should I Run Per Application Pool


When we think of desktop applications like notepad, we know that each time it runs, the OS defines a new process (notepad.exe for example)...

This is not the same for a web application... A web application needs a host process to be executed. This means that a process manages the web application and allows its execution. Starting from IIS6, this process is called w3wp.exe.
A web application runs on top of an application pool that you define in IIS. An application pool, can be associated to one or more worker processes.

The following example defines a common mistake people make when setting up web applications within web servers.... A web server administrator defines an application pool and executes multiple web applications on top of it. I worked with customer who has 20 -30-web applications on top of a single application pool!
This is bad very bad especially for 32-bit processes. Let me try to explain why...

Every web application needs:
1. Assemblies loaded in memory
2. Memory to calculate the application goal.

If you define a single application pool for all web applications, what happens is that all web apps have to load their assemblies and share the same memory. Do you think that this memory is infinite? No, it is not.

In a 32-bit process, by default every application can allocate 2GB of memory and a 32-bit process on a 64-bit machine 4GB. Those values are the maximum ones available by default, but do not except to use all that memory.

Typically, an Out Of Memory exception occurs before that value. It happens due to internal memory fragmentation!

In the case that your applications are managed: ASP.NET to be clear, what happens is that Garbage Collector (GC) cleans up the memory. If all web applications share the same memory, then this one is under pressure.
It means that GC runs multiple times per second in order to provide clean memory for your app. What is the side effect? In server mode, the GC is required to stop all threads activities to clean up the memory (this was improved on .NET Fx 4.5, where the collation does not require to stop all threads).

What is the side effect?
Slow performance of your web application
High impact on CPU time due to GC’s activity.

Furthermore, if one of your web applications crashes, all application running on the same pool will be impacted. There are also other things to consider....
Threadpool, the number of threads that a single process can define is not infinite. So all of the web applications have to share that number/threads. Same thing for the connection pool and so on.

Please do not consider this post in the following way: define necessarily one application pool per web application. This is not the goal, IIS gives you the flexibility to host multiple applications on the same application pool. However, when you decide how many applications have to run on top of the same application pool, take care!

No comments: