---
title: Optimizing performance of MVC sites
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

This page contains recommendations that can help you optimize the performance of your MVC site.

> **Info:** **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](https://docs.kentico.com/k12sp/on-line-marketing-features/configuring-and-customizing-your-on-line-marketing-features/best-practices-for-ems-performance-in-mvc.md) 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](https://docs.kentico.com/k12sp/developing-websites/retrieving-content-in-mvc-applications.md) 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](https://docs.kentico.com/k12sp/custom-development/working-with-pages-in-the-api.md) or [ObjectQuery](https://docs.kentico.com/k12sp/custom-development/retrieving-database-data-using-objectquery-api.md) API (for example via [generated providers](https://docs.kentico.com/k12sp/developing-websites/generating-classes-for-kentico-objects.md) 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.

```csharp title="Example"

// 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:** **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](https://docs.kentico.com/k12sp/configuring-kentico/configuring-caching/caching-on-mvc-sites.md) for more information.
- To avoid displaying of outdated content, set up [cache dependencies](https://docs.kentico.com/k12sp/configuring-kentico/configuring-caching/setting-cache-dependencies.md).
- 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](https://docs.microsoft.com/en-us/iis/extensions/iis-compression/iis-compression-overview) 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:

1. [Install](https://docs.microsoft.com/en-us/iis/configuration/system.webserver/urlcompression) the required compression modules.
2. 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_.

> **Note:** **_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](https://docs.kentico.com/k12sp/configuring-kentico/setting-up-web-farms.md).

> **Info:** With the [MVC development model](https://docs.kentico.com/k12sp/developing-websites/mvc-development-overview.md), your MVC application and Kentico application should already be configured to run as servers in an automatic web farm (see [Starting with MVC development](https://docs.kentico.com/k12sp/developing-websites/starting-with-mvc-development.md)). 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](https://docs.kentico.com/k12sp/configuring-kentico/managing-sites/managing-site-licenses/kentico-licensing-for-mvc-applications.md) 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:

1. Develop and test the site in a web farm with two servers (one MVC application, one Kentico application).
2. 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](https://docs.kentico.com/k12sp/deploying-websites/running-kentico-on-microsoft-azure.md).

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](https://docs.kentico.com/k12sp/configuring-kentico/setting-up-search-on-your-website/using-locally-stored-search-indexes.md)).

> **Note:** **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.
