Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to share System.Runtime.Caching cached objects between multiple web servers?

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.

like image 841
Darren Avatar asked Dec 11 '12 06:12

Darren


People also ask

Is MemoryCache shared?

MemoryCache does not allow you to share memory between processes as the memory used to cache objects is bound to the application pool. That's the nature of any in-memory cache implementation you'll find. The only way to actually use a shared cache is to use a distributed cache.

What is runtime caching?

Runtime caching refers to gradually adding responses to a cache "as you go". While runtime caching doesn't help with the reliability of the current request, it can help make future requests for the same URL more reliable.

What is distributed caching in asp net?

A distributed cache is a cache shared by multiple app servers, typically maintained as an external service to the app servers that access it. A distributed cache can improve the performance and scalability of an ASP.NET Core app, especially when the app is hosted by a cloud service or a server farm.

Where is MemoryCache stored?

"text": "When system stores the data in a RAM, it is called in-memory caching. It is the simplest cache as compared to the other cache forms. It sits between applications and databases to deliver responses at high speeds by storing data from earlier requests or copied directly from databases."


1 Answers

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.

like image 56
Jonathan Harrison Avatar answered Nov 02 '22 03:11

Jonathan Harrison