Sitecore – Redis Cache For Session State (PaaS or IaaS)

Azure Redis is one of the latest offerings from Microsoft and this blog goes through using this distributed cache with Sitecore Azure. Doing this has many advantages including:

  • Session state ans Sitecore Cache to managed and monitored independently of delivery servers.
  • Session state to be scaled (horizontally) independently thus reducing costs and improving performance
  • Because Redis Cache is fast!

The first steps are to create a new Redis Cache using the Azure Preview Portal. This is well explained on the MSDN:

MSDN Creating Redis Cache . And it should look like this:

redis setup
redis-settings

The only decision to make here is on the Redis Type, basic or standard, as it’s currently free I went Standard. The difference is:

  • Basic – A single cache node (ideal for development/test and non-critical workloads)
  • Standard – A replicated cache (Two nodes, a master and a slave)

The next step is to get the right Assemblies to manage the Redis Cache. I used RedisSessionStateProvider, using the following nuget command

1
    PM> Install-Package Microsoft.Web.RedisSessionStateProvider -Pre

see this nuget command here. And then obtained the following assemblies:

  • Microsoft.Web.RedisSessionStateProvider.dll
  • StackExchange.Redis.dll

For Sitecore Infrastructure as a Servie (IaaS) on Azure

It’s as simple as swapping out the <session> type in the web.config with the Redis one created in the Preview  Portal. It should look like this:

1
2
3
4
5
6
7
8
9
10
11
        <add name="DistributedCacheSessionStateStoreProvider"
                        type="Microsoft.Web.Redis.RedisSessionStateProvider"
                        host="xyz.redis.cache.windows.net"
                        port="6379" 
                        accessKey="my-cache-key"
                        ssl="false"
                        throwOnError="true"
                        retryTimeoutInMilliseconds="20" />
 
also make sure the pDataCacheClients does not have a specific configured cache config.
ie remove it.

For Sitecore Platform as a Servie (PaaS) on Azure

We essentially need to get to the same point as the IaaS. The steps:

1. Open sitecore on the PaaS deployment / development machine
2. Setup the Editing / Delivery instances as desired – these will use the APP_Fabric (default) distributed session state provider.
3. Open the Content Editor and locate each Deployement Cloud service, ie

Sitecore -> Modules -> Azure -> project name -> Azure Location -> Editing01 -> Role01 -> Staging

4. For each Deployment Cloud Service, modify the Field named “[Do not edit!] – Global Web Config Patch (These are the global web config patches that will always be applied to every deployment) [shared]:”. And change the in there to use the Redis Cache settings:
Sitecore-Azure_PaaS-custom config

(see example below, with default settings commented out)

<!-- Distributed cache Begin --> 
  <xsl:template match="configuration/system.web/sessionState"> 
    <!--<sessionState mode="Custom" customProvider="DistributedCacheSessionStateStoreProvider"> 
      <providers> 
        <add name="DistributedCacheSessionStateStoreProvider" type="Microsoft.Web.DistributedCache.DistributedCacheSessionStateStoreProvider, Microsoft.Web.DistributedCache" cacheName="default" useBlobMode="true" dataCacheClientName="default" /> 
      </providers> 
    </sessionState>--> 
        <sessionState mode="Custom" customProvider="DistributedCacheSessionStateStoreProvider"> 
      <providers> 
        <add name="DistributedCacheSessionStateStoreProvider" 
                        type="Microsoft.Web.Redis.RedisSessionStateProvider" 
                        host="<your cache>.redis.cache.windows.net" 
                        port="6379" 
                        accessKey="<your key>" 
                        ssl="false" 
                        throwOnError="false" 
                        retryTimeoutInMilliseconds="20" /> 
      </providers> 
    </sessionState> 
  </xsl:template> 
  <xsl:template match="node()|@*" name="identity"> 
    <xsl:copy> 
      <xsl:apply-templates select="node()|@*" /> 
    </xsl:copy> 
  </xsl:template> 
  <xsl:param name="pSectionDataCacheClients"> 
    <!--<section name="dataCacheClients" type="Microsoft.ApplicationServer.Caching.DataCacheClientsSection, Microsoft.ApplicationServer.Caching.Core" allowLocation="true" allowDefinition="Everywhere" />--> 
    <section name="cacheDiagnostics" type="Microsoft.ApplicationServer.Caching.AzureCommon.DiagnosticsConfigurationSection, Microsoft.ApplicationServer.Caching.AzureCommon" allowLocation="true" allowDefinition="Everywhere" /> 
  </xsl:param> 
  <xsl:template match="configuration/configSections/section[position()=last()]"> 
    <xsl:call-template name="identity" /> 
    <xsl:copy-of select="$pSectionDataCacheClients" /> 
  </xsl:template> 
  <!--<xsl:param name="pDataCacheClients"> 
    <dataCacheClients> 
      <dataCacheClient name="default" isCompressionEnabled="false"> 
        <autoDiscover isEnabled="true" identifier="SitecoreWebRole" /> 
        <localCache isEnabled="true" sync="TimeoutBased" objectCount="100000" ttlValue="300" /> 
      </dataCacheClient> 
    </dataCacheClients> 
    <cacheDiagnostics> 
      <crashDump dumpLevel="Off" dumpStorageQuotaInMB="100" scheduledTransferPeriodInMinutes="5" /> 
    </cacheDiagnostics> 
  </xsl:param>--> 
  <xsl:template match="configuration/configSections"> 
    <xsl:call-template name="identity" /> 
    <!--<xsl:copy-of select="$pDataCacheClients" />--> 
  </xsl:template> 
  <!-- Distributed cache End -->

After all edits have been made, do an update – files. The new Redic Cache for Session state will be activated

Check out the stats:

\Sitecore-Azure-Cache-Monitor

If you get stuck. Or find the Azure Preview Pshows zero (0) hit, or no gets or sets. Then you can test the cache by:
1. Install redis.cli.exe on the IaaS or PaaS instance (VM) from
2. Connect to your Redis Cache using the command line
3. Check its working by getting all keys and checking the values!

Comments are closed.