AJAX performance and Session Management

Asynchronous loading of content on a web page is an effective way to make web pages with slow loading content appear to run faster.  However, beware abusing this technique, as you may end up making your initial problem worse.

Consider a web page with 2 div elements that will be loaded by a jQuery ajax load function.  If you simply list these two load commands one after another in a javascript function, the browser will submit both requests back to back, effectively simultaneously. 

If you are like me, and don’t modify your Asp.Net MVC controllers too much from their boilerplate defaults, you are getting burned by this configuration.  By default, the two requests from jQuery will request read-write access to their server-side session. These requests are translated as GetItemExclusive calls to the Session provider, which block other requests for session while they are operating.

The first request will request a exclusive access to session, succeed and return promptly.  However, this request will block the second request.  When the second request to the server is processed and requests exclusive access, it will be denied by the first request.  In Asp.Net, the attempt to reload session will block for half a second and then re-attempt to acquire the lock. (MSDN:  http://msdn.microsoft.com/en-us/library/aa479024.aspx)

How should we work around this ‘limitation’?  Use an Asp.Net controller decorated with the SessionState attribute  When you mark the controller that will generate this content with a ReadOnly or None session state, the requests Asp.Net will make for session will NOT block each other.  The result you should see is near simultaneous return of your two requests.