Tuesday, November 19, 2013

ASP.Net MVC AJAX requests are not running in parallel when the Session is enabled

Recently I was into a mission of increasing the speed of an ASP.Net MVC web site which was continuously throwing exceptions (time outs) when the data increased to 10,000 records in one table.

Initially the site was taken care as stepchild which got less developers to work on and less allocation of design time as it is targeted to admin people who are less in number. Now the things changed suddenly and it needs to be a competitor for Facebook in terms of performance.

The main issue was due to lack of join support in OData. We fixed so many coding issues which are related to usage of WCF Data services which uses OData protocols internally. Then made most of the pages AJAX friendly. Implemented real lazy paging in grids. Earlier, the app was pulling all data to the client side and was doing paging in javascript.

The site became responsive in 5 seconds. Now a special issue got our attention. Multiple AJAX calls, we issued from javascript are not getting executed in parallel in the server side. In particular page, there are 2 AJAX requests made from jQuery. Based on the server logs, each takes 2 seconds to process. But when we looked in F12 tools, the second call ends only after 4 seconds. The search ended up in usage of session variables. By default 'only one thread can access particular Session'. In our case both the AJAX requests are executed from same page which means the execution of action methods will be serial.

Initially we though of getting rid of session usage in the whole application. But after sometime, we started thinging differently. We are not the ones who are facing this issue. Somebody might also faced it and resolved it. What they did to resolve this?

It simply ended in SessionState attribute where we can control the behaviour of session usage per Controller. Thanks to the authors of below blogs.

http://www.stefanprodan.eu/2012/02/parallel-processing-of-concurrent-ajax-requests-in-asp-net-mvc/
http://johnculviner.com/asp-net-concurrent-ajax-requests-and-session-state-blocking/

How to apply SessionState to action method level

http://sapoval.org/per-action-custom-sessionstateattribute

No comments: