ASP.NET has grown significantly in popularity as a technology for web applications, with more developers now using it to create high-traffic applications. To handle the higher traffic, these applications are deployed in load-balanced web farms where you can add more servers as your traffic load increases.
But a problem occurs; the database and your data storage cannot scale in the same fashion to handle the higher traffic loads. This results in a bottleneck where your ASP.NET application slows down and can even grind to a halt. In such situations, data caching is an excellent way of resolving this bottleneck. Caching allows you to store application data close by, significantly reducing the need for those expensive database trips.
What is ASP.NET Cache?
It allows you to cache application data and is a fairly feature-rich cache including the following features:
- Expirations: To automatically expire items using absolute and sliding expirations.
- CacheDependency: To manage data relationships in the cache.
- SqlCacheDependency: To synchronize the cache with database.
- Callbacks: To be notified when items are updated in the cache.
Here is some sample code showing its usage
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System.Web.Caching; // Create a key to lookup in the cache // The key for will be like “Employees:PK:1000” string key = "Employee:EmployeeId:" + emp.EmployeeId.ToString(); Employee employee = (Employee)Cache[key]; if (employee == null) { // item not found in the cache. load from db LoadEmployeeFromDb(employee); // Now, add it to the cache for future reference Cache.Insert(key, employee, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Default, null); } |
ASP.NET Cache Limitations in Web Farms
Despite its highly effective caching capabilities, ASP.NET Cache has some serious limitations. Some of them are as follows:
- Does not synchronize across a server or worker processes:ASP.NET Cache does not synchronize across multiple servers or even multiple worker processes. So, you cannot use it in a web farm or even a web garden unless your data is read-only whereas you need to cache all kinds of data, including one that changes somewhat frequently.
- Cache size limitation: You cannot grow the ASP.NET Cache to be more than what one ASP.NET worker process can contain. For 32-bit systems, this is 1GB and that includes application code as well. Even for 64-bit systems, the size cannot scale.
Use ASP.NET Cache Compatible Distributed Cache
The way to work around these limitations of ASP.NET Cache is to use a distributed cache like NCache for web farms. NCache offers all the features of ASP.NET Cache and additional capabilities. But, as a distributed cache, NCache easily synchronizes across multiple servers. Here are some benefits you get from NCache:
- Scale’s transaction load: You can keep adding more cache servers to the cache cluster as your web farm grows from 2 to 200 servers. NCache never becomes a bottleneck in handling more traffic.
- Scale’s data storage: As you add more cache servers, your cache storage capacity grows due to Partition Cache topologies
- Replicates data for reliability: You can ensure that no data loss occurs even if a server goes down because data is replicated to other servers.
- Dynamic self-healing cache cluster:NCache provides 100% uptime through this. And, you can add or remove cache servers at runtime without stopping the cache or your application.
Conclusion
In conclusion, ASP.NET Cache faces limitations in high-traffic web farms, including lack of synchronization and limited cache size. To address these issues, a distributed cache like NCache is recommended, as it provides superior scalability, reliability, and performance across multiple servers. Therefore, NCache is indispensable for modern ASP.NET applications.
You can always drop us an email at sales@alachisoft.com
Appreciate it for the excellent writeup. Anyway, how could we communicate?
I’ve used NCache during one of my projects and results were very impressive. highly recommended !!!