Caching in multiple Web Applications

Topics: Caching Application Block
Mar 7, 2007 at 3:01 PM
I am having a little issue with caching and was wondering if anyone could help me out.

I have two web applications with dll project backs. One application maintains Security for the other application and possibly many others. The Security application has a caching block in the Security Web project. The other application, which uses the security dll also has its own cachine block, but also uses the security application block. The problem is that while both applications are running on the same server, using the same web service, the two applications are using two seperate Security Caching Blocks. This means that when I make a change to cache in the Security application, it is not reflected in the other application.

Is there a way to ensure that both applications use the same Security Cache? If not, is there another solution to my problem? thank you very much for your help.

David
Mar 7, 2007 at 3:06 PM
I wanted to post a little code to further explain my situation.
Here is the web.config for the Security app:

<cachingConfiguration defaultCacheManager="SecurityDataCacheManager">
<cacheManagers>
<add expirationPollFrequencyInSeconds="600" maximumElementsInCacheBeforeScavenging="5000"
numberToRemoveWhenScavenging="10" backingStoreName="Security Null Storage"
name="SecurityDataCacheManager" />
</cacheManagers>
<backingStores>
<add encryptionProviderName="" type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
name="Security Null Storage" />
</backingStores>
</cachingConfiguration>

Here is the web.config for the other application:

<cachingConfiguration defaultCacheManager="DataCacheManager">
<cacheManagers>
<add expirationPollFrequencyInSeconds="600" maximumElementsInCacheBeforeScavenging="5000"
numberToRemoveWhenScavenging="10" backingStoreName="Null Storage"
name="DataCacheManager" />
<add expirationPollFrequencyInSeconds="600" maximumElementsInCacheBeforeScavenging="5000"
numberToRemoveWhenScavenging="10" backingStoreName="Security Null Storage"
name="SecurityDataCacheManager" />
</cacheManagers>
<backingStores>
<add encryptionProviderName="" type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
name="Null Storage" />
<add encryptionProviderName="" type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
name="Security Null Storage" />
</backingStores>
</cachingConfiguration>

Objects that reside in the security app use the SecurityDataCacheManager, and objects in the other app use the DataCacheManager

In both apps, the cache manager is created like:
private static CacheManager cacheManager = CacheFactory.GetCacheManager("DataCacheManager");



Thanks again for any help
Mar 7, 2007 at 7:22 PM
Hi David -

The Caching Application Block is architected such that the master copy of the cache is stored in-process. While you can use a backing store to make the cache resilient to application restarts, this data is only read when the cache manager first starts up, and after that it is only written to. The consequence is that the architecture fundamentally does not support reusing a single cache across appdomains, processes or machines. The block will let you point two cache managers at the same backing store, but this will result in unpredictable behavior and should not be done.

So unfortunately you'll need to come up with another solution to your problem, such as storing the data in a database or shared file.

Tom
Mar 8, 2007 at 9:57 AM
Hi Tom,

You mean that I can't use the Caching Application Block to cache things in a farm at all or that I can't updates won't be synchronized across servers?

Imagine this scenario: I have some info that is expensive to retrieve (several web service calls and database accesses), but when retrieved and processed is considered static for some period of time. Would it be ok to use the Caching Application Block accross servers in the same farm?

--
Paulo Morgado
Mar 8, 2007 at 12:41 PM
hello,

I think that my problem is a little more specific. But to answer your question Paulo, there should be no issues with caching in a web farm. Each farm would have its own caching, but when a cache is invalidated on one farm, there has to be a web service to invalidate that cache on the other farms.

My problem is that I have two different web applications running. Both of the applications have some backend code that is sharded, and therefore should work in the same cache block. The only thing that is being cached is database retrievals. I have my backed code generated, and each fetch call is put into cache automatically. When that object is updated or deleted, the cache for that object is flushed. If a fetch call was made on both applications for an object, and then the object was updated or deleted in one of the applications, I need the cache to be flushed in both applications so the object is retrieved from the database on its next fetch. When the cache is invalidated on one of the applications, it should also be invalidated in the other application to be sure that the data matches in both applications.

tom, I know you said that it might give unpredictable behavior, but can you show me how to have the two cache managers work off of the same backing store so I can test it?

Thanks again.

David
Mar 8, 2007 at 4:04 PM
Paulo - David is correct. You can use the block in a web farm, but each node will have its own copy of the cache, and you can't guarantee that the data will be the same in each node. In some situations this may be fine - if the data changes relatively infrequently and you can tolerate a bit of staleness or inconsistency (maybe a list of postal codes) then this will be quite efficient. But if you need to be sure that each node has the exact same data, you'll need to use a shared cache (such as a database).

David - sharing a backing store is a bad idea. Both apps will initially load whatever is in the store, but from then on they will only write to it. This will result in a "last in wins" situation and could result in random data in the store. Furthermore a change in the cache on one node won't be visible to the other node, unless the cache manager is restarted.

Tom
Mar 8, 2007 at 5:37 PM
Edited Mar 8, 2007 at 5:38 PM
Tom,
I understand the plight and think I have come up with a solution, no matter how messy it may become. I am going to simulate what I am doing for the web farms for the two applications. Luckily, I designed the applications so that only the web application directly tied to the backend can make edits to the objects, and therefore invalidate the cache. This means that the cache is only invalidated from the Security application and only that application has to let the other applications that read from its cache know to invalidate those cache objects.

In my cache design, each web application has a web service set up to flush or remove items from cache, I have extended this to allow the security app to call these web services to remove the security cache from those applications. I have tested this and it seems to be working fine. The only problem is that for every web farm for every application, I am going to have to add and maintain the address location for those web services in the web.config of the Security application. This is a small price to pay though to accomplish what I was looking to do.

Thanks for the insight and a push in the right direction

David
Dec 3, 2009 at 9:09 AM

Hi all,

can you suggest me the best method for Cache Application synchronization in Web Farm scenario?

Jan 4, 2010 at 6:42 AM

Hi,

Is this question specific to using the Caching Application Block in a web farm? Taken from the documentation "Situations that would be better served by other caching solutions are when there are multiple applications using the cache or when the cache and the application are not on the same system. For example, you cannot synchronize caching across a Web farm. However, you can replace the CacheManager class with a custom class if you need to fundamentally change the behavior of the application block. For more information, see Extending the Caching Application Block.".

http://msdn.microsoft.com/en-us/library/cc511557.aspx

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com