Optimizing performance of MVC sites
This page contains recommendations that can help you optimize the performance of your MVC site.
Optimizing on-line marketing performance
For high-traffic websites that use the Kentico EMS on-line marketing functionality, we strongly recommend following the Best practices for EMS performance in MVC in addition to the recommendations listed below.
Loading data efficiently
Transferring data between storage spaces and the application can be one of the main performance bottlenecks. When retrieving content from the Kentico database (or other external sources), load only the data that you require in your views or other related code. In most cases, you do not need all data columns available in the source. The less data you retrieve, the faster your pages will be.
When loading data using the Kentico DocumentQuery or ObjectQuery API (for example via generated providers or in custom repositories and services), you can limit which data columns are loaded through the Columns method – specify the names of the required columns as an array of strings.
// Gets article data using a generated provider
IEnumerable<Article> articles = ArticleProvider.GetArticles()
.Columns("NodeID", "NodeAlias", "NodeSiteID", "ArticleTitle", "ArticleText") // Limits the retrieved data columns
.OnSite("MySite")
.Culture("en-US")
.Path("/Articles/", PathTypeEnum.Children)
.ToList();
Tip: In custom repositories or services, you may often need to wrap and extend DocumentQuery or ObjectQuery calls from other classes or providers. In these scenarios, use the AddColumns method instead of Columns – this adds to the list of retrieved columns without overriding any columns specified by the previous call.
Caching data and page output
Whenever possible, use caching for retrieved data and the output of controller actions:
- See Caching on MVC sites for more information.
- To avoid displaying of outdated content, set up cache dependencies.
- Use cache keys containing variables to cache different versions of dynamic content (for example different page output for each user).
Enabling IIS content compression
IIS content compression allows the system to lower the volume of transferred data by compressing the resources. There are two types of compression available in the IIS:
- Dynamic compression – compression of dynamically generated responses
- Static compression – compression of static content (images, document and other files on the file system)
To enable IIS compression in your MVC project:
- Install the required compression modules.
- Add a urlCompression element into the projects Web.config file and specify the following settings:
- doDynamicCompression – enables or disables the dynamic compression of content. The default value is true.
- doStaticCompression – enables or disables the static compression of content. The default value is true.
dynamicCompressionBeforeCache incompatible with Kentico environment
Using dynamicCompressionBeforeCache attribute of the urlCompression element is not possible in the Kentico environment.
Kentico is using a custom HTTP module, which modifies the HTML output with output filters (resolves relative links, adds anti-forgery tokens). When using the dynamicCompressionBeforeCache setting, HTML output is compressed before any output filters are applied and this results in invalid HTML output.
Scaling out MVC sites
If your site’s performance is not satisfactory after you have taken all possible steps to optimize the website’s code, you can consider scaling your hosting environment to multiple web farm servers.
With the MVC development model, your MVC application and Kentico application should already be configured to run as servers in an automatic web farm (see Starting with MVC development). The web farm ensures that the MVC application invalidates cache according to content or setting changes made in the Kentico application and vice versa.
Licensing of the web farm servers works automatically for basic scenarios – see Kentico licensing for MVC applications for details.
If you wish to use a web farm to scale the site’s performance, you can add further instances of the MVC application. We recommend using the following process:
- Develop and test the site in a web farm with two servers (one MVC application, one Kentico application).
- Deploy any number of additional instances of the same MVC application. Each instance must connect to the same Kentico database.
The automatic web farm mode automatically registers the new instances as web farm servers and ensures correct synchronization (among all instances of the MVC application and the Kentico application). One way to create a scalable website is to deploy your instance to cloud hosting.
If you need to scale the performance of the Kentico administration interface used to manage the site content and settings, you can also run multiple instances of the Kentico application in the web farm. In this scenario, you need to use one of the servers as the “primary” Kentico instance (for example for holding files shared by the entire web farm, such as locally stored search indexes).
Note: If you scale out to have more than the two basic web farm servers per MVC site (live site + administration), you need to have a license that supports the additional number of web farm servers.