Sep 29 2008

Shared Memory Scoped Variables and Clustering

Posted by Mike Brunt at 1:08 PM
39 comments
- Categories: ColdFusion | JRun-J2EE

Several clients and potential clients asked us recently how they can deal with Application scoped variables in a ColdFusion cluster where a visitor is moved from one cluster instance to another, during their time on the web site.  Before answering that I thought it would be worth going through an overview of the three shared memory scopes used in CF and some comments about their use.  I realize that many of us already know what we are showing here, having said that there are many newcomers to CF so it won't hurt to discuss this; in my opinion.  Please note that the only version of ColdFusion which supports clustering is ColdFusion Enterprise.  

The three shared memory scopes are as follows:

  • Server Scope: This scope is shared by all ColdFusion Applications and all Users on a single server.  Prior to ColdFusion MX 6, this always meant one physical server.  In the J2EE world and in ColdFusion Enterprise, we entered a new era where multiple instances are the norm.  An instance is directly equivalent  to a physical server in pre CF-J2EE installs. So if you have multiple ColdFusion Applications on a single instance, as instantiated using the <cfapplication name = "application_name"> tag, all those named ColdFusion Applications have access to the Server Scope as a single shared scope.  Adobe will be introducing hooks into the Session Scope from Application.cfc in ColdFusion 9 (codenamed "Centaur"). The Server Scope cannot be replicated to other instances, natively, in the ColdFusion-J2EE clustering mechanism, so all creations/sets of Server Scope variables should be made in the Application.cfm or Application.cfc so that they are available immediately after the first visitor hits the ColdFusion Application and to all subsequent visitors.  The creating/setting of Server Scope variables should be locked using <cflock> so as to avoid a "race-condition", which is roughly equivalent to what could happen in a Database where a row being added or updated is being read at the same time.  One other very important point, in clustered environments the code for each instance should be identical, this can be achieved by using either real-time replicated code or a single shared copy of the code on something like Network Attached Storage (NAS) or a Storage Area Network (SAN).  Having identical code bases ensures that even if site visitors are moved around instances-servers the Server Scope will contain identical variables and just to reiterate; all instantiating of server variables should be done in the root Application.cfm or Application.cfc and no lower in the code structure.
  • Application Scope: The Application Scope is created using the <cfapplication name = application_name"> tag which creates a named ColdFusion Application and there can be multiple ColdFusion Applications on a single ColdFusion instance/server;  if there are, they must all be uniquely named.  Each ColdFusion Application can have an Application Scope which can contain Application Scope variables which are shared by all visitors.  The Application Scope cannot be replicated in the ColdFusion-J2EE clustering mechanism, natively, so all creations/sets of Application Scope variables should be made in the Application.cfm or Application.cfc so that they are available immediately after the first visitor hits the ColdFusion Application and to all subsequent visitors. The creating/setting of Session Scope variables should also be locked using <cflock> so as to avoid a "race-condition", which is roughly equivalent to what could happen in a Database where a row being added or updated is being read at the same time.   One other very important point, in clustered environments the code for each instance should be identical, this can be achieved by using either real-time replicated code or a single shared copy of the code on something like Network Attached Storage (NAS) or a Storage Area Network (SAN).  Having identical code bases ensures that even if site visitors are moved around instances-servers the Application Scope contains identical variables after they have been instantiated and just to reiterate; all sets of Application variables should be done in the root Application.cfm or Application.cfc and no lower in the code structure.  The Application Scope has a timeout value which is set either in the ColdFusion administrator GUI and in the <cfapplication name = application_name"> tag, this setting overrides the setting in the ColdFusion administrator GUI .  One other very important point about the Application scope, there is another scope which is not persisted in memory and which can often be interchanged with the Application Scope, this is the Request Scope.  During my time troubleshooting ColdFusion applications some clients have encountered memory leakage problems where a large amount of data is persisted in the Application scope.  My opinion, as a result, is that the Request Scope should be used wherever possible in preference to the Application scope.  This is not practical, though, where frameworks such as ColdBox, FuseBox, mach-ii/Model Glue, ColdSpring, Reactor/Transfer are used.  These frameworks are persisted in the Application Scope which means large numbers of objects are created and persisted in the Application Scope, in order to ensure acceptable performance.   The Request Scope is not persisted in memory and variables created there, die at the end of each request.
  • Session Scope:  The Session Scope is in essence a sub-set of the Application Scope and contains all variables relating to a single visitors session.  There cannot be a Session Scope unless an Application scope also exists and the Session Scope is typically set within the <cfapplication> tag.  The reason that the Session Scope is considered to be a shared scope is that the variables in the Session Scope are shared throughout a single users session.  If a web site has multiple visitors, which most do, there will be multiple sessions existing at any one time; one Session Scope per user.  This maintaining of state is tracked by either using a cookie on the users browser or passing a user identifier on the URL.  Passing this on the URL is not recommended.  In the J2EE clustering mechanism, which is utilized by ColdFusion, Session variables can be replicated using J2EE "buddy" replication.  J2EE clustering is "peer-to-peer" which means that there is no central controller of the cluster; this is a note point.  All cluster members (ColdFusion instances) must have unique names and all clusters also must also have unique names.  The buddy mechanism is activated when we select "Session Replication" in the ColdFusion Administrator when creating or updating a cluster.  Once this is successfully activated, all Session data is replicated via the network and where there is a lot of data per Session and where there are large numbers of concurrent users this creates a large volume of network traffic; as replication occurs in real-time.  I have seen a good number of cases where Session Replication can be fraught with difficulties, such as out of synch data across cluster members (instances), for this reason I always recommend that the "round-robin" with "sticky sessions" algorithm be selected so that site visitors are not bounced around a cluster with each request.  As with the Application scope, there is an alternative to the Session Scope, in ColdFusion.  This is the Client Scope and I would always recommend its use over the Session Scope, the one draw-back being that there is no native way to store complex data types in the Client Scope.  However, I have seen our clients get around this by using serialization-de serialization, typically employing WDDX to do so.  Typically we would maintain-persist Client Variables in a database, this is configurable in the ColdFusion administrator GUI.  The big advantages of the Client Scope is we do not need to replicate large amounts of data around each cluster member, via the network, in real-time and this makes it very easy to grow the cluster, by adding new instances. 

In this blog piece I have attempted to detail memory resident shared scope variables and how their use is impacted when we move to a clustered environment.  If some of you use different methodologies it would be good to hear about them here.

Comments

Michael Sharman

Michael Sharman wrote on 10/09/08 6:26 PM

Hi Mike,

>This is the Client Scope and I would always recommend its use over the Session Scope

Could you please explain this a bit further?

Isn't the client scope stored either in the registry (bad) or in a database? Storing in a database gets around the problem of access "session" data in a cluster, but has the impact of having to request this information from the database on every request.

Of course a very general rule of thumb is that the databaes is one of the first places you look for in terms of performance bottlenecks.

Session data stored in server memory is so much faster and obviously natively handles complex data structures. As long as you had sticky sessions across a cluster why would you recommend client scope?
Mike Brunt

Mike Brunt wrote on 10/12/08 6:05 PM

@Michael, thanks for your comment as always. In essence and taking into account the difficulties with using complex data types in the client scope I have found it to be more reliable to use the client scope than replicating sessions. Of course sticky sessions would help a lot, providing all cluster members are up and running.
Mike Brunt

Mike Brunt wrote on 10/12/08 6:09 PM

@Michael, I missed your point about storing client vars in the registry, which is the default, and yes that is bad and should be changed to a DB in my opinion.
Jose Galdamez

Jose Galdamez wrote on 06/16/09 3:32 PM

Hi Mike,

The answer to my question may not be so straightforward, but I thought I would ask anyway since you would know better than I.

Why is it that there are memory leakage problems with the Application scope? Aren't all Application variables (complex and simple) supposed to be flushed from memory when the application terminates?

Also, in the description for the Application scope you have this sentence:

"The creating/setting of Session Scope variables should also be locked using <cflock> so as to avoid a 'race-condition'"

Shouldn't it read "Application Scope variables" instead of "Session Scope variables?" I read about of cflock'ing Application variables in CF WACK Vol. 1, and it seems like locking with the Session scope is seldom needed.

Thanks for the article! It is quite informative even though I currently do not write for applications in a clustered environment.
Door Canopies

Door Canopies wrote on 07/13/10 10:59 AM

I also share the same question as above:

"Why is it that there are memory leakage problems with the Application scope? Aren't all Application variables (complex and simple) supposed to be flushed from memory when the application terminates?"

Thanks for the help!
cosplay

cosplay wrote on 09/16/10 12:19 AM

Prom Dresses: Find Online fashionable prom dresses, 2011 Prom Dresses, prom gown, cheap prom dresses bridesmaid dresses, bridesmaid gowns, cheap bridesmaid
san diego divorce lawyers

san diego divorce lawyers wrote on 10/17/10 2:12 AM

Thanks for a nice share you have given to us with such an large collection of information. Great work you have done by sharing them to all. simply superb.
smart card

smart card wrote on 10/20/10 9:25 PM

very good article about shared memory scoped variables and clustering which I am sure is a sticky area for many in this field especially whose in who deal with Application scoped variables in a ColdFusion cluster! All the three shared memory scopes – Server Scope, Application Scope and Session Scope are discussed here and I am sure that it will clear a lot of doubts and help utilize them better!!
select comfort beds

select comfort beds wrote on 10/22/10 6:02 PM

It's glad to read the article about an overview of the three shared memory scopes used in CF and some comments about their use. Awesome!
Baccarat Strategies

Baccarat Strategies wrote on 12/09/10 10:37 PM

Really great work! Everything is open and clear explanation of issues. It contains truly information. Your website is very useful. Thanks for sharing. Looking forward to more!
Custom flag

Custom flag wrote on 07/12/11 3:06 AM

ohh man this is awesome post.i think they should usebanner to get more flow in look.
you know this is great working by the writer.The writer has done a great job in speniding his time in
research about this article. can i subscribe all his posts ?
I am really happy to read this. i was searching this from last two months and atlast i got it. hurrah..!!
Zawflnrd

Zawflnrd wrote on 09/24/11 2:07 AM

Go travelling <a href=" http://www.fotolog.com/jeuocih/about ">Nymphets Toplist</a> 089507
Ncycmxzx

Ncycmxzx wrote on 09/24/11 2:32 AM

I'm unemployed <a href=" http://www.fotolog.com/uiukufi/about ">Non-Nude Model Teen</a> chb
Alrvhtle

Alrvhtle wrote on 09/24/11 2:33 AM

One moment, please <a href=" http://www.fotolog.com/olehepapi/about ">Webcams Fitness Models</a> >:)))
Rtkxyvrl

Rtkxyvrl wrote on 09/24/11 2:33 AM

Just over two years <a href=" http://www.fotolog.com/ecamyaro/about ">Kacy Teen Model</a> 4632
Khiajbrs

Khiajbrs wrote on 09/24/11 5:53 AM

I'll text you later <a href=" http://www.fotolog.com/masipifij/about ">15yo Nn Models</a> rxfkaj
Lzorysaz

Lzorysaz wrote on 09/24/11 5:53 AM

Will I be paid weekly or monthly? <a href=" http://www.fotolog.com/ydoriyi/about ">Super Children Models</a> 8-DDD
Makeealz

Makeealz wrote on 09/24/11 6:22 AM

Punk not dead <a href=" http://www.fotolog.com/enucaise/about ">Young Forum Models</a> cysatp
Tojwclfv

Tojwclfv wrote on 09/24/11 6:22 AM

Do you like it here? <a href=" http://www.fotolog.com/nanayqeki/about ">Lls Nude Models</a> cocro
Yedvinmy

Yedvinmy wrote on 09/24/11 6:22 AM

I work here <a href=" http://www.fotolog.com/uyqifypy/about ">Little Model Sexy</a> =[[[
Tsesjvnp

Tsesjvnp wrote on 09/24/11 10:19 AM

Have you got any experience? <a href=" http://www.fotolog.com/obekunata/about ">Japanesse Sex Models</a> 691
Wteuvwsl

Wteuvwsl wrote on 09/24/11 12:29 PM

A staff restaurant <a href=" http://www.fotolog.com/yjiyluaq/about ">Mania's Teen Model</a> 8]
Qkzfjxwy

Qkzfjxwy wrote on 09/24/11 12:29 PM

I like watching TV <a href=" http://www.fotolog.com/ajetejiy/about ">Coed Models Tgp</a> %-))
Rhmwhalc

Rhmwhalc wrote on 09/24/11 12:30 PM

Photography <a href=" http://www.fotolog.com/asosapee/about ">Little Lia Model</a> 7096
Kjtcuprs

Kjtcuprs wrote on 09/24/11 1:35 PM

Where do you live? <a href=" http://www.fotolog.com/higeuqod/about ">Child Generation Modeling</a> wdjq
Jqxbpzmq

Jqxbpzmq wrote on 10/30/11 7:32 PM

I've only just arrived <a href=" http://www.blogtv.com/people/ulefojyta ">bikini myspace pics</a> >:[[
Dejjgxrt

Dejjgxrt wrote on 10/30/11 8:54 PM

I'm not sure <a href=" http://www.blogtv.com/people/fijitaje ">nude lolas top 100</a> :-)
Hkbnaxuf

Hkbnaxuf wrote on 10/30/11 8:55 PM

Some First Class stamps <a href=" http://www.blogtv.com/people/yohohece ">sven s place bbs</a> :-]
Kabxkhbj

Kabxkhbj wrote on 10/30/11 8:56 PM

A Second Class stamp <a href=" http://www.blogtv.com/people/yrafijo ">max bbs touzokudan 12449</a> jqwufv
Audjtxji

Audjtxji wrote on 10/30/11 8:57 PM

Yes, I love it! <a href=" http://www.blogtv.com/people/yrafijo ">max bbs touzokudan 12449</a> lis
How long does it take gum to digest?

How long does it take gum to digest? wrote on 01/04/12 11:04 PM

zltgndgxijtqfsfs, <a href="http://superiorantivirus.com/">avast antivirus free download</a>, sHcGsZo, [url=http://superiorantivirus.com/]avast internet security[/url], AkjxZNb, http://superiorantivirus.com/ avast antivirus, cIfWwMT, <a href="http://www.exaltcustoms.com/">Order soma online</a>, qMAWQwX, [url=http://www.exaltcustoms.com/]Buy carisoprodol online[/url], sCdHGQZ, http://www.exaltcustoms.com/ Soma, KWbmKEQ, <a href="http://www.fallergy.com/">Buy kg site xanax</a>, wpiRdJn, [url=http://www.fallergy.com/]Buy 2 mg xanax bars[/url], uVXbqFx, http://www.fallergy.com/ Buy xanax online ups delivery, pFAtgbm, <a href="http://rpggameszone.com/">rpg games</a>, mQJXaFE, [url=http://rpggameszone.com/]Sim rpg games[/url], vMlFsvo, http://rpggameszone.com/ rpg games, oldTLvj, <a href="http://buyvolumepillsonline.com/">Free volume pills</a>, eVGZEki, [url=http://buyvolumepillsonline.com/]Volume Pills[/url], rVMteSO, http://buyvolumepillsonline.com/ Girl gets result of volume pills in face, WwULzXl, <a href="http://digestitshop.net/">Digest It</a>, GwdoODw, [url=http://digestitshop.net/]How long does it take for a dog to digest it's food[/url], OvNlShv, http://digestitshop.net/ Digest It, TSWefhF.
volusion vs shopify

volusion vs shopify wrote on 01/04/12 11:09 PM

opidkdgxijtqfsfs, <a href="http://powerfulhostnow.com/">my volusion</a>, hEFejui, [url=http://powerfulhostnow.com/]volusion shopping cart[/url], BaBlqJe, http://powerfulhostnow.com/ volusion vs shopify, hDXhpyV.
dresses

dresses wrote on 01/04/12 11:19 PM

obmbmdgxijtqfsfs, <a href="http://www.how-to-lose-stomach-fat.net/">How To Lose Stomach Fat</a>, WVgVJdd, [url=http://www.how-to-lose-stomach-fat.net/]How To Lose Belly Fat[/url], zsAIdyC, http://www.how-to-lose-stomach-fat.net/ How To Lose Stomach Fat, LibjDHt, <a href="http://www.im4designs.com/bloghoster/tramadol/">Tramadol</a>;, LBxTiub, [url=http://www.im4designs.com/bloghoster/tramadol/]Cod delivery tramadol[/url], stwlEGV, http://www.im4designs.com/bloghoster/tramadol/ Powered by smugmug new comment link online pharmacy tramadol, qXnGlIR, <a href="http://www.ppiwon.co.uk/">ppi</a>;, piYCUUi, [url=http://www.ppiwon.co.uk/]Payment Protection Insurance[/url], IPiRDLK, http://www.ppiwon.co.uk/ 960-by-640-pixel resolution at 326 ppi cellphone, PWgfWpb, <a href="http://www.comoesquecerumamor.com.br/">Como Esquecer Um Amor</a>, xdyWFCV, [url=http://www.comoesquecerumamor.com.br/]Como Esquecer Um Amor[/url], akJXwSf, http://www.comoesquecerumamor.com.br/ Como Esquecer Um Amor, ewlOMsR, <a href="http://www.motherofthebridedresses.org/">mother of the bride</a>, sCXNJwU, [url=http://www.motherofthebridedresses.org/]mother of bride dress[/url], hpdDSRo, http://www.motherofthebridedresses.org/ mother of the bride, jrZuEwC, <a href="http://www.mrwaterdamage.com/">Water Damage Restoration Service</a>, dzOmwsh, [url=http://www.mrwaterdamage.com/]Water damage restoration phoenix[/url], SMXjPGr, http://www.mrwaterdamage.com/ Water damage restoration romeoville, KuEbsqV.
Ares Download

Ares Download wrote on 01/04/12 11:23 PM

weyuedgxijtqfsfs, <a href="http://tagesgeldzinsen.tv/">Tagesgeld Zinsen</a>, gJAbegO, [url=http://tagesgeldzinsen.tv/]Zinsen Tagesgeld[/url], yhERyMl, http://tagesgeldzinsen.tv/ Zinsen Tagesgeld, uitlEee, <a href="http://www.ares-p2p-download.com/">Ares Download</a>, QaPPcVa, [url=http://www.ares-p2p-download.com/]Ares Download[/url], WsGtvHS, http://www.ares-p2p-download.com/ Ares 2 0 9 free download windows software, sBfCFMs, <a href="http://www.ppiwon.co.uk/">Payment Protection Insurance</a>, QHUuTAL, [url=http://www.ppiwon.co.uk/]Ppi group[/url], ZOKXyXm, http://www.ppiwon.co.uk/ Ppi claims, gCnZvqt, <a href="http://www.how-to-lose-stomach-fat.net/">How To Lose Stomach Fat</a>, JOrvvGQ, [url=http://www.how-to-lose-stomach-fat.net/]How To Lose Stomach Fat[/url], VecdmNy, http://www.how-to-lose-stomach-fat.net/ How To Lose Stomach Fat, rgIjqPO, <a href="http://de.gigajob.com/index.html">Job</a>;, IjmhWbo, [url=http://de.gigajob.com/index.html]Jobs[/url], PBCPdUp, http://de.gigajob.com/index.html Stellenangebote daf, XnFWrde, <a href="http://invitationpaper.org/">Business Cards</a>, IugPTRz, [url=http://invitationpaper.org/]Photo Cards[/url], YCiXsmn, http://invitationpaper.org/ Graduation Invitations, xDGKfRG.
Download microsoft spider solitaire

Download microsoft spider solitaire wrote on 01/04/12 11:26 PM

ulfywdgxijtqfsfs, <a href="http://www.mymedsbenefits.com/">Buy ambien sleeping pills online</a>, WYmfxLV, [url=http://www.mymedsbenefits.com/]Buy ambien online canada[/url], cOmurXu, http://www.mymedsbenefits.com/ Buying ambien generic, bqfwwzR, <a href="http://meridiareview.com/">Meridia sale</a>, mLqqkrv, [url=http://meridiareview.com/]Meridia[/url], NGbJKwL, http://meridiareview.com/ Meridia, QsPOrMS, <a href="http://solitairelove.com/">Free cell solitaire</a>, dFkckCm, [url=http://solitairelove.com/]Diamond solitaire rings[/url], CtSbruS, http://solitairelove.com/ Princess cut solitaire diamond ring, xzsvhkY, <a href="http://oxycodoneinformation.com/">Oxycodone</a>;, yzcWFpv, [url=http://oxycodoneinformation.com/]Deafness oxycodone[/url], TzyPOjc, http://oxycodoneinformation.com/ Oxycodone, TPOaqWu, <a href="http://vimax2k.com/">Apt vimax leave a reply name email comment -comments closed</a>, CRpxUtB, [url=http://vimax2k.com/]Vimax[/url], QWIhBOz, http://vimax2k.com/ Does vimax work, LqLoTiK, <a href="http://dailygeeknews.com/">Vigrx plus</a>, LYKdNWn, [url=http://dailygeeknews.com/]VigRX[/url], naFsWiW, http://dailygeeknews.com/ Vigrx oil reviews, YvKVktM.
Employment security commission of north carolina

Employment security commission of north carolina wrote on 01/04/12 11:28 PM

pwwtgdgxijtqfsfs, <a href="http://www.motherofthebridedresses.org/">mother of the bride dresses</a>, elIIDER, [url=http://www.motherofthebridedresses.org/]Discounted mother of the bride dresses[/url], imwEJaU, http://www.motherofthebridedresses.org/ gowns, qmeisFx, <a href="http://us.gigajob.com/">job site</a>, CViMPCB, [url=http://us.gigajob.com/]new job[/url], kGCSPKS, http://us.gigajob.com/ best job, gbltGGI, <a href="http://www.eillusionmagereview.com/">Animation Software</a>, bhCOWEr, [url=http://www.eillusionmagereview.com/]Animation Software[/url], qYQryFH, http://www.eillusionmagereview.com/ Graphics for wraps software, ubOQoNQ, <a href="http://invitationpaper.org/">Graduation Invitations</a>, lqphXdC, [url=http://invitationpaper.org/]Birthday Invitations[/url], qAZDfRW, http://invitationpaper.org/ Greeting Cards, VGSbufp, <a href="http://kumbakonam.biz/">kumbakonam map</a>, egLtClk, [url=http://kumbakonam.biz/]Kumbakonam[/url], LUoYwgr, http://kumbakonam.biz/ kumbakonam map, jAfKlFe, <a href="http://us.gigajob.com/">employment</a>;, UADTWXZ, [url=http://us.gigajob.com/]applications for employment[/url], zjLUVvO, http://us.gigajob.com/ find employment, rFZztOL.

Write your comment



(it will not be displayed)



Leave this field empty: