Drawbacks of Regular
ASP.NET Session State in
Server Farms
Although adding web servers to
the farm is a great way to scale
up ASP.NET applications, one
thing that does not scale well
is ASP.NET Session State itself.
Following are some of its
drawbacks:
-
Loss of session data
if using InProc, StateServer,
or SqlServer mode without
clustering.
-
Slow
performance
if you store session data in
SQL Server.
-
Not
scalable
if
using InProc or StateServer
modes and expensive
scalability thru SQL Server.
-
Single point of failure
if you're using InProc, StateServer,
or SqlServer without
clustering.
-
Expensive high availability
if using
SqlServer mode due to cost
of SQL Server clustering.
-
Maintenance downtime
difficult
if using InProc or
StateServer
mode.
Benefits of NCache
Clustered ASP.NET
Sessions in Server Farms
NCache lets
you transform your regular
ASP.NET sessions into clustered
sessions without any code
change. You follow a few simple
steps and your existing ASP.NET
applications can suddenly
benefit from powerful clustered
sessions. Below are some of the
benefits of using clustered
sessions:
-
No loss of session data:
You can
replicate your session data
to multiple nodes and avoid
any loss of data if a web
server crashes.
-
High performance:
Keep
your session data close-by
(either InProc or local
OutProc) on all web servers
for an extremely fast
performance. Even keeping a
separate clustered
caching-tier for session
data is much faster than
accessing SQL Server.
-
Highly scalable:
You
have a rich set of
clustering topologies
(replicated, partitioned,
and client nodes) that allow
you to scale up to 100’s of
web servers in your farm
without compromising on
performance.
-
No single point of failure:
Clustering the session
allows you to eliminate all
single points of failures
from your environment. This
makes your application
highly available.
-
Low cost high availability:
Instead
of buying expensive SQL
Server clustering, you can
cluster sessions on your web
servers without the need for
any additional expensive
hardware.
-
Easy maintenance downtime:
Clustering means that data
is not only on one server.
This allows you to schedule
down time for your servers
without any problem.
-
100% Native .NET:
NCache
is a 100% native .NET
product. This means that you
can expect us to stay on top
of all additions and changes
to .NET as compared to a
non-.NET solution.
How Does It Work?
Using NCache Clustered Sessions
is very simple. You have three
options for incorporating
session clustering into your
application. Two require no code
change and the third requires
minor code change. Here they
are:
1. Use HttpModule for ASP.NET
1.1 & 2.0 (no code change)
NCache provides an HttpModule
for ASP.NET 1.1 and 2.0
(although for 2.0 there is a
more suitable option). This
HttpModule intercepts all Http
Requests and Responses and
synchronizes the regular
SessionState with NCache. This
allows you to
incorporate
session clustering without any
code change to your ASP.NET
application.

Incorporating NCache HttpModule
into your ASP.NET is a simple
three-step process as described
below.
1.
Install NCache
on each web server
2.
Define a clustered cache:
Use NCache Manager (a graphical
tool) to define a clustered
cache that is appropriate for
you. You should either select replicated cache
or partitioned
cache with backup nodes. And, you can
also specify whether the cache
should reside on each web server
or whether you should have a
separate caching tier (again a
cluster of multiple servers)
where all the session cache is
kept. For larger configurations,
a separate caching tier is
sometime more appropriate.
3. Modify web.config
file
to add the
HttpModule
information
and the name of the cache you’ve
just created. NCache provides a
sample project called
guessgamehttpmodule
that demonstrates how to use
HttpModule
option for sessions. Here is a
sample
web.config:
<appSettings>
<add
key="CacheName"
value="myReplicatedCache"/>
</appSettings>
<httpModules>
<add
name="NCacheWebSessionState"
type="Alachisoft.NCache.Web.SessionState.NSessionStateModule,
Alachisoft.NCache.SessionState,
Version=3.0.0.0,
Culture=neutral,
PublicKeyToken=cff5926ed6a53769"/>
</httpModules>
Once you do this, you’re ASP.NET
application is ready to start
using clustered sessions.
2. Use SessionState Provider in ASP.NET 2.0 (no code change)

For ASP.NET 2.0, NCache also
provides a custom SessionState
Provider that synchronizes your
regular ASP.NET Session with
NCache. This allows you to
use
session clustering without any
code change. Just like the HttpModule option, this is also
a simple three-step process as
described below.
1. Install NCache
on each web server
2.
Define a clustered cache:
Use NCache Manager (a graphical
tool) to define a clustered
cache that is appropriate for
you. You should either select replicated cache
or partitioned
cache with backup nodes. And, you can
also specify whether the cache
should reside on each web server
or whether you should have a
separate caching tier (again a
cluster of multiple servers)
where all the session cache is
kept. For larger configurations,
a separate caching tier is
sometime more appropriate.
3.
Modify
web.config
file:
to add the
SessionState
Provider
information and the name of the
cache you’ve just created.
NCache provides a sample project
called
guessgamesessionstoreprovider
that demonstrates how to use
SessionState
Provider
option for sessions.
<sessionState
cookieless="false"
regenerateExpiredSessionId="true"
mode="Custom"
customProvider="NCacheSessionProvider"
timeout="1">
<providers>
<add
name="NCacheSessionProvider"
type="Alachisoft.NCache.Web.SessionState.NSessionStoreProvider"
cacheName="myreplicatedcache"
writeExceptionsToEventLog="false"
AsyncSession="false"/>
</providers>
</sessionState>
Once you do this, you’re ASP.NET
application is ready to start
using clustered sessions.
3. Use NCache Session Object
(minor code change)
In most cases, using either
HttpModule or SessionState
Provider is the better choice
because it has a more optimal
performance. However, if your
session is very large, contains
numerous items, and your ASP.NET
page changes only a few of them,
then it might be more efficient
for you to use NCache Session
Object. This session object then
updates the cache with only those items in the cache
that you have modified instead
of copying the entire session
into the cache.
To use this object, you need to
follow these steps:
-
Install NCache
on each web server
-
Define a clustered cache:
Use NCache Manager (a
graphical tool) to define a
clustered cache that is
appropriate for you. You
have the option of
replicated cache,
partitioned cache, and
partitioned cache with
backup nodes. And, you can
also specify whether the
cache should reside on each
web server or whether you
should have a separate
caching tier (again a
cluster of multiple servers)
where all the session data
is kept. For larger
configurations, a separate
caching tier is sometime
more appropriate.
-
Modify
each ASP.NET file:
The changes are very minor
but must be made to each
ASP.NET file so it starts
using NCache Session Object
instead of the regular
ASP.NET Session object. The
changes are:
-
Include Alachisoft
namespace as following:
i.
using
Alachisoft.NCache.Web.UI;
-
Change the base class to
NPage
i.
public
partial
class
MainForm :
NPage
-
Modify
Global.asax.cs (or
Global.asax.vb) file
as following:
-
Include Alachisoft
namespace as following:
i.
using
Alachisoft.NCache.Web;
ii.
using
Alachisoft.NCache.Web.Caching;
-
Change base class to
NHttpApplication:
i.
public
class
Global
:
NHttpApplication
-
Initialize NCache in
Init() method
i.
string
cacheName =
ConfigurationSettings.AppSettings["CacheName"];
ii.
NCache.InitializeCache(cacheName);
|