Friday, July 1, 2011

Using of ASP.Net Cache in Sharepoint farm environment

Often in order to improve performance of Sharepoint web application developers use standard ASP.Net
in-memory Cache (see HttpContext.Cache). As cached objects reside in memory of appropriate AppPool
it works very fast. However you should be aware about potential problem with using of ASP.Net of
multiple web front ends environment. In Sharepoint world farms are often used in production. At the
same time single-server environment is commonly used for development. The problem that in this case
behavior of the ASP.Net cache may differ from your expectations.

As I already said cached objects are stored in the memory of AppPool inside w3wp process. If you have
multiple WFEs – each will have its own IIS installed and w3wp process. So there will be several different
unsynchronized caches on different WFEs.

Consider the following scenario. Suppose that we have a farm with 2 WFEs:

  1. User makes request and NLB routes it to WFE1
  2. Some code is executed on WFE1 and result stored in the cache (with specified expiration life time)
  3. After that the same user makes the same request and NLB now routes it to WFE2
  4. As WFE2 doesn’t has access to the cache of WFE1 – the code is executed again and result is
    stored to the cache of WFE2. This result can differ from the data which is stored in the cache of
    WFE1
  5. We have unsynchronized cache on both WFEs. It means that user may see different information
    based on NLB routing

In order to avoid such situations you need to use distributed cache system:

  1. memcached
  2. NCache
  3. Windows Server AppFabric cache (Velocity)

In this case you will avoid problems with unsynchronized caches.

No comments:

Post a Comment