Understanding and Configuring the Distributed Cache in SharePoint 2013

SharePoint 2013 includes a new caching mechanism known as the Distributed Cache service. Various SharePoint features depend on this service, including micro-blogging and newsfeeds. The Distributed Cache service also improves the performance of various other features, including authentication, page load, search, OneNote client access and security trimming.

The Distributed Cache service is built on Windows Server AppFabric, that is installed as part of the prerequisites for SharePoint 2013. The service can run in one of the following two modes:
  • Collocated mode. In this mode, all the servers running the Distributed Cache service form a cache cluster. The distributed cache is then a single entity that spans each cache host. 
  • Dedicated mode. In this mode, one server runs the Distributed Cache service and no other services. This mode is sometimes appropriate in larger server farms when use of micro-blogging and feeds is extensive.

By default, the Distributed Cache service runs in collocated mode and the service is started on every SharePoint server in the server farm. The SharePoint configuration process automatically allocates 10 percent of total physical memory to the service.

Changing the allocated memory

There are two circumstances when you may need to change the memory allocated to the Distributed  Cache service:
  • If you add physical memory to the server, the Distributed Cache service will not automatically recalculate the 10 percent memory allocation. You should manually change the amount of memory allocated to the service.
  • If you run the Distributed Cache service in dedicated mode, you should reserve 2 GB of memory for system processes and services and assign the remaining memory to the Distributed Cache service.
Remember that no other SharePoint services should run on a dedicated Distributed Cache server. You can use the following procedure to change the allocated memory for the Distributed Cache service:
  1. Stop the Distributed Cache service on every cache host. You can do this from the Services on server page in the Central Administration website.
  2. Open the SharePoint 2013 Management Shell on one of the cache hosts and run the following command, where <CacheSize> is the desired memory allocation in MB.
  1. Update-SPDistributedCacheSize –CacheSizeInMB <CacheSize>  
  1. Restart the Distributed Cache service on all cache hosts. You can do this from the Services on the server page in the Central Administration website. The cache size you specify in the Update-SPDistributedCacheSize cmdlet actually represents only half the memory assigned to the Distributed Cache service. The service will use the same amount of memory again for memory management overhead. For example, if you want to allocate 8 GB of memory to the Distributed Cache service, you should specify 4000 MB (4 GB) as the cache size in the Update- SPDistributedCacheSize cmdlet.

    Adding and removing cache hosts
In some circumstances, you may need to add or remove servers from the cache cluster. For example, if you want to repair or upgrade a server in the server farm, you should remove it from the cache cluster, do your maintenance and then add it back into the cache cluster. To add a server to the cache cluster, run the following Windows PowerShell cmdlet on the server you want to add.

Adding a Cache Host to a Cache Cluster
  1. Add-SPDistributedCacheServiceInstance  
When you run this cmdlet, SharePoint will first register the Distributed Cache service on the server, so that it appears on the Services on server list in Central Administration. SharePoint will then start the service When you remove a server from the cache cluster, you must first do a graceful shutdown procedure on the server you want to remove. This graceful shutdown transfers all cached data on the server to other servers within the cache cluster. If you remove a server from the cache cluster without first doing a graceful shutdown, you are likely to lose data. To shut down a Distributed Cache service instance gracefully and then remove the server from the cache cluster, run the following Windows PowerShell cmdlets on the server you want to remove.
 
Removing a Cache Host from a Cache Cluster
  1. Stop-SPDistributedCacheServiceInstance –Graceful  
  2. Remove-SPDistributedCacheServiceInstance  
When you remove a server from a cache cluster, SharePoint stops the Distributed Cache service instance and de-registers the service, so that it no longer appears on the Services on the server list in Central Administration. You can add the server back in to the cache cluster at any time by running the
  1. Add- SPDistributedCacheServiceInstance cmdlet.