Question

This question:

Is it possible to share HttpRuntime.Cache between multiple web servers?

..Relates to http://msdn.microsoft.com/en-us/library/system.web.caching.aspx

..I think I know the answer, but for everyone's benefit, with the newer:

http://msdn.microsoft.com/en-us/library/system.runtime.caching.aspx

It is realistically possible? Or would one be better off using memcached / AppFabric / redis / xyz.

Was it helpful?

Solution

HttpRuntime.Cache runs in-process (i.e., in server memory) and cannot be shared between multiple web servers. You can extend the mentioned System.Runtime.Caching with a custom provider to centralize caching using a platform such as SQL Server or a dedicated server.

Recommended multi-web server caching strategy would definitely depend on the environment.

On the lower end of scale, using SQL Server is a quick option that is probably familiar to a .NET developer. Another option is using a web service (e.g., WCF) to centralize cached data calls.

On the higher end, you have mentioned the leaders in the field: memcached, AppFabric, Redis, etc. If these are already setup in your environment and/or you are familiar with them I could definitely see using them whenever you have multiple web servers (big or small).

I recommend at least checking out Redis. It's the newer category of the group mentioned but it's lightweight, fast and in addition to the key/value store it has other functionality great for distributed systems such as pub/sub.

OTHER TIPS

.Net's System.Runtime.Caching api provides abstract class ObjectCache that provides the base methods and properties for accessing the object cache. Only implementation of this abstract class provided in .Net is MemoryCache which is an in-proc cache and cannot be shared between multiple web servers

However you can yourself extend ObjectCache class to incorporate functionality of a distributed cache.

Or for an out of the box solution you can use a distributed caching solution that provides ObjectCache's implementation for distributed caching. NCache is one of the distributed caching solutions that provide's ObjectCache implementation, as described here

NCache has developed a provider for .NET 4.0 Cache that results in an extremely fast and highly scalable .NET distributed cache. This allows applications using .NET 4.0 Cache to now scale to multi-server environments and also remove any database bottlenecks. You can incorporate NCache as your .NET distributed cache without any code changes to your application. You only change your configuration file to use NCache.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top