Sitecore XP behavior when MongoDB server is unavailable


Description

Sitecore Experience Platform is designed to minimize the impact of MongoDB connectivity issues on the content delivery process.

For example, the Submit Queue flushes the captured data to the local hard drive and submits it to MongoDB when the collection database comes back online. For more information about this process, see the Submit Queue Technical Details section.

However, specific MongoDB unavailability use case scenarios can still have a significant impact on your Sitecore solution. This article describes some known MongoDB issues and their recommended solutions.

Analytics API In The Custom Code

When the Analytics API is used directly in the custom code (for example, Identifying contacts in a layout) and MongoDB is not available, then exceptions are not caught by Sitecore API.

Solution: The custom code must also handle possible exceptions (for example, through try-catch) that can be thrown by the Analytics API. In addition, you should also take into account situations when xDB is completely or partially disabled.

MongoDB Performance

When a Sitecore XP site is under heavy load, the xDB reads and writes a large amount of data to the Mongo databases.

This can lead to performance issues when the MMAPv1 storage engine is used, due to locking on the database and collection level, in addition to some suboptimal query handling.

MongoDB 2.6 has locks on a per-database basis for most read-and-write operations.
MonogDB 3.0(MMAPv1) has collection-level locking. This is an improvement over earlier versions in which the most granular available lock was the database lock.

Solution: You should upgrade to the latest possible MongoDB version that is supported by your Sitecore XP version. In addition, switch to the WiredTiger storage engine whenever possible, as it has many performance improvements and document-level concurrency control. 

Page Performance During XDB Outage

Sometimes, MongoDB is available but any requests that are made to it time out. When this happens, the Analytics API and some Sitecore XP APIs wait the default 30 seconds for a connection timeout. This increases the page load time.

Solution:

Note: you can separate the parameters in a connection string using:

If one separator does not work and causes a configuration error, try using a different character.

Tracker.Current Is Not Initialized Error In Sitecore XP 7.5—8.2

Sitecore XP may sometimes throw the Tracker.Current is not initialized error for MVC pages after the MongoDB .NET Driver fails to connect to the MongoDB server.

For example:

ERROR Application error.
Exception: System.InvalidOperationException
Message: Tracker.Current is not initialized
Source: Sitecore.Analytics
   at Sitecore.Analytics.Pipelines.StartAnalytics.StartTracking.Process(PipelineArgs args)
   at (Object , Object[] )
   at Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)
   at Sitecore.Analytics.Pipelines.StartAnalytics.StartAnalyticsPipeline.Run()
   at Sitecore.Mvc.Analytics.Pipelines.MvcEvents.RequestBegin.StartTracking.Process(RequestBeginArgs args)
   at (Object , Object[] )
   at Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)
   at Sitecore.Mvc.Pipelines.PipelineService.RunPipeline[TArgs](String pipelineName, TArgs args)
   at Sitecore.Mvc.Routing.RouteHttpHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

Solution: Download and apply the following patches found on this page:

https://github.com/SitecoreSupport/Sitecore.Support.147407/releases

Tracker.Current Is Not Initialized Error In Sitecore XP 8.2 In EE

Sitecore XP throws the Tracker.Current is not initialized error in Experience Editor for MVC pages when xDB is disabled and Tracking is enabled.

Exception example:

ERROR Application error.
Exception: System.InvalidOperationException
Message: Tracker.Current is not initialized
Source: Sitecore.Analytics
   at Sitecore.Analytics.Pipelines.StartAnalytics.StartTracking.Process(PipelineArgs args)
   at (Object , Object[] )
   at Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)
   at Sitecore.Pipelines.DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args, Boolean failIfNotExists)
   at Sitecore.Analytics.Pipelines.StartAnalytics.StartAnalyticsPipeline.Run()
   at Sitecore.Mvc.Analytics.Pipelines.MvcEvents.RequestBegin.StartTracking.Process(RequestBeginArgs args)
   at (Object , Object[] )
   at Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)
   at Sitecore.Pipelines.DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args, String pipelineDomain)
   at Sitecore.Mvc.Pipelines.PipelineService.RunPipeline[TArgs](String pipelineName, TArgs args)
   at Sitecore.Mvc.Routing.RouteHttpHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

Solution: Download and apply the following patch:

https://github.com/SitecoreSupport/Sitecore.Support.122492/releases

Submit Queue Technical Details

When MongoDB is not available, Sitecore XP still collects analytics data and stores the data to the file system until the collection server comes back online.

There is a key in the \App_Config\Include\Sitecore.Analytics.config file that controls Sitecore XP behavior when MongoDB is unavailable:

<!-- ANALYTICS FAIL ON DATABASE ERRORS 
Note: This is a diagnostics setting that should not be enabled on a live system. 
If set to true, web pages crash and show exception details in case 
an operation cannot access the database. 
If set to false, exceptions are suppressed and logged, and visit data 
is queued until the database is back up. See <submitQueue> section. 
Default: false 
-->
<setting name="Analytics.FailOnDatabaseErrors" value="false" />

The setting is used in the <mongo> section in the \App_Config\Include\Sitecore.Analytics.MongoDb.config file:

<mongo recoveryTimeout="60"> 
     <driver type="Sitecore.Analytics.Data.DataAccess.MongoDb.MongoDbDriver, Sitecore.Analytics.MongoDB"> 
         <param desc="connectionString">$(0)</param> 
         <param desc="failOnReadErrors" ref="settings/setting[@name='Analytics.FailOnDatabaseErrors']/@value" /> 
     </driver>
</mongo>

Starting from Sitecore XP 8.1, the setting was deprecated. You can now control this directly in the <mongo> section:

<mongo recoveryTimeout="60">
      <driver type="Sitecore.Analytics.Data.DataAccess.MongoDb.MongoDbDriver, Sitecore.Analytics.MongoDB">
        <param desc="connectionString">$(0)</param>
        <param desc="failOnReadErrors">false</param>
      </driver>
</mongo>

In addition, starting from the Sitecore XP 8.1, you can enable or disable the Submit Queue in the \App_Config\Include\Sitecore.Analytics.Tracking.config file:

<!-- ANALYTICS USE SUBMIT QUEUE 
Specifies if the submit queue should be used when the contact or session submit operations cannot access the database. 
If set to true, the contact or session is queued until the database is backed up (see the <submitQueue> section). 
Default: true 
--> 
<setting name="Analytics.UseSubmitQueue" value="true" />

You configure the background task that flushes the collected data to MongoDB in the \App_Config\Include\Sitecore.Analytics.Tracking.Database.config file:

<submitQueue> 
    <backgroundService type="Sitecore.Analytics.SubmitQueueService, Sitecore.Analytics"> 
       <!-- Service wakeup interval in seconds. --> 
       <Interval>60</Interval> 
    </backgroundService> 
</submitQueue>