Manage the Distributed Cache service in SharePoint Server

SharePoint Server 2016
 

Applies to: SharePoint Server 2013, SharePoint Server 2016

Topic Last Modified: 2017-08-03

Summary:  Learn how to configure and manage the Distributed Cache service in SharePoint Server 2013 and SharePoint Server 2016.

To perform management and operational tasks on the Distributed Cache service in SharePoint Server, an administrator must perform specific, ordered procedures. This article describes how to conduct several management and operational tasks on the Distributed Cache service.

ImportantImportant:
The Distributed Cache service can end up in a nonfunctioning or unrecoverable state if you do not follow the procedures that are listed in this article. In extreme scenarios, you might have to rebuild the server farm. The Distributed Cache depends on Windows Server AppFabric as a prerequisite. Do not administer the AppFabric Caching Service from the Services window in Administrative Tools in Control Panel. Do not use the applications in the folder named AppFabric for Windows Server on the Start menu.
ImportantImportant:
Do not use service account names that contain the symbol $.

In this article:

An administrator that performs maintenance and operational tasks might need to start and stop the Distributed Cache service. Some of these tasks include the following:

  • Changing the default configuration of the server farm at installation time. The Distributed Cache service is started on all SharePoint servers at installation time. An administrator might want to stop the Distributed Cache service on some servers in the farm.

  • Updating the server and there is only one Distributed Cache server in the SharePoint Server farm.

Stopping the cache results in partial data loss. The Feed Cache depends on the Distributed Cache service. Tags and document activities are saved only to the Feed Cache. Tags and document activities are not persisted to content databases. When the Distributed Cache service is stopped, tags and document activities are lost. When the Distributed Cache service is started, repopulation occurs when the feed cache repopulation timer job runs. One way to maintain the tags and document activities is to use the method described in Graceful shutdown of the Distributed Cache service later in this article. When the graceful shutdown of the Distributed Cache service method is used, all cache data is moved from one server to another server before the Distributed Cache service is stopped.

NoteNote:
If your cache hosts are part of a cache cluster, do not start or stop the Distributed Cache service as described here. Instead, see Adding or removing a server in a Distributed Cache cluster later in this article.

  1. In Central Administration, click Application Management.

  2. In Service Applications, click Manage Services on Server.

  3. On the Services on Server page, locate the Distributed Cache service.

  4. If the Distributed Cache service is started and you want to stop the service, under Action, click Stop. If the Distributed Cache service is stopped and you want to start the service, under Action, click Start.

At the SharePoint Management Shell command prompt, run the following command:

$instanceName ="SPDistributedCacheService Name=AppFabricCachingService"
$serviceInstance = Get-SPServiceInstance | ? {($_.service.tostring()) -eq $instanceName -and ($_.server.name) -eq $env:computername}
$serviceInstance.Provision()

At the SharePoint Management Shell command prompt, run the following command:

$instanceName ="SPDistributedCacheService Name=AppFabricCachingService"
$serviceInstance = Get-SPServiceInstance | ? {($_.service.tostring()) -eq $instanceName -and ($_.server.name) -eq $env:computername}
$serviceInstance.Unprovision()

When SharePoint Server is installed, it assigns the Distributed Cache service 10 percent of the total physical memory on the server. The Distributed Cache service uses half of that memory allocation for data storage (also known as cache size), and the other half of that memory allocation is used for memory management overhead. When the cached data grows, the Distributed Cache service uses the entire 10 percent of the allocated memory.

You should increase the memory allocation of the Distributed Cache service in these scenarios:

  • When you add physical memory to the server. The Distributed Cache service does not automatically recalculate the 10% memory allocation, so when you increase the total physical memory on the server, you have to manually increase the Distributed Cache service's memory allocation.

  • When your server farm has a dedicated Distributed Cache server. Use the following method to calculate how much memory can be assigned to the Distributed Cache service:

    1. Determine the total physical memory on the server. For this example, we will use 16 GB as the total physical memory available on the server.

    2. Reserve 2 GB of memory for other processes and services that are running on the cache host. For example, 16 GB – 2 GB = 14 GB. This remaining memory is allocated to the Distributed Cache service.

    3. Take half of the remaining memory, and convert it to MB. For example, 14 GB/2 = 7 GB or 7168 MB. This is the cache size of the Distributed Cache service.

    4. Use the following procedure to update the memory allocation accordingly.

Use this procedure to reconfigure the memory allocation of the cache size of the Distributed Cache service.

  1. (Optional) To check the existing memory allocation for the Distributed Cache service on a server, run the following command at the SharePoint Management Shell command prompt:

    Use-CacheCluster
    Get-AFCacheHostConfiguration -ComputerName ComputerName -CachePort "22233"
    
    

    Where:

    • ComputerName is the computer name of the server that you are running the SharePoint Management Shell cmdlet on.

  2. Stop the Distributed Cache service on all cache hosts. To stop the Distributed Cache service, go to Services on Server in Central Administration, and Stop the Distributed Cache service on all cache hosts in the farm.

  3. To reconfigure the cache size of the Distributed Cache service, run the following command one time only on any cache host at the SharePoint Management Shell command prompt:

    Update-SPDistributedCacheSize -CacheSizeInMB CacheSize
    

    Where:

    • CacheSize is the cache size's memory allocation assignment in MB. In the previous example, the cache size was calculated at 7168 MB for a server with 16 GB of total memory.

  4. Restart the Distributed Cache service on all cache hosts. To restart the Distributed Cache service, go to Services on Server in Central Administration, and Start the Distributed Cache service on all cache hosts in the farm.

An administrator can add or remove a server to a cache cluster, or might want to remove a server from the cache cluster, perform some operational or maintenance tasks on the server, and then rejoin or add the server to the cache cluster. When removing the server, the Distributed Cache service is stopped, then unregistered from the server. Unregistering the Distributed Cache service means that an administrator will not see the Distributed Cache service listed on the Services on Server page in Central Administration. Similarly, when a server is added, the Distributed Cache service is registered and then is started on the server. Registering the Distributed Cache service means that an administrator will see the Distributed Cache service listed on the Services on Server page in Central Administration.

Use the following procedures to add and remove a server from a cache cluster. These SharePoint Management Shell cmdlets are run on the server being added or removed.

NoteNote:
Before performing the following procedures, ensure the firewall allows Inbound ICMP (ICMPv4) traffic through it. For more information, see Firewall configuration considerations.

At the SharePoint Management Shell command prompt, run the following command:

Add-SPDistributedCacheServiceInstance

At the SharePoint Management Shell command prompt, run the following command:

Remove-SPDistributedCacheServiceInstance

ImportantImportant:
This procedure will stop the cache service and nonpersisted cached data will be lost. If you want to keep the cached data, use the graceful shutdown procedure that is described in the next section.

In a SharePoint Server 2016 farm, a cache cluster exists when one or more cache hosts run the Distributed Cache service. In a SharePoint Server 2016 farm, one cache exists, and the cache spans the cache cluster. An administrator may need to move the cached contents to another cache host when applying updates to the server. To prevent data loss associated with moving the cached contents you need to perform a graceful shutdown of the server using the PowerShell script in the following procedure. The graceful shutdown procedure transfers all cached data from the cache host on which the graceful shutdown procedure is being run on to another cache host in the farm. The transfer process takes 15 minutes or more to run depending on how many items exist in the cache.

Use the following PowerShell script to perform a graceful shutdown of the Distributed Cache server in order to move the cached contents to another cache host. Ensure that you specify the correct node to shutdown and change the script as needed to name the correct parameters for your organization.

NoteNote:
There is no need to remove the cache host from a cache cluster if you use the PowerShell script in the following procedure to perform a graceful shutdown.
  1. Verify that you meet the following minimum requirements:

  2. Copy the following variable declarations, and paste them into a text editor such as Notepad. Set parameter values specific to your organization. Save the file, and name it GracefulShutdown.ps1.

    NoteNote:
    You can use a different file name, but you must save the file as an ANSI-encoded text file with the extension .ps1.
    ## Settings you may want to change for your scenario ##
    $startTime = Get-Date
    $currentTime = $startTime
    $elapsedTime = $currentTime - $startTime
    $timeOut = 900
    
    try
    {
        Write-Host "Shutting down distributed cache host."
     $hostInfo = Stop-CacheHost -Graceful -CachePort 22233 -ComputerName sp2016App.contoso.com
    
     while($elapsedTime.TotalSeconds -le $timeOut-and $hostInfo.Status -ne 'Down')
     {
         Write-Host "Host Status : [$($hostInfo.Status)]"
         Start-Sleep(5)
         $currentTime = Get-Date
         $elapsedTime = $currentTime - $startTime
         $hostInfo = Get-CacheHost -HostName SP2016app.contoso.com -CachePort 22233
     }
    
     Write-Host "Stopping distributed cache host was successful. Updating Service status in SharePoint."
     Stop-SPDistributedCacheServiceInstance
     Write-Host "To start service, please use Central Administration site."
    }
    catch [System.Exception]
    {
     Write-Host "Unable to stop cache host within 15 minutes." 
    }
    

    Where sp2016App.contoso.com is the computer domain name of Distributed Cache server you use.

  3. Open the SharePoint Management Shell

  4. Change to the directory to which you saved the file.

  5. At the PowerShell command prompt, type the following command:

    ./GracefulShutdown.ps1
    

    For additional information about PowerShell scripts and .ps1 files, see Running Windows PowerShell Scripts.

When the server farm is first configured, the server farm account is set as the service account of the AppFabric Caching service. The Distributed Cache service depends on the AppFabric Caching service. To change the service account of the AppFabric Caching service to a managed account:

  1. Create a managed account.

  2. Set the Managed account as the service account on the AppFabric Caching service. At the SharePoint Management Shell command prompt, run the following command:

    $farm = Get-SPFarm
    $cacheService = $farm.Services | where {$_.Name -eq "AppFabricCachingService"}
    $accnt = Get-SPManagedAccount -Identity domain_name\user_name
    $cacheService.ProcessIdentity.CurrentIdentityType = "SpecificUser"
    $cacheService.ProcessIdentity.ManagedAccount = $accnt
    $cacheService.ProcessIdentity.Update() 
    $cacheService.ProcessIdentity.Deploy()
    

    Where Domain_name\user_name is the domain name and user name of the managed account.

The Distributed Cache service setting for MaxConnectionsToServer is often tuned based on the number of CPUs that are used in the host computer. If, for instance you use multiple cores and then set the MaxConnectionsToServer setting to the same number of CPUs then the computer often uses too much memory and freezes. Similar issues happen when tuning the DistributedLogonTokenCache and DistributedViewStateCache settings. The default setting is 20ms but often exceptions are found when the token caching doesn't happen in the 20ms setting. Use the following PowerShell script to change the settings for max connections and timeouts.

