Implement Caching for your BizTalk applications using "static" classes and methods.

Posted at: 6/4/2007 at 7:18 PM by saravana

UPDATED: 24th August 2007, to reflect Richard Seroter's comment

There is no necessity to explain the importance of caching in any server based developments like BizTalk, ASP .NET etc. You need to plan early in your development cycle to cache resources in order to make most out of your limited server resources.

In BizTalk applications, it's quite common to lookup a data store to pickup a value, at different stages like custom Adapters, Pipelines, Orchestration, etc. The data store could be a custom SQL database/table,  a XML File, Active directory etc etc. Whatever the data store is, if you are doing the lookup for every message you process without any caching, its going to be an expensive and useless operation.

Due to the nature of BizTalk architecture and behavior of .NET run-time, its very easy to implement a caching logic just with a static class and a static method as shown below:

public static class CacheHelper
    {
        private static Dictionary<string, string> _authorIds = null;
        public static string GetAuthorName(string authorId)
        {
            if (_authorIds == null)
            {
                Debug.WriteLine("Cache is empty. So populating the cache...");
                _authorIds = new Dictionary<string, string>();
                lock (_authorIds)
                {
                    _authorIds.Add("2FC0CF1D-E107", "Matthew Johnstone");
                    _authorIds.Add("71F5C860-80FA", "Carl Reynolds");
                    _authorIds.Add("158FF294-1A89", "Robert Perkins");
                    _authorIds.Add("A71AE681-9C39", "Michael Killey");
                    _authorIds.Add("58794661-A9A3", "Saravana Kumar");
                }
            }
            else
            {
                Debug.WriteLine("Cache list is pre-populated. Value is going to be taken from the cache.");
            }
            string authName = _authorIds[authorId];
            if (authName != string.Empty)
                return authName;
            else
                throw new Exception("Author cannot be found with id :" + authorId);
        }
    }

In .NET static variables are maintained per Common Language Runtime (CLR) "AppDomain". We can just exploit this behavior of .NET for our caching needs. 

Inside the BizTalk server host instances, several subservices will be running. The exact services can be found in the adm_HostInstance_SubServices table in the BizTalk Server Management database. The services are listed here for reference:

Service Description
Caching Used internally by BizTalk Server to cache configuration information for the other services
Endpoint Manager Responsible for hosting receive and send ports including adapters and pipelines
Tracking Responsible for moving data out of the MessageBox database and into the BAM or Tracking databases as appropriate
XLANG/s Host engine for BizTalk Server orchestrations
MSMQT MSMQT adapter service; serves as a replacement for the MSMQ protocol when interacting with BizTalk Server

The BizTalk host instance simply acts as a container to host these other services. When the service is started, each of these subservices is started as well. They handle all of the processing related to the messaging and orchestration engines in BizTalk Server. For isolated hosts, the Endpoint Manager is the only service that is loaded into the process. Isolated hosts are only intended to be used for hosting adapters that send and/or receive messages.

So, inside the BizTalk host instance by default there will be one .NET "AppDomain" created per each BizTalk SubService (EPM, XLANG, etc). Majority of our custom code will be running under the "AppDomains" either created for EPM (if you are doing any messaging related stuff, example custom pipeline components and any assemblies you reference) or XLANG (any custom orchestration and any assemblies you reference). Both the "AppDomains" will be hosted inside the BizTalk host instance.

As I mentioned earlier, in .NET static variables are maintained per Common Language Runtime (CLR) "AppDomain". Any static data you hold lives inside the "AppDomain" for the life time of the "AppDomain", in BizTalk term that means from the time BizTalk Host Instance is started, corresponding DLL's (pipeline components, pipelines, orchestrations, in general any custom assembly like schemas, maps, helper classes) are loaded (assemblies get loaded into "AppDomain" on demand) into the "AppDomains" (EPM, XLANG, etc) to the time BizTalk host instance is restarted.

We can utilize this behavior of .NET run-time, to create a pattern to cache values, which doesn't change very often. Lets see the behavior with an example.

Here is an example using Orchestration:

In the above code snippet (showed earlier), I just added few Name-Value pair items to "authorsIds" collection, in real world scenarios we might need to populate them from a data store like SQL database. If we are doing that database lookup for every message, then its going to be unnecessary roundtrip's to the database and wastage of server resource.

This utility class can now be used in any of your custom BizTalk solution like Adapters, Pipelines, Orchestrations etc.  The following Orchestration shows its usage.

Orchestration

The message assignment shape got the following lines of code.

MSG_AUTHOR_OUT = MSG_AUTHOR_IN;
MSG_AUTHOR_OUT.Name = Utility.CacheHelper.GetAuthorName(MSG_AUTHOR_IN.Id);

When I Build, Deploy and run the sample, for the first message the DebugView showed the following output:

image

For subsequent messages (I posted 4), DebugView showed the following output:

image

You can make sure the cache is repopulating by restarting the BizTalk host instance (NT Service).

NOTE: If you are using isolated adapters like HTTP/SOAP the data will be cached under the IIS worker process, so to reset the cache you need to reset IIS.

This technique is simple to implement and easy to use. If you want more advanced caching like cache expiration, dependency etc then you can consider using Enterprise Library caching application block.

Download the full sample here.

Nandri!

Saravana

Tags:  Categories: BizTalk General
Actions: Email this article Email | Kick it! | DZone it! | Save to del.icio.us | Technorati Links
Post Information: Permanent LinkPermalink | CommentsComments(30) | Comments RSS

Comments

Friday, October 12, 2007 8:21 PM
shawjz77
Saravana,

This is a simple way of caching data in BizTalk.  However, due to multiple app domains you will occur a static call per domain.  If you were loading lots of data from a database to cache you would do this more than once.  One way around this is to either create your own service that can be queried for cached data or use another service such as SSO service that can be used for caching.  

www.masteringbiztalk.com/.../...-08b1eb6c4def.aspx

Hardly revolutionary this idea has been around since 2005.

John S
Wednesday, July 07, 2010 10:02 AM
oil painting
To find the universal elements enough; to find the air and the water exhilarating; to be refreshed by a morning walk or an evening saunter; to be thrilled by the stars at night; to be elated over a bird's nest or a wildflower in spring - these are some of the rewards of the simple life.  ~John Burroughs
Wednesday, July 14, 2010 3:39 PM
tmobile customer service number
This could have such amazing effects on the performance of biztalk. Caching is very important to have a fast and reliable  performance and where else better than your blog i could have found a trick to do this. There couldn't have been a method simpler and easier than this. Itz great easy to implement and execute and works wonders. I had no idea about caching in Biztalk. Thanks to you now i have one and that too such an effective one. This is a great blog..!!
        
