How Cache Dependency Manages Data Relationships?

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 Cache Dependency which ASP.NET Cache provides and that NCache incorporated from day one. 

Download NCache free trial - Extremely fast and scalable in-memory distributed cache

Just like ASP.NET Cache, in NCache, Cache 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 Cache 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 Cache Dependency:
Cache 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: 

One-to-one relationship:

A has one-to-one with B. Add B without any Cache Dependency. Then, add A and specify a Cache Dependency for B. If A and B both have a mutual dependency, then when update B afterwards and specify a dependency on A. 

One-to-many relationship:

A has one-to-many with B. Add A first without any Cache Dependency. Then, add one or more B items and specify a Cache 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. 

Many-to-many relationship:

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 Cache Dependency for the appropriate A’s. Then, go back and update A’s to specify Cache Dependency on appropriate B’s. 

public void CreateDependencies(Cache cache)
{
   string keyB = "objectB-1000";
   ObjectB objB = new ObjectB();
   string keyA = "objectA-1000";
   ObjectA objA = new ObjectA();
    // “null” third argument means objB does not depend on anything
   _cache.Add(keyB, objB, null,
              Cache.NoAbsoluteExpiration,
              Cache.NoSlidingExpiration,
              CacheItemPriority.Default, null, null);
    // third argument specifies that objA depends on objB
   string[] ADependsOn = { keyB };
   _cache.Add(keyA, objA, new CacheDependency(null, ADependsOn),
              Cache.NoAbsoluteExpiration,
              Cache.NoSlidingExpiration,
              CacheItemPriority.Default, null, null);
    // Removing "objB" automatically removes “objA” as well
   _cache.Remove(keyB);
   _cache.Dispose();
}

So, NCache allows you to take advantage of Cache 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.  

Download NCache Trial | NCache Details

This entry was posted in ASP .NET performance, ASP.NET Cache, Cache dependency, Distributed caching, SQL cache dependency and tagged , , , . Bookmark the permalink.

4 Responses to How Cache Dependency Manages Data Relationships?

  1. mikerosss says:

    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.

    • Naty says:

      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

  2. Khushi Shaikh says:

    Hi Iqbal,
    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.
    Thanks
    Khusi

    • Iqbal Khan says:

      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.

Leave a Reply

Your email address will not be published. Required fields are marked *


9 − = seven

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>