To fine-tune the Distributed Cache service by using a PowerShell script

  1. Verify that you meet the following minimum requirements:

  2. Copy the following variable declarations, and paste them into a text editor such as Notepad. Set parameter values specific to your organization. Save the file, and name it MaxConnections.ps1.

    NoteNote:
    You can use a different file name, but you must save the file as an ANSI-encoded text file with the extension .ps1.
    Add-PSSnapin Microsoft.Sharepoint.Powershell
    
    #DistributedLogonTokenCache
    $DLTC = Get-SPDistributedCacheClientSetting -ContainerType DistributedLogonTokenCache
    $DLTC.MaxConnectionsToServer = 1
    $DLTC.requestTimeout = "3000"
    $DLTC.channelOpenTimeOut = "3000"
    Set-SPDistributedCacheClientSetting -ContainerType DistributedLogonTokenCache $DLTC
    
    #DistributedViewStateCache
    $DVSC = Get-SPDistributedCacheClientSetting -ContainerType DistributedViewStateCache
    $DVSC.MaxConnectionsToServer = 1
    $DVSC.requestTimeout = "3000"
    $DVSC.channelOpenTimeOut = "3000"
    Set-SPDistributedCacheClientSetting -ContainerType DistributedViewStateCache $DVSC
    
    #DistributedAccessCache
    $DAC = Get-SPDistributedCacheClientSetting -ContainerType DistributedAccessCache
    $DAC.MaxConnectionsToServer = 1
    $DAC.requestTimeout = "3000"
    $DAC.channelOpenTimeOut = "3000"
    Set-SPDistributedCacheClientSetting -ContainerType DistributedAccessCache $DAC
    
    #DistributedActivityFeedCache
    $DAF = Get-SPDistributedCacheClientSetting -ContainerType DistributedActivityFeedCache
    $DAF.MaxConnectionsToServer = 1
    $DAF.requestTimeout = "3000"
    $DAF.channelOpenTimeOut = "3000"
    Set-SPDistributedCacheClientSetting -ContainerType DistributedActivityFeedCache $DAF
    
    #DistributedActivityFeedLMTCache
    $DAFC = Get-SPDistributedCacheClientSetting -ContainerType DistributedActivityFeedLMTCache
    $DAFC.MaxConnectionsToServer = 1
    $DAFC.requestTimeout = "3000"
    $DAFC.channelOpenTimeOut = "3000"
    Set-SPDistributedCacheClientSetting -ContainerType DistributedActivityFeedLMTCache $DAFC
    
    #DistributedBouncerCache
    $DBC = Get-SPDistributedCacheClientSetting -ContainerType DistributedBouncerCache
    $DBC.MaxConnectionsToServer = 1
    $DBC.requestTimeout = "3000"
    $DBC.channelOpenTimeOut = "3000"
    Set-SPDistributedCacheClientSetting -ContainerType DistributedBouncerCache $DBC
    
    #DistributedDefaultCache
    $DDC = Get-SPDistributedCacheClientSetting -ContainerType DistributedDefaultCache
    $DDC.MaxConnectionsToServer = 1
    $DDC.requestTimeout = "3000"
    $DDC.channelOpenTimeOut = "3000"
    Set-SPDistributedCacheClientSetting -ContainerType DistributedDefaultCache $DDC
    
    #DistributedSearchCache
    $DSC = Get-SPDistributedCacheClientSetting -ContainerType DistributedSearchCache
    $DSC.MaxConnectionsToServer = 1
    $DSC.requestTimeout = "3000"
    $DSC.channelOpenTimeOut = "3000"
    Set-SPDistributedCacheClientSetting -ContainerType DistributedSearchCache $DSC
    
    #DistributedSecurityTrimmingCache
    $DTC = Get-SPDistributedCacheClientSetting -ContainerType DistributedSecurityTrimmingCache
    $DTC.MaxConnectionsToServer = 1
    $DTC.requestTimeout = "3000"
    $DTC.channelOpenTimeOut = "3000"
    Set-SPDistributedCacheClientSetting -ContainerType DistributedSecurityTrimmingCache $DTC
    
    #DistributedServerToAppServerAccessTokenCache
    $DSTAC = Get-SPDistributedCacheClientSetting -ContainerType DistributedServerToAppServerAccessTokenCache
    $DSTAC.MaxConnectionsToServer = 1
    $DSTAC.requestTimeout = "3000"
    $DSTAC.channelOpenTimeOut = "3000"
    Set-SPDistributedCacheClientSetting -ContainerType DistributedServerToAppServerAccessTokenCache $DSTAC
    
    #DistributedFileLockThrottlerCache
    $DFLTC = Get-SPDistributedCacheClientSetting -ContainerType DistributedFileLockThrottlerCache
    $DFLTC.MaxConnectionsToServer = 1
    $DFLTC.requestTimeout = "3000"
    $DFLTC.channelOpenTimeOut = "3000"
    Set-SPDistributedCacheClientSetting -ContainerType DistributedFileLockThrottlerCache $DFLTC
     
    #DistributedSharedWithUserCache
    $DSWUC = Get-SPDistributedCacheClientSetting -ContainerType DistributedSharedWithUserCache
    $DSWUC.MaxConnectionsToServer = 1
    $DSWUC.requestTimeout = "3000"
    $DSWUC.channelOpenTimeOut = "3000"
    Set-SPDistributedCacheClientSetting -ContainerType DistributedSharedWithUserCache $DSWUC
     
    #DistributedUnifiedGroupsCache
    $DUGC = Get-SPDistributedCacheClientSetting -ContainerType DistributedUnifiedGroupsCache
    $DUGC.MaxConnectionsToServer = 1
    $DUGC.requestTimeout = "3000"
    $DUGC.channelOpenTimeOut = "3000"
    Set-SPDistributedCacheClientSetting -ContainerType DistributedUnifiedGroupsCache $DUGC 
    
    #DistributedResourceTallyCache
    $DRTC = Get-SPDistributedCacheClientSetting -ContainerType DistributedResourceTallyCache
    $DRTC.MaxConnectionsToServer = 1
    $DRTC.requestTimeout = "3000"
    $DRTC.channelOpenTimeOut = "3000"
    Set-SPDistributedCacheClientSetting -ContainerType DistributedResourceTallyCache $DRTC
     
    #DistributedHealthScoreCache
    $DHSC = Get-SPDistributedCacheClientSetting -ContainerType DistributedHealthScoreCache
    $DHSC.MaxConnectionsToServer = 1
    $DHSC.requestTimeout = "3000"
    $DHSC.channelOpenTimeOut = "3000"
    Set-SPDistributedCacheClientSetting -ContainerType DistributedHealthScoreCache $DHSC 
    
    
    
  3. Open the SharePoint Management Shell

  4. Change to the directory to which you saved the file.

  5. At the PowerShell command prompt, type the following command:

    ./MaxConnections.ps1
    

    For more information, see Best Practices for using Windows Azure Cache/Windows Server Appfabric Cache and Application Configuration Settings (Windows Server AppFabric Caching). For additional information about Windows PowerShell scripts and .ps1 files, see Running Windows PowerShell Scripts.

