5

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.

Community
  • 1
  • 1
Darren
  • 9,014
  • 2
  • 39
  • 50

2 Answers2

8

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.

Jonathan Harrison
  • 883
  • 1
  • 6
  • 13
  • Cool, I expected it could not be done just wanted it stated here. I guess by 'in-memory' you mean really mean 'in-process' because memcached, AppFabric, Redis are all in memory too of course. What I was wondering was if HttpRuntime.Cache had some easy separate-process mode like ASP.NET session state does – Darren Dec 13 '12 at 00:20
  • ...I guess you could always 'roll your own' separate process using it...but why reinvent the wheel. – Darren Dec 13 '12 at 00:26
  • Yes, in-process is a more appropriate term (I'll edit). FYI, Redis does operate by default with persistent storage... you can however switch it to run strictly in memory. Still very fast with persistence and a nice to have feature as compared to competitors. – Jonathan Harrison Dec 17 '12 at 02:26
  • If you're in a .net (Microsoft) environment, I would highly recommend opting for AppFabric, since it leverages existing knowledge about 2008 R2 and other supporting infrastructures. – Elad Lachmi Dec 17 '12 at 02:39
0

.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.

Sameer Shah
  • 1,073
  • 7
  • 12