Web Applications Performance Rules

Introduction

When you design and develop your web application, you always must take into consideration the performance factor. By doing this, you will remove the cost of rewriting modules, modifying code, or redistributing the application. A good practice that must be kept in mind for writing quality code is to make frequent code reviews. For example, I often find a better implementation of a certain module after a code review. Also, you should test different ways of implementing your code to determine the performance impact over your application.

Here are some rules that should be followed for writing high performing applications.

Use Caching to Store Your Content

As you know, ASP.NET allows you to cache entire pages, fragments of pages, or controls. You also can cache variable data by specifying the parameters on which the data depends. By using caching, you help the ASP.NET engine to return data for repeated requests for the same page much faster. There is one catch here: Caching consumes server memory, so it's not recommended to be used when you need to get updated data on your page.

Avoid Session State

Whether you store your data in in-process, on a state server, or in a SQL database, session state requires memory; it's also time consuming when you store or retrieve data from it. If you do not want to use session state, disable it on your web form by using the <@%Page EnableSessionState="falseb"%> directive. In case you use the session state only to retrieve data from it and not to update it, make the session state read only by using the <@%Page EnableSessionState ="ReadOnly"%> directive.

Avoid ViewSate

ViewState allows you to keep the content of a control across trips to the server. This comes with a cost: A greater amount of data is sent from the server to client end vice versa, so the speed and network bandwidth can be affected. You can avoid this drawback by setting the EnableViewState property of your web controls to false when you don't need them to keep their state across server trips.

Use Low-Cost Authentication

Authentication also can have an impact over the performance of your application. For example, passport authentication is slower than form-base authentication, which in turn is slower than Windows authentication.

Use the Server.Transfer() Method for Server-Side Page Redirection

It is better to use the Server.Transfer() method for server-side ASPX page redirection in the same application than the Response.Redirect() method. This will reduce the extra roundtrip required by the second method (Response.Redirect()) to perform client-side redirection.

Reduce The Number of Web Server Controls

The use of web server controls increases your application's response time because they need time to be processed on the server side before they are rendered on the client side. Therefore, take in consideration the usage of HTML elements where they are suited; for example, if you want to display static text.

Avoid Frequent Usage of Boxing and Unboxing

When a value type, such as a structure, is copied to a reference type such as a class, the compiler creates an object on the heap and copies the value of the value type from the stack to this newly created object on the heap. This process is called boxing and requires more overhead than just a simple from value type to value type. When you copy a reference type to a value type, the value of the object from the heap is copied to the value type from the stack. This process is called unboxing. You should take in consideration the overhead of these processes when you design your application.

Avoid Using the String Type for Complex Strings Operations

The string type is immutable; this means that after a string is created it can't be modified. When you modify a string, the CRL creates a new string based on your modifications and returns it. The old string remains in memory until garbage collector clears it. If your application is extensively modifying strings, use the StringBuilder class. This class stores the string as an array of characters. The StringBuilder object is mutable and does in-place modifications of strings.

Use the AddRange() Method with Collections

There is a large number of collection classes that expose the AddRange() method. You can use this to add an array of items to the collection instead of calling the Add() method repeatedly inside a loop.

Avoid Throwing Exceptions

Throwing exceptions is a costly operation. You should be very careful when you throw an exception from your application. Exceptions should be thrown only to signify exceptional error cases. You should never throw exceptions just to manage your application flow.

Avoid Using Unmanaged Code

Calls to unmanaged code are a costly marshaling operation. Try to reduce the number of calls between the managed and unmanaged code. Consider doing more work in each call rather than making frequent calls to do small tasks.

Avoid Making Frequent Calls Across Processes

If you are working with distributed applications, this involves additional overhead negotiating network and application level protocols. In this case, network speed can also be a bottleneck. Try to do as much work as possible in fewer calls over the network.

Make Use of Optimized Managed Providers

As you know, OleDb is a generic provider that offers access to data exposed by any OleDb provider. Managed providers are specifically optimized for some databases. For example, when you use OleDb to connect to a SQL server database, OleDb first passes your request to the OLE DB COM components, which in turn translate the request to SQL Native Tabular Data Stream (TDS) format. Using SqlClient directly constructs the TDS packets and communicates with SQL server. The removal of the extra translation will significantly improve your application's performance.

Use Stored Procedures and Not SQL Statements

When you are working with a RDBMS as a SQL server, you should use stored procedures rather than SQL statements given as a text command because stored procedures are highly optimized for server-side data access.

Use DataReader Instead of DataSet for Forward-Only Sequential Access

If you are reading a table sequentially, you should use the DataReader rather than DataSet. The DataReader object creates a read-only stream of data that will increase your application performance because only one row is in memory at a time.

Use Connection Pooling

The slowest operation performed in a database application scenario is establishing a connection with a database. The SQL server .NET Data Provider offers connection pooling to improve performance when connecting to a SQL server database. In connection pooling, old connection information is stored in a connection pool so it can be re-used for the next connection. If you are using dynamic connection strings, you should disallow the connection pooling mechanism because connections are only pooled on the exact connection string.

Use Transactions

Distributed transactions might add performance overhead to your application, so you should use them only when required and keep their life as short as possible.



About the Author

Michael Heliso

I work as a software developer for about 3 years. Until these days I have worked for different international software companies using different technologies and programming languages like: C/C++, lotus script, lotus API, C#, ASP.NET, MS-SQL, Oracle, Domino Server. My main interest, at this time, is focused on .NET technology (http://dotnetcaffe.net).

Comments

  • Nice points

    Posted by falconsoft on 11/29/2006 12:10am

    Very useful points and listed very concisely.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • It's not unusual for a company to use a variety of formal and informal file-sharing methods. Many methods are fraught with significant operational, financial, and legal risks in addition to other potentially negative business consequences. Strategic managed file transfer (MFT) software and services help connect the business "dots" between the various ad hoc and more systematic ways that files are moved. Read this white paper to learn how the right MFT solution, can bring order, structure, and, importantly, …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds