Skip to main content

Sitecore Cache Tuning

High performance for any digital platform is elementary. In this post, I will address some basics around Sitecore caching and some best practices that Sitecore developers/architects can follow to boost the performance of Sitecore solution.

While working with one of our clients, content authors started complaining about the page load time in experience editor. The environment was Azure IaaS. So, we started looking at all the performance tweaks we could apply.

Sitecore Cache Tuning

Performance and cache go hand-in-hand :D So first thing we wanted to explore was Sitecore caching especially the cache sizes. I assume the readers are already aware about Sitecore caches and how to configure them.

We used the showconfig.aspx (https://<my sitecore hostname>/sitecore/admin/showconfig.aspx) and looked especially for Sitecore's prefetch, items and data cache sizes.

So, how do we know if the cache sizes are sufficient for our application?

First, you need to open Cache Administration page (http://<my sitecore instance>/sitecore/admin/cache.aspx). This page lists the cache details in a tabular format and gives you runtime view of cache details. 

Note: On a Content Delivery server, access to the administrative pages is normally not available. Sitecore saves of copy of the cache.aspx page every ten minutes in the App_Data\diagnostics\health_monitor folder with names such as CacheStatus.20200907Z.131237Z.html where 20200907Z.131237Z is a time stamp. You can use these snapshots instead.

Cache.aspx looks like this - 


The output of cache.aspx is easy to interpret, isn't it :P

How to understand the output of cache.aspx?

The Cache Administration page reports the following details for each cache:

  1. Name — A unique string that identifies each cache. Sitecore cache works on collection. Suppose you are dealing with HashTable where you have Key/Value Pair. Where Key is an identifier and Value is an Object. This name indicates Key.
  2. Count — The number of items in the cache.
  3. Size — The approximate current size of the cache.
  4. Delta — The approximate change in the size of the cache since the last refresh of the cache administration page. In Bytes. Open page first time and then click on refresh [By clicking on Refresh button at top]. If Delta value goes in negative then it means that Sitecore removed some values from Cache entries to allocate another cache values. It indicates your Cache Size for that key is not enough. Will discuss more on this in a while.
  5. MaxSize — The approximate limit to the size of the cache. Comes from your Caching Configuration.

This can be better understood by opening the Cache Administration page on your local Sitecore instance. So, as per Sitecore Caching guide, below are the indicators that tell you to increase cache size:

  • If the size of the cache is consistently more than 80% of the maximum cache size, increase the size of the cache by 25%.
  • If the cache Delta fluctuates significantly, increase the size of the cache by 25%.

Lets try to understand this by example. A typical entry in cache.aspx for prefetch cache for master database looks like -

SqlDataProvider - Prefetch data(master)1042191.8 MB43 bytes200 MB

This entry on cache.aspx page tells us - 

  • Name Of Cache = SqlDataProvider – Prefetch data(master)
  • Count (number of items in cache) = 1042
  • Size  = 191.8 MB
  • Delta = 43 bytes
  • Max Size = 200 MB

We can see Size of the cache is about to limit of 200 MB. 191.8/200 = 96% of max size.

This means that current cache size is not enough to handle all the data (96%>80%). The guide recommends to increase size by 25% i.e. to 250 MB

* If Delta goes to minus, this means that Sitecore wants to put fresh data into the cache, but it can’t because of limited cache size. So Sitecore deletes some data from cache and puts new data. Negative Delta value is an indicator to increase cache size.

How to know proper cache sizes?

  • Run a load test using a tool (Gatling or JMeter) or by asking multiple users to use most important pages of your application simultaneously.
    • If you are tuning caches for Content Delivery, use your load test to simulate visits to the published site.
    • If you are tuning caches for Content Management, use your load test to simulate visits to the Experience Editor and Content Editor. Ideally, your test should include expanding the Content Tree.
  • Open the Cache Administration Page of your Sitecore instance and keep refreshing it by clicking Refresh button. You shall see the number of items and cache size growing as the number of users increase. 
  • Identify some of the most important caches that need to be configured properly - 
    • While tuning the Content Delivery environment, pay particular attention to the following caches:
      • web[data] (Data cache)
      • web[item] (Item cache)
      • SqlDataProvider- Prefetch data(web) (Prefetch cache) 
    • When tuning the Content Management environment, pay particular attention to the following caches :
      • master[data] (Data cache)
      • master[item] (Item cache)
      • SqlDataProvider- Prefetch data(master) (Prefetch cache)
  • If the Delta for a cache fluctuates constantly, or if the size of the cache is consistently above 80% of the size limit, then increase the size of the cache by at least 25%.
  • If the size of the cache remains less than 50% of the MaxSize, decrease the size of the cache by 25%.
  • Repeat the previous step until the caches are stable. A cache is considered stable when:
    • The cache size is between 70% and 80%.
    • The Delta value remains relatively consistent.

Your configuration patch shall look like - 

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <databases>
      <!-- Core DB -->    
      <database id="core">
        <cacheSizes hint="setting">
          <data>100MB</data>
          <items>50MB</items>
          <paths>1MB</paths>
          <itempaths>20MB</itempaths>
          <standardValues>1MB</standardValues>
        </cacheSizes>
      </database>
      <!-- Master DB -->
      <database id="master">
        <cacheSizes hint="setting">
          <data>1GB</data>
          <items>500MB</items>
          <paths>10MB</paths>
          <itempaths>50MB</itempaths>
          <standardValues>10MB</standardValues>
        </cacheSizes>
      </database>
      <!-- Web DB -->
      <database id="web">
        <cacheSizes hint="setting">
          <data>2GB</data>
          <items>1GB</items>
          <paths>10MB</paths>
          <itempaths>50MB</itempaths>
          <standardValues>10MB</standardValues>
        </cacheSizes>
      </database>
    </databases>
  </sitecore>
</configuration>

Few points to be noted :

  • If you increase prefetch cache heavily, it may affect your Sitecore startup time. But, it  will make application smoother after first start.
  • Take care that large caches that consume all memory of the application can lead to out of memory exception. When the memory consumed by all application pools exceeds available memory on the system, Windows swaps memory to disk and can raise out-of-memory and other errors. Paging can adversely affect performance. Tune cache size limits to prevent paging and out-of-memory conditions. 
After setting up the patch file for the caches size, you shall see improvement in your application's performance just as in our case. But we are not done yet ;)

Analyze requests on page load

Another step we took was to analyze the requests made by page during page load. This was done by - 

  1. Open Developer tools in your browser (hit F12 in Chrome)
  2. Goto Network Tab (make sure to enable preserve log in Network tab to capture all the requests)
  3. Refresh the application page in the browser.
  4. Take note of all the requests that take time in seconds (not ms) to load (many requests will appear as pending requests)
Some of these requests in our applications were related to custom logic applied on page load or item save events (in experience editors). One should review such logics to tradeoff between the value these logics bring in comparison to performance hit the application takes. We refined some of these custom logics to make our page loads faster.

Sitecore Debugger
This is another important tool which will breakup your page load performance rendering-wise and will upfront serve you with a trace of page load. Give it a try.

Sitecore Debugger

What additional steps do you take to improve your application performance? Let me know in commnets.

Popular posts from this blog

Sitecore PowerShell Script to create all language versions for an item from en version

  We have lots of media items and our business wants to copy the data from en version of media item to all other language versions defined in System/Languages. This ensures that media is available in all the languages. So, we created the below powershell script to achieve the same -  #Get all language versions defined in System/Languages $languages = Get-ChildItem /sitecore/System/Languages -recurse | Select $_.name | Where-Object {$_.name -ne "en"} | Select Name #Ensuring correct items are updated by comparing the template ID  $items = Get-ChildItem -Path "/sitecore/media library/MyProjects" -Recurse | Where-Object {'<media item template id>' -contains $_.TemplateID} #Bulk update context to improve performance New-UsingBlock (New-Object Sitecore.Data.BulkUpdateContext) { foreach($item in $items){    foreach($language in $languages){ $languageVersion = Get-Item -Path $item.Paths.Path -Language $language.Name #Check if language versi

Export Sitecore media library files to zip using SPE

If you ever require to export Sitecore media files to zip (may be to optimize them), SPE (Sitecore Powershell Extension) has probably the easiest way to do this for you. It's as easy as the below 3 steps -  1. Right click on your folder (icons folder in snap)>Click on Scripts> Click on Download 2. SPE will start zipping all the media files placed within this folder. 3. Once zipping is done, you will see the Download option in the next screen. Click Download Zip containing the media files within is available on your local machine. You can play around with the images now. Hope this helps!! Like and Share ;)

Make Sitecore instance faster using Roslyn Compiler

When we install the Sitecore instance on local, the first load is slow. After each code deploy also, it takes a while for the Sitecore instance to load and experience editor to come up. For us, the load time for Sitecore instance on local machines was around 4 minutes. We started looking for ways to minimize it and found that if we update our Web.config to use Roslyn compiler and include the relevant Nugets into the project, our load times will improve. We followed the simple steps - Go to the Project you wish to add the NuGet package and right click the project and click 'Manage NuGet Packages'. Make sure your 'Package Source' is set to nuget.org and go to the 'Browse' Tab and search Microsoft.CodeDom.Providers.DotNetCompilerPlatform. Install whichever version you desire, make sure you note which version you installed. You can learn more about it  here . After installation, deploy your project, make sure the Microsoft.CodeDom.Providers.DotNetCompilerPlatform.d

Experience of a first time Sitecore MVP

The Journey I have been working in Sitecore for almost 10 years now. When I was a beginner in Sitecore, I was highly impressed by the incredible community support. In fact, my initial Sitecore learning path was entirely based on community written blogs on Sitecore. During a discussion with my then technology lead Neeraj Gulia , he proposed the idea that I should start giving back to developer community whenever I get chance. Just like I have been helped by many developers via online blogs, stackoverflow etc., I should also try to help others. Fast forward a few years and I met  Nehemiah Jeyakumar  (now an MVP). He had a big archive of his technical notes in the form Sitecore blogs. I realized my first blog dont have to be perfect and it can be as simple as notes to a specific problem for reference in future. That's when I probably created my first blog post on Sitecore. At that time, I didn't knew about the Sitecore MVP program. Over the years, I gained more confidence to write

Clean Coding Principles in CSharp

A code shall be easy to read and understand. In this post, I am outlining basic principles  about clean coding after researching through expert recommended books, trainings and based on my experience. A common example to start with is a variable declaration like - int i  The above statement did not clarify the purpose of variable i. However,  the same variable can be declared as -  int pageNumber The moment we declared the variable as int pageNumber, our brain realized that the variable is going to store the value for number of pages. We have set the context in our brain now and it is ready to understand what the code is going to do next with these page numbers. This is one of the basic advantages of clean coding. Reasons for clean coding -  • Reading clean code is easier - Every code is revisited after certain amount of time either by the same or different developer who created it. In both the cases, if the code is unclean, its difficult to understand and update it. • To avoid s