During installation, configuration, or maintenance activities, the Distributed Cache service might enter a non-functioning state. Evidence of a malfunctioning Distributed Cache service will appear in Health Rules in Central Administration, or when users use features in SharePoint Server that rely on the Distributed Cache. For example, the Newsfeed on a user's My Site will start reporting errors. Also, administrators might receive the error message "cacheHostInfo is null " when they run SharePoint Management Shell cmdlets to manage the Distributed Cache service.

There are two steps to repair a cache host.

On the non-functioning Distributed Cache host, use the following procedures to restore the Distributed Cache host.

  1. At the SharePoint Management Shell command prompt, run the Remove-SPDistributedCacheServiceInstance cmdlet.

  2. At the SharePoint Management Shell command prompt, run the Add-SPDistributedCacheServiceInstance cmdlet.

    NoteNote:
    If step 1 fails, manually remove the Distributed Cache service, use the following steps.
    • At the SharePoint Management Shell command prompt, type the following syntax.

      $instanceName ="SPDistributedCacheService Name=AppFabricCachingService"
      
      $serviceInstance = Get-SPServiceInstance | ? {($_.service.tostring()) -eq $instanceName -and ($_.server.name) -eq $env:computername}
      
      If($seriveInstance -ne $null)
      {
      $serviceInstance.Delete()
      }
      
      
    • After the Distributed Cache Service has been manually deleted, run step 2 again.

Show: