Saturday, January 21, 2012

SharePoint 2010 AppFabric Distributed Caching & Session Management

I had an opportunity to explore a bit on AppFabric (Distributed Caching  & Session Management) back in July 2011. I wanna share what i have learned:


Overview
Caching is very beneficial for web applications that need to maintain state, especially when that state needs to be maintained in a scaled Web Farm scenario. However, there are two problems with the classic implementation. The first is that when using the local machine to cache state, you need to ensure that the client gets routed back to the same machine to get back the state (sticky routing a.k.a sticky session). If you decide to put the state in a database, you incur additional overhead and performance drag in managing this state across servers. The AppFabric cache can help to solve both these challenges.
  
App Fabric Distributed Caching & Session State
  • We are developing a very large SharePoint farm, that would be used by over 90k users,  with a lot of information, custom components and various different sub-systems .
  • We were considering to use Windows Server AppFabric Caching for distributed caching to  manage some of the not-so-frequently updated information in the Cache store instead of accessing them from database.  Though,  we were using the local machine to cache state,  but you need to ensure that the client gets routed back to the same machine to get back the state (sticky routing a.k.a sticky session).   The AppFabric distributed  Caching can help to solve this issue. 
  • Besides,  we also wanted to figure out the need to maintain  users's session(session state), especially when that state needs to be maintained in a scaled Web Farm scenario.  If you decide to put the state in a database, you incur additional overhead and performance drag in managing this state across servers. AppFabric works just fine with ASP.NET Session State and I assumed that it would work as fine as with SharePoint 2010.
Architecture
The architecture of the SharePoint application is typical of a SharePoint  web application and can be summarized as follows:


  • A database stores application data from a Content management system, containing tables with users, content, with security, and targeting information for the content.
  • Horizontally scalable web servers (WFE) perform the basic functions of authentication (Security), authorization (content-targeting), searching (content searching), etc.,. These WFEs has got custom Web Parts that are working with REST-based service layer which are hosted on these servers.
  • Each request to the web server (WFE) generates a request to the database followed by some additional processing of raw data and applying domain logic on the data-layer.
Setup and Configuration
Installing AppFabric Cache
In this task you will install and configure Windows Server AppFabric Cache.

1.       Downloaded AppFabric setup application from http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=15848
WindowsServerAppFabricSetup_x64_6.1.exe

2.       Selected and Installed Caching Services and Cache Administration.

3.       Configured Caching Service and created to New cluster with 1-5 machines . 
Using the Cache API

I have created a new named Cache, we are going to write a simple SharePoint Web Part that reads/writes data in the cache. You can of course store any serializable .NET object in the cache. To use the Cache you need to reference the Cache client assemblies.

1.       Microsoft.ApplicationServer.Caching.Core.dll is the Cache’s base library, referenced by both the Cache’s client layer and server layer. It contains things like the configuration libraries and base Cache types.

2.       Microsoft.ApplicationServer.Caching.Client.dll  contains the Cache’s client library, which is the API that connects to the Cache cluster to store and retrieve data.

AppFabric cache configuration inthe web.config file

XML
<configSections>
<section name="dataCacheClient"
 type="Microsoft.ApplicationServer.Caching.DataCacheClientSection, Microsoft.ApplicationServer.Caching.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</configSections>
<dataCacheClient>
<hosts>
<host name="hostname" cachePort="22233" />
</hosts>
</dataCacheClient>

3.  There's a public method named GetCachedName that returns a string. Inside of that method, you will get the DataCacheFactory , and then look for a key in the Lab1Cache named  

C#
public string GetCachedName()
    {
    string name = null;
    string key = "ApplicationName";
             dcf = new DataCacheFactory();
            if (dcf != null)
            {
                var cache = dcf.GetCache("Lab1Cache");
                name = cache.Get(key) as string;
                if (name == null)
                {
                    name = "Windows Server App Fabric Cache Lab";
                    cache.Put(key, name);
                }
                else
                {
                    name += " From Cache!";
                }
            }
    return name;
}
Using the AppFabric Cache ASP.NET Session State Provider in SharePoint 2010

·         If you have already used ASP.NET session state to store user activity data, you can just change the web.config sessionState binding and have the Cache take over storing ASP.NET Session State across the cache cluster. This enables you to simply add scale and performance without any code changes to your existing web applications.

1.       Execute the Get-CacheHost cmdlet to see the state of your cache cluster.
·   PowerShell

·   Get-CacheHost

You should see something that looks similar to the following Figure. Note that the Service Status is Down


1.       Use the $myhost.HostName and $myhost.PortNo properties as the input to Start-CacheHost. 

·   PowerShell

·   $myhost = Get-CacheHost

·   Start-CacheHost $myhost.HostName $myhost.PortNo

You should see something that looks similar to following Figure . Note that the Service Status is UP

·          


·        

2.       Configure the underlined SharePoint Web App to use the AppFabric Cache ASP.NET Session State Provider. Web.Config file
         XML

·            <sessionState mode="Custom" customProvider="SessionStoreProvider">

·               <providers>

·                 <add name="SessionStoreProvider" type="Microsoft.ApplicationServer.Caching.DataCacheSessionStoreProvider, Microsoft.ApplicationServer.Caching.Client, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" cacheName="Lab1Cache" />

·               </providers>

·             </sessionState>

Understand the ramifications of moving session out of process. 

Once you move session state outside of the web server process, all the objects you put in the Session need to be serializable.





 

1 comment:

  1. Hi TV,
    This is a nice article regarding integration of AppFabric with sharepoint platform. i have few queries mentioned below :-

    1)Do i require the above mentioned code if i am storing & retreiving Session data and not serializing .Net Session objects ?

    2)The ASP.Net session is enabled in Sharepoint 2007 while disabled in SP-2010. Do i have to enable the ASP.Net Session as a pre requisite?

    ReplyDelete