views:

606

answers:

3

I'm wondering how the concurrency in a web application actually works. Ive read several articles and to my understanding multiple instances of HttpApplication would be working at the same time. Now, I created a simple web app to test concurrency and put the following to global.asax:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    Response.Write("Request started: " + DateTime.Now);
    System.Threading.Thread.Sleep(10000);
    Response.Write("<br />");
    Response.Write("Request ended: " + DateTime.Now);
    Response.End();
}

I was expecting that if I browse to the web app root in several browser tabs at nearly the same time, they would start and stop concurrently. However, it seems that they don't. Second tab start time is same as first time end time. I then tested by having this same code in an httpmodule or default.aspx page_load and got the same result.

What is going on here? Why aren't the requests being served parallel?

Edit: I'm placing my understanding mainly to two articles:

http://msdn.microsoft.com/en-us/magazine/cc188942.aspx says "If multiple requests targeting the same application arrive simultaneously, multiple HttpApplication objects will be used."

and http://www.code-magazine.com/article.aspx?quickid=0511061&amp;page=5 has an example for an aspx page doing basically what I tested, with comment "Simulate slow request so we can see multiple requests side by side." next to Thread.Sleep call

It is possible that I'm completely misunderstanding something... but what?

http://www.code-magazine.com/article.aspx?quickid=0511061&amp;page=5

+1  A: 

Why don't you modify the sample to print the thread ID? That will tell you if multiple threads are serving the requests concurrently. I bet it is.

msvcyc
Thread ID does change every now and then, but still each request starts after each other.
mmiika
+2  A: 

Heh. The problem was Google Chrome. I opened two tabs in it and it seems when they point to same url that the requests get sent sequentially, one after the other has completed!

Thanks for ideas though!

mmiika
+1  A: 

Each request that arrives is routed to a separate HttpApplication object. The HttpApplication object is either created from scratch or allocated from a pool. The maximum number of HttpApplication objects created is limited by the maximum number of threads available. In ASP.NET 1.x I think the default was 20 or so. Under ASP.NET 2.0 this limit is managed dynamically.

You would need create enough requests to exhaust the ASP.NET thread pool before seeing your application start to falter.

What the comment in the Code article means is not that his code is bottlenecking the server, he's using it as a way to be able to see the state multiple requests side by side such as different thread ID's.

Hope that helps
Kev

Kev
Thanks Kev, see my answer also... I thought that something odd was going on!
mmiika