ASP.NET Site Performance Secrets
Simple and proven techniques to quickly speed up your ASP.NET website
- Speed up your ASP.NET website by identifying performance bottlenecks that hold back your site’s performance and fixing them
- Tips and tricks for writing faster code and pinpointing those areas in the code that matter most, thus saving time and energy
- Drastically reduce page load times
- Configure and improve compression – the single most important way to improve your site’s performance
- Written in a simple problem-solving manner – with a practical hands-on approach and just the right amount of theory you need to make sense of it all
It is good to monitor the performance of your site continuously
Tip: The performance of your website is affected by both the things you control, such as code changes, and the things you cannot control such as increases in the number of visitors or server problems. Because of this, it makes sense to monitor the performance of your site continuously. That way, you find out that the site is becoming too slow before your manager does.
It is important to reduce the “time to first byte”
Tip: The “time to first byte” is the time it takes your server to generate a page, plus the time taken to move the first byte over the Internet to the browser. Reducing that time is important for visitor retention—you want to give them something to look at, and provide confidence that they’ll have the page in their browser soon. It involves making better use of system resources such as memory and CPU.
Caching is one of the methods used to improve website performance
Tip: Caching allows you to store individual objects, parts of web pages, or entire web pages in memory either in the browser, a proxy, or the server. That way, those objects or pages do not have to be generated again for each request, giving you:
- Reduced response time
- Reduced memory and CPU usage
- Less load on the database
- Fewer round trips to the server, when using browser or proxy caching
- Reduced retrieval times when the content is served from proxy cache, by bringing the contents closer to the browser
Building projects in the Release mode is a good practice
Tip: If your site is a web-application project rather than a website, or if your website is a part of a solution containing other projects, be sure to build your releases in the release mode. This removes debugging overhead from your code, so it uses less CPU and memory.
It is important to reduce Round Trips between browser and server
Tip: Round trips between browser and server can take a long time, increasing wait times for the visitor. Hence it is necessary to cut down on them. The same goes for round trips between web server and database server.
Tip: If you are redirecting visitors to a new page because the page is outdated, use a permanent 301 redirect. Browsers and proxies will update their caches, and search engines will use them as well. That way, you reduce traffic to the old page.
You can issue a 301 redirect programmatically:
Response.StatusCode = 301;
For .NET 4 or higher:
Hotlinking should be avoided
Tip: Hotlinking is the practice of linking to someone else’s images on your site. If this happens to you and your images, another web master gets to show your images on their site and you get to pay for the additional bandwidth and incur the additional load on your server. A great little module that prevents hot linking is LeechGuard Hot-Linking Prevention Module at http://www.iis.net/community/default.aspx?tabid=34&i=1288&g=6.
Session state is taking too much memory
Tip: If you decide that session state is taking too much memory, here are some solutions.
- Reduce session state life time
- Reduce space taken by session state
- Use another session mode
- Stop using session state
Tip: ASP.NET disables all output caching if you set a cookie, to make sure the cookie isn’t sent to the wrong visitor. Since setting cookies and proxy caching also don’t go together performance-wise, you’ll want to keep setting the number of cookies to a minimum. This can be done by trying not to set a cookie on every request but only when strictly needed.
Minimizing the duration of locks
Tip: Acquire locks on shared resources just before you access them, and release them immediately after you are finished with them. By limiting the time each resource is locked, you minimize the time threads need to wait for resources to become available.