Introduction
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
cda.Update(shoppingCart);
}
else
{
// just unlock the object in the cache
cda.ReleaseLock();
}
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.