Distributed cache is becoming very popular because it is a powerful way to boost your application performance and scalability and handle extreme transaction load without slowing down. Both .NET and Java applications are using it more and more each day.
But, one challenge people face with distributed cache is how to map and store relational data in a HashTable (key, value) pairing storage that a distributed cache is. Most caches today do not provide any mechanism to handle this. Today, I will discuss Data Dependency which ASP.NET Cache provides, and that NCache incorporated from day one.
Just like ASP.NET Cache, in NCache, Data Dependency allows you to specify a dependency in the distributed cache between two cached items. Cached item A depends on cached item B. And, if B is ever updated or removed from the distributed cache, A is automatically removed. This ensures that if there is a referential integrity constraint between A and B in the database that it is also honored in the distributed cache. You can also specify cascading Data Dependency where A depends on B and B depends on C. Then, if you update or remove C, A and B are both removed. Here is a brief example of Data Dependency:
NCache Details Managing Relational Data Data Dependency Docs
Data Dependency lets you create one-to-one, one-to-many, and many-to-many relationships in the distributed cache. Here is how you would handle different scenarios:
A has one-to-one with B. Add B without any Data Dependency. Then, add A and specify a Data Dependency for B. If A and B both have a mutual dependency, then when update B afterwards and specify a dependency on A.
A has one-to-many with B. Add A first without any Data Dependency. Then, add one or more B items and specify a Data Dependency for the given A for all of them. This way, if A is updated or removed, all of B’s will be removed automatically by NCache.
A and B have many-to-many with each other. Add one or more A’s. Then, add one or more B’s and specify Data Dependency for the appropriate A’s. Then, go back and update A’s to specify Data Dependency on appropriate B’s.
Here is a simple example of creating one-to-one dependency in cache.
public static void CreateDependencies(ICache _cache)
string keyB = "objectB-1000";
Object objB = new Object();
string keyA = "objectA-1000";
Object objA = new Object();
// Initializing cacheItems
var itemOne = new CacheItem(objA);
var itemTwo = new CacheItem(objB);
// Adding objA dependent on ObjB
itemOne.Dependency = new KeyDependency(keyB);
//Adding items to cache
// Removing "objB" automatically removes “objA” as well
catch (Exception e)
So, NCache allows you to take advantage of Data Dependency and specify data relationships in the distributed cache. Download a fully working 60-day trial of NCache Enterprise and try it out for yourself.
Thanks for sharing the blog, it was well explained about data dependency, it was worth investing time reading this blog.
Thank you for sharing informative article. Can you please explain distributed cache? Does it mean by Cache maintained at different (physical) frontend servers and DB Server?
I have a scenario – Frontend Server (FS)1 is maintaing local cache C1. FS2 is maintaing its own local cache C2. FS3 is maintaning its own local cacheC3. There is DB Server that has global cache GC. All the local cache C1, C2, C3 need be to depends on GC. If GC is updated then all the local cache gets invalidated. I do not how this gonna work because In my opinon we cannot control the memory on the other system?
Please throw some light on this.
NCache is an out of process in-memory distributed cache. It can form a cluster of cache nodes at runtime and can be distributed across multiple applications.
Here is how NCache will handle your scenario:
You can create a Global clustered cache (for example having 2 nodes in cluster) on the DB servers or dedicated cache servers and can then utilize “Client Cache” feature on each front end server FS1, FS2 and FS3. NCache Client cache is a local cache running on NCache client boxes (Web/App servers are NCache clients) and keeps a copy of data locally on the client box which is most frequently accessed from the clustered cache.
Subsequent calls for the same data will be served from local client cache directly giving more performance by saving network trips, client caches can also be in-proc in nature.
The data synchronization is managed automatically by NCache. In case of updates on the Global clustered cache, client caches will automatically invalidate/Remove items and will bring an updated copy from the clustered cache on subsequent access.
It’s really a nice and helpful piece of information. I’m glad that you shared this helpful info with us. Please keep us informed like this. Thanks for sharing.
Hi Iqbal,Thank you for sharing imairnftove article. Can you please explain distributed cache? Does it mean by Cache maintained at different (physical) frontend servers and DB Server?I have a scenario Frontend Server (FS)1 is maintaing local cache C1. FS2 is maintaing its own local cache C2. FS3 is maintaning its own local cacheC3. There is DB Server that has global cache GC. All the local cache C1, C2, C3 need be to depends on GC. If GC is updated then all the local cache gets invalidated. I do not how this gonna work because In my opinon we cannot control the memory on the other system?Please throw some light on this.ThanksKhusi