Improve Performance of ASP.Net and Web Service

This article provides a few important tips for improving performance of ASP.NET and Web Service applications. They can help in the development of highly responsive web applications. This article assumes a basic understanding of ASP.NET and web applications.

Generally, a web application's performance  depends on network speed and low bandwidth. Since there is a client and a server, all the information (not stored in local storage or cached) comes from the server and is displayed in the client. Now, if the channel capacity or speed of the network is low then the application will definitely run slowly, in spite of good design and best practice implementation. So, for that we need to follow a few guidelines for application development.

Improve performance of ASP.NET

In this section we will explain a few topics for improving the performance of ASP.NET applications.

Consider low bandwidth in time of development

Just keep in mind that the application should be able to run in a low bandwidth. Start by timing your page size and by minimizing the number and the size of graphics, particularly in low network bandwidth scenarios. We can partition the entire page to benefit from improved cache efficiency. If not necessary, try to disable the view state of the pages that do not post back to the server or pages that use server control. Ensure that your pages are batch compiled. Enable buffering so that ASP.NET batch works on the server and avoids chatty communication with the client.

Try to not use ASP.NET Server controls

Yes, this is not my advice. Microsoft people suggest not to use server controls in ASP.NET applications. The reason is that an ASP.NET server control is first processed in the server and is then converted to a pure HTML element for the Web Browser. Now, the conversion time is a bottleneck of the application's performance. For example we can use a GridView to display data in bulk in the web page. It's easy to manage a huge amount of data but a GridView generates a lot of server-side code that processes when the data is bound. Then, what is the solution? We can use a simple HTML control instead of an ASP.NET server control. We can use the MVC design pattern to do that.

Tune ASP.NET thread pool

If your application queues requests with an idle CPU then you should tune the thread pool. For applications those server requests quickly consider those settings in the Machine.config file.

  • Set maxconnection to 12 times the number of CPUs
  • Set maxIoThreads and maxWorkerThread to 100
  • Set minFreeThreads to 88 times the number of CPUs

Store data in Cache

The main purpose of caching is to improve performance. ASP.NET can cache data by using the Cache API. We can cache either the output of an entire page or a portion of a page. Regardless of the implementation approach, we need to consider an appropriate caching policy that identifies the data you want to cache, the place you want to cache the data in, and how frequently you want to update the cache. It is a good idea to update the cache once data is changed in the database or if the input condition changes.

Try to implement AJAX where needed

Like a cache, AJAX is also all about performance enhancement. Try to implement AJAX for an ASP.NET application. Using AJAX will enable the DOM model to be changed without effecting an entire page or reloading an entire page without reason.

Now, if you are an experienced web developer and working with the ASP.NET technology then you might know various ways to implement AJAX in ASP.Net (using an Update panel, or the jQuery AJAX method) the best and feasible way to implement AJAX is with the jQuery AJAX method, because an update panel fails in heavy data processing and if there are more update panels in a single page then page performance decreases.

Handle Session data

If there is no need to use a session then just disable it. If there is a need to use a session then the following are three options to maintain the session:

  • In process session state
  • Out of process session state, we can use state server.
  • SQL Server

The in-process state offers the best performance, but it introduces process affinity, that prevents you from scaling out your solution in a web farm. For a web farm scenario, you need one of the out-of-process stores. However the out of process store incur the overhead of serialization and network latency. We need to keep in mind that any object we want to store out of process session state must be serializable.

Other optimization include using primitive types where you can minimize serialization overhead and using the Read Only attribute on pages that only read the session state.

Call any service asynchronously

Ensure that your application is not waiting for any service. We don't know the status of the third-party web server. If their server is down or slow then our application will suffer from performance . It's a good idea to call any service asynchronously, then the application will not wait for the results of the other service.

Improve performance of Web Service

Let's try to understand how to improve the performance of a Web Service in this section.

At a Glance

Start by tuning the thread pool. If you have sufficient CPU and if you have queued work then ensure that you have a pool web service connection.

Ensure that you are sending the exact data that needs to be sent. Try to send chunky data from the service rather than sending a big chunk. Also consider using asynchronous server-side processing if your Web Service performs extensive I/O operations. Consider caching fro reference data from any internal data that your web service relies upon.

Handle large data efficiently

If data is too large then chunk it and send it. Check that the maxRequestLength paremeter in the <httpRuntime> element of your configuration file is large enough. This parameter limits the maximum SOAP message size from a Web service. Check the timeout settings of the Web service application. Keep in mind that the timeout of a Web Service should be less than the timeout of ASP.NET (or any client) application.

  • Use a byte array parameter
  • Return an URL to the file and then use HTTP to download it.
  • Try to use streaming if you want to transfer data in megabytes.

Conclusion

Those are the few guidelines we can follow in time of ASP.NET and Web service application. Hope you have enjoyed the concepts and understood them properly.