Use Distributed Caching to Speed-Up Session Management in Your ASP.NET Application
page 1 of 1
Published: 21 Nov 2007
Company Logo

This is a whitepaper provided by one of our AspAlliance sponsors. These whitepapers are intended to provide you with information on products and services that we consider useful and of value to developers.

ASP.NET’s out-of-process session-state management can bog down performance when used on web farms. A distributed caching solution can help increase performance when an application's session grows too large. For a faster ASP.NET application running in a server farm, try using distributed caching APIs instead of standard ASP.NET session state to store session data.
by ScaleOut Software
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 8251/ 11


You may have noticed that ASP.NET’s out-of-process session-state management on Web farms can start to bog down performance when your application’s session objects grow large (say, 100KB or more). The reason for this is that ASP.NET has to read and write the entire session object on every Web hit in which even a small portion of the object may have changed or if the ASP.NET runtime cannot be certain that a complex object is unchanged. Many applications create session objects that are updated relatively infrequently. For example, a session object might contain a shopping cart that only changes when the user adds a new item to the cart. Updating these objects on every Web hit is unnecessary and can lead to a substantial slowdown in performance.

If you use a distributed caching product, such as ScaleOut StateServer from ScaleOut Software, you can selectively update cached objects only when there is a change to the data. In case you’re not familiar with distributed caching, this middleware software technology stores objects in-memory on your Web farm and makes them available to all servers. It speeds up session-state access by scaling its storage capacity and throughput as your Web farm grows. There’s a big win in doing this because you can lower response time while offloading your database server. Moreover, you can instantly plug the distributed cache into ASP.NET’s runtime and automatically manage session-state storage.

But there’s no need to stop there. With distributed caching, you can pull selected objects out of session-state and store them directly in the distributed cache using the cache’s APIs. This lets you update the cache only when an object changes instead of on every Web hit as ASP.NET does with session-state. This fine-grained control helps you tune cache access to reduce unnecessary overhead and maximize performance.

Sample Code

To see how easy it is to manage session-data in a distributed cache, let’s look at a simple C# code sample. Say that you need to create a shopping cart as a hash table which stores items for purchase:

Listing 1 – Create shopping cart and hash table

Hashtable shoppingCart = new Hashtable();

shoppingCart ["item1"] = "basketball";

shoppingCart ["item2"] = "tennis racket";


At the end of a Web hit that creates a new shopping cart, you just save it in the distributed cache using the APIs. Each cached object has an associated key, which can be generated from the session id or from a user’s login name. ScaleOut StateServer’s APIs use a helper object called a CacheDataAccessor that keeps track of the key and maintains a connection to the cache during each Web hit:

Listing 2 – Save shopping cart

CacheDataAccessor cda = new CacheDataAccessor(key);

// add the data to the SOSS store for 20 minutes and

// update the object if it already exists:

cda.Add(shoppingCart, 20, true);


At the beginning of each subsequent Web hit, your application can read in the shopping cart object and update it only when it changes:

Listing 3 – Read and update shopping cart

CacheDataAccessor cda = new CacheDataAccessor(key);
// retrieve the object from the cache and lock it for exclusive use:
Hashtable shoppingCart = (Hashtable)cda.Retrieve(true);
if (cartChanged)
      // update the cache and release the lock
      // just unlock the object in the cache


Note that the APIs lock the object while it is being accessed so that all Web hits see a consistent view of the object’s data. This is important in case multiple hits access the same object and are simultaneously handled by different servers.

Wrap Up

This example shows how easy it is to directly manage cached objects within your Web application. Using the distributed cache’s APIs instead of storing data within ASP.NET session objects lets you optimize your application’s performance so that users experience the fastest possible response times. Distributed caching’s unique combination of scalable throughput and fast, flexible access gives your Web site a double performance gain that your users will appreciate.

About ScaleOut Software

ScaleOut Software develops software products that provide scalable, highly available distributed caching for workload data in server farms and grids. It has offices in Bellevue Washington and Beaverton, Oregon. The company was founded by Dr. William L. Bain, whose previous company, Valence Research, developed and distributed Web load-balancing software that was acquired by Microsoft Corporation and is now called Network Load Balancing within the Windows Server operating system.


Sales and Support


10900 NE 8th Street

15075 SW Koll Parkway

Suite 900

Suite J

Bellevue, WA 98004

Beaverton, OR 97006





User Comments

No comments posted yet.

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-07-25 1:06:02 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search