# Optimize for performance

## Default optimizations

The .NET Core Renderer application communicates with the CMS through HTTP calls over the `IRestClient` interface.

Sitefinity has the following calls optimized out-of-the-box:

* The web service responses from coming from Sitefinity CMS are cached.
* The Renderer leverages its own internal per-request cache.\
  This way, it does not send duplicate CMS requests during the execution of a single page render request.
* For faster communication, the `IRestClient` uses the HTTP 2 protocol underneath.
* The Renderer uses page-level caching to cash the entire page.\
  For more information, see [Configure output cache](https://www.progress.com/documentation/sitefinity-cms/configure-output-cache).

## Recommended optimizations

In addition to the above optimizations, you can further optimize the requests to Sitefinity CMS in the following ways:

* Fetch only the data that you need.\
  When fetching a collection of items using `IRestClient.GetItems`, make sure to pass a filter, `Skip` and `Take`, and `Fields` as parameters to fetch the minimum amount of data that is needed. \
  This is useful, because when the response comes in the HTTP protocol, it is in text format (`JSON`) and can get big, if the amount of data requested is large.

* Avoid N+1 requests. \
  For example, if you fetch the items for a given collection of taxons, instead of making a request to the CMS for every taxon, consider combining them into a single request. 

  > success  **EXAMPLE**: You can see examples at Sitefinity GitHub Repository » [Filter items](https://github.com/Sitefinity/sitefinity-aspnetcore-mvc-samples/blob/master/RestSDK.md#filtering-items).

* To check how many requests each page makes and which `ViewComponent` is making them, you can use page diagnostics.\
  For more information, see [Diagnostics and Troubleshooting](https://www.progress.com/documentation/sitefinity-cms/diagnostics-and-troubleshooting) » *Page diagnostics*.\
  Long running and bulky requests are problematic and you should avoided them, if possible.

* Sitefinity CMS and the Renderer must be closely deployed - in the same network.\
  This way, you achieve a minimum roundtrip time for each request.

* To fetch a collection of items for multiple `MixedContentContext` objects, use the helper methods in the namespace `Progress.Sitefinity.AspNetCore.RestSdk`.

  > success **EXAMPLE**: You can see examples at Sitefinity GitHub Repository » [SelectorDemoUsageViewComponent.cs](https://github.com/Sitefinity/sitefinity-aspnetcore-mvc-samples/blob/master/src/content-selectors/ViewComponents/SelectorDemoUsageViewComponent.cs#L47).

* Use `IHttpClientFactory` for making requests to external services. \
  Using this interface is recommended by Microsoft, because the `IHttpClientFactory` reuses the same underlying TCP connection and automatically opens a new connection, if there is the need to. \
  Using `HttpClient` and disposing it puts the underlying connection in a `TIME_WAIT` state, therefore blocking it for use.

* When possible, use HTTP2 protocol when making web requests.
