views:

1417

answers:

9

How do you improve your ASP.NET MVC application performance?

+5  A: 

Not an earth shattering optimization but I thought I'd throw this out there - Use CDN's for jquery, etc.

Quote from ScottGu himself: The Microsoft Ajax CDN enables you to significantly improve the performance of ASP.NET Web Forms and ASP.NET MVC applications that use ASP.NET AJAX or jQuery. The service is available for free, does not require any registration, and can be used for both commercial and non-commercial purposes.

We even use the CDN for our webparts in Moss that use jquery.

RandomNoob
Thanks BnkDev +1Adding to the list....
SDReyes
+4  A: 

Basic suggestion is to follow REST principals and the following points ties some of these principals to the Asp.Net MVC framework:

  1. Make your controllers stateless - this is more of a 'Web performance / scalability' suggestion (as opposed to micro/machine level performance) and a major design decision that would affect your applications future - especially in case it becomes popular or if you need some fault tolerance for example.
    • Do not use Sessions
    • Do not use tempdata - which uses sessions
    • Do not try to 'cache' everything 'prematurely'.
  2. Use Forms Authentication
    • Keep your frequently accessed sensitive data in the authentication ticket
  3. Use cookies for frequently accessed non sensitive information
  4. Make your resources cachable on the web
  5. Compile your Javascript. There is a library to do it as well. (sure there are others too)
  6. Use CDNs - especially for your large media files and so on.
  7. Consider different types of storage for your data eg. files, key/value stores etc. - not only SQL Server
Maxwell Troy Milton King
Thank you Max well +1!!Please, May you explain me 6 in further detail? : )Adding to list....
SDReyes
hi, how can i compile javascript?
nWorx
Nice question : O, probably he means minimize... do you Max?
SDReyes
It's impossible to not use sessions in ASP.NET MVC given that the core framework relies on them anyway. Obviously you could always argue that not using something would increase performance, but I don't see any major performance difference by using sessions selectively.
Nissan Fan
Yep : ) already removed...
SDReyes
@Nissan Fan: I don't use Session directly in my ASP.NET MVC application, but it is needed if you want to use TempData. By default TempData is based on Session, you can write your own TempDataProvider and completely get rid of session. There are many reasons not to use Session, but performance doesn't seem to be most relevant.
LukLed
How would compile javascript ??
Yassir
Are there other parts of the MVC framework relying on Sessions? (apart from TempData)
Maxwell Troy Milton King
Sorry, just put a link to javascript compilation.
Maxwell Troy Milton King
Than you again Maxwell +1 listing updated : )
SDReyes
@NissanFan Just because ASP.NET uses sessions, doesn't mean your app does. If you want your app to best RESTful (and therefore easily horizontally scalable) you are fine so long as you don't actually utilize session data.
Matt
+5  A: 

This is nice presentation about ASP.NET MVC performance:

http://codeclimber.net.nz/archive/2009/04/17/how-to-improve-the-performances-of-asp.net-mvc-web-applications.aspx http://blog.whiletrue.com/2009/04/aspnet-mvc-performance/

Compiled query will increase performance of your application, but it has nothing in common with ASP.NET MVC. It will speed up every db application, so it is not really about MVC.

LukLed
Thank you Luk +1!You're right, nonetheless I think it will be useful for lots of MVC developers to know, what do you think? : )Updating title accordingly... thanks again1!
SDReyes
+4  A: 

When accessing data via LINQ rely on IQueryable ...

http://stackoverflow.com/questions/1106802/why-use-asqueryable-instead-of-list

... and leverge a good Repository pattern:

http://stackoverflow.com/questions/1223194/loading-subrecords-in-the-repository-pattern

This will optimize data access to ensure only the data needed is loaded and when only it is needed.

Nissan Fan
Thank you Nissan, good point +1
SDReyes
+31  A: 

A compiled list of possible sources of improvement are below:

General

  • Make use of a profiler to discover memory leaks and performance problems in your application. personally I suggest dotTrace
  • Run your site in Release mode, not Debug mode, when in production, and also during performance profiling. Release mode is much faster. Debug mode can hide performance problems in your own code.

Caching

  • Use CompiledQuery.Compile() recursively avoiding recompilation of your query expressions
  • Cache not-prone-to-change content using OutputCacheAttribute to save unnecessary and action executions
  • Use cookies for frequently accessed non sensitive information
  • Utilize ETags and expiration - Write your custom ActionResult methods if necessary
  • Consider using the RouteName to organize your routes and then use it to generate your links, and try not to use the expression tree based ActionLink method.
  • Consider implementing a route resolution caching strategy
  • Put repetitive code inside your PartialViews, avoid render it xxxx times: if you end up calling the same partial 300 times in the same view, probably there is something wrong with that. Explanation And Benchmarks

Routing

Security

  • Use Forms Authentication, Keep your frequently accessed sensitive data in the authentication ticket

DAL

Load balancing

  • Utilize reverse proxies, to spread the client load across your app instance. (this site for example use HAProxy (MSDN).

  • Use Asynchronous Controllers to implement actions that depend on external resources processing.

Client side

  • Optimize your client side, use a tool like YSlow for suggestions of your work.
  • Use AJAX to update components of your UI, avoid to update the whole page when possible.
  • Consider implement a pub-sub architecture -i.e. Comet- for content delivery against reload based in timeouts.
  • Move charting and graph generation logic to client side if possible. graph generation is a expensive activity. deferring to client side frees your server from an unnecessary burden, and allow you to work with graphs locally without make a new request. i.e. Flex charting, jqbargraph, MoreJqueryCharts
  • Use CDN's for scripts and media content -and so on- to improve scripts loading in the client side. i.e. Google CDN
  • Minify -Compile- your JavaScript in order to improve your script size
George Stocker
The nissanfan links don't seem to work. :(
JTew
Thanks for your feedback Jtew +1
SDReyes
Content reordered, categorized and reviewed ; )
SDReyes
wait you mean i do lose performance when i e.g. have a view that displays a result set by irritating through an IList and call Render.PartialView("Row", item) for each list item? how much do i lose? or how could i measure the performance gain?
marc.d
Thanks Marc! +1A explanatory link was added (benchmarking subtly included ; )
SDReyes
+1 for figuring out how to snatch all the up votes just by concatenating everyone else's answers
cottsak
@cottsak for Wiki posts it's the 'way you do it'.
George Stocker
@Cottsak thank you : )
SDReyes
+3  A: 

Also if you use NHibernate you can turn on and setup second level cache for queries and add to queries scope and timeout. And there is kick ass profiler for EF, L2S and NHibernate - http://hibernatingrhinos.com/products/UberProf . It will help to tune your queries.

zihotki
Thank you again Zihotki +1 !Adding... adding..
SDReyes
Ayende recently blogged about how EF Profiler helped him tune a sample MVC app: http://ayende.com/Blog/archive/2010/05/17/analyzing-the-mvc-music-store-data-access.aspx
Frank Schwieterman
+2  A: 

IN addition to all the great info on optimising your app on the server side I'd say you should take a look at YSlow, it's a superb resource for improving site performance on the client side.

This applies to all sites, not just ASP.NET MVC.

Steve Haigh
Thank you Steve! +1Nice tip ; )
SDReyes
+1  A: 

One super easy thing to do is to think asynchronously when accessing the data you want for the page. Whether reading from a web service, file, data base or something else, use the async model as much as possible. While it won't necessarily help any one page be faster it will help your server perform better overall.

No Refunds No Returns
+3  A: 

This may seem obvious, but run your site in Release mode, not Debug mode, when in production, and also during performance profiling. Release mode is much faster. Debug mode can hide performance problems in your own code.

Craig Stuntz