Monday, July 19, 2010 2:21 AM
Supra Vaider High
Great post, I favor to make comments since the device lets writers for being more engaged and for the chance to get a further exchange with from each other.
Monday, July 19, 2010 5:29 PM
payday loans
The upside is your future is in your hands. And the downside is your future is in your hands.
Friday, July 23, 2010 1:37 AM
Nike Shox NZ
Found your site today and really liked it.. I bookmarked it and will be back to check it out some more later. Thanks so much for your awesome aticle!
Monday, July 26, 2010 1:41 AM
Rerto Jordans
Thanks for taking the time to chat about this, I feel fervently about this and I benefit from learning about this subject. Please, as you gain information, please add to this blog with more information. I have found it really useful.
http://www.nikeairjordan.cc
Tuesday, July 27, 2010 1:50 AM
Nike Shox NZ
Aw, this was a extremely high quality post. I really appreciate all the great content you have here. I am glad I cam across it!
Tuesday, July 27, 2010 1:59 PM
women seeking men
A blog with information really interesting, I come here all the time and am very happy with your updates, Thank you!Can you please provide more information on this subject?
Friday, July 30, 2010 6:59 AM
Coach factory outlet
Your article is very characteristic, I like reading, to work!
http://www.coachoutletmalls.com/   Coach factory outlet
http://www.guccihandbagsoutlet.com/   Gucci outlet online
This article is written by zocy003 on 2010-7-30
Saturday, July 31, 2010 10:29 AM
new balance
We're interested inside your site is presented merchandise. We pray to possess the opportunity to talk to together with sitting down slowly but surely. We are keen to cooperate with you. Also encouraged you to visit our web pages!http://www.yaahshoes.com/
Monday, August 02, 2010 4:32 AM
lenen
BKR problemen? Nu Geld lenen zonder BKR toetsing? Op zoek naar betrouwbare aanbieders? Wij vergelijken banken die u toch kunnen helpen aan een betrouwbare
Thursday, August 05, 2010 1:44 AM
Seattle Limousines
I was very pleased to find this site.I wanted to thank you for this great read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post.
Friday, August 06, 2010 2:24 AM
Timberland boots
I had just finished reading your articles,and this is very good! timberland boots uk Being a comprehensive trade solution provider offering sale services with free shipping to international buyers who are interested in shopping online.The products here are timberland boots outlet, timberland roll top boots and men timberland boots. As an online business platform mainly retailing, waterproof boots we provide our buyers with an efficient and manageable procurement process covering every phase of the international supply chain and streamlining trade channels. Also welcome wholesaling, feedback now!www.timberlandshoesonline.com
Saturday, August 07, 2010 3:52 PM
Nude Chat
Hi i love your blog ill add you to my favorites thanks
Saturday, August 07, 2010 4:19 PM
Camgirls
Great site, just gave it a bump on stumbleupon.com
Tuesday, August 10, 2010 7:56 AM
hypotheek
Hypotheek informatie, hypotheek aanvragen of afsluiten? Hypotheekrentes bekijken. Hypotheek aanbieders vergelijken, hypotheek vormen, bijkomende kosten,
Tuesday, August 10, 2010 10:05 AM
Binary Options
Good post, but have you thought about ASP ASP.NET load JavaScript dynamically with no duplicate before?
Monday, August 16, 2010 3:21 AM
Retro Jordans
Hi...Your post really got me thinking man..... an intelligent piece ,I must say.  
http://www.nikeairjordan.cc/
Tuesday, August 17, 2010 1:23 AM
Detectoare Radar
Just wanted to say thanks for this.
Thursday, August 19, 2010 1:51 PM
Online Shopping

everyone want to online shopping so if you want to your shopping easy and secure then you can online shopping through redshopcard.com
Thursday, August 19, 2010 1:52 PM
Sports Betting
SPORTS BETTING World's No.1 destination for online betting, football betting, baseball betting,spread betting & horse racing betting on the internet. Online Betting made simple & secure at SPORTSBETTINGCHUMP.COM
very fast
Friday, August 20, 2010 10:20 AM
Blova Accutron
Hold yourself responsible for a higher standard than anyone else expects of you. Never excuse yourself. Thank You. . .
Said by <a href="http://www.obwatches.com/">Breitling Watches</a>.<br>
Monday, August 23, 2010 5:14 PM
cheapretailjordan
[url=http://www.cheapretailjordan.com]cheap gucci clothing[/url]
[url=http://www.cheapretailjordan.com]Juicy Couture Bikini[/url]
[url=http://www.cheapretailjordan.com]Chanel t shirts[/url]
[url=http://www.cheapretailjordan.com]Christian Audigier Jeans[/url]
[url=http://www.cheapretailjordan.com]Polo t shirt[/url]
[url=http://www.cheapretailjordan.com]ed hardy handbags[/url]
[url=http://www.cheapretailjordan.com]armani jeans[/url]
[url=http://www.cheapretailjordan.com]Coogi Clothing[/url]
[url=http://www.cheapretailjordan.com]Radii ShoeS[/url]
[url=http://www.cheapretailjordan.com]cheap dolce gabbana shoes[/url]
Thursday, August 26, 2010 8:13 AM
Nike Dunk High Premium
Your publish are so decent, I identified numerous fascinating things as part of your website particularly its discussion. retain up the great perform.
Friday, August 27, 2010 4:12 AM
flower shop bangkok
I really liked your article. Keep up the good work.
I love flowers...I am also interested to send flowers all over the world....
Sunday, August 29, 2010 2:53 PM
registry cleaner reviews
wow  - you have already lots of fans and visitors on your blog - I wish I would have the same success with my blogs

Wednesday, September 01, 2010 2:55 PM
cartoon yourself
interessting post, keep posting
Wednesday, September 01, 2010 2:55 PM
sell my house
i bookmarked your blog, keep posting good stuff Smile
Wednesday, September 01, 2010 2:55 PM
sell my house
Good informative post. I will visit your site often to keep updated.

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading