开发者

How can I manage Cache stored on the web server when I'm using 3 web servers?

开发者 https://www.devze.com 2023-01-09 02:00 出处:网络
We are working on a web application that is distributed across 3 load-balanced web servers. A lot of the data we retreive via NHibernate is stored in System.Web.Caching.Cache.

We are working on a web application that is distributed across 3 load-balanced web servers. A lot of the data we retreive via NHibernate is stored in System.Web.Caching.Cache.

System.Web.Caching.Cache does a lot to increase the responsiveness of an application, but there are a few issues that we don't exactly know how to resolve, such as

  1. when a user requests data on server1 that data is cached开发者_如何学C on server1, but for their next request, the load balancer might direct them to server2. That data they requested on server1 is no longer available, and server2 will have to request it from the database again.

  2. If the user does something on server1 to invalidate the cached data, the cache is flushed on server1. However the original cache is still available on server2 & server3, so when the user submits a subsequent request and they're directed to either of the other servers, they are going to be presented with invalid data.

  3. We have applications that update data (such as performance data) on a regular basis. When the performance data is updated we want to flush this from the cache so when a user requests the data again, they're presented with the latest data. How can we get these applications to flush the cache on 3 web servers?

What are the best ways to resolve these issues?

Should we have cache stored on a separate server such as we could to for HttpContext.Session with a SessionState server?

Is there a way for us to set a Cache Dependency on the Cache in the other 2 servers?

Is it possible for us to implement a Cache Dependency on the actual database tables or rows? When these change the cache is flushed? -- or could we set up a database trigger to flush the cache somehow?


Yes, a multi-server environment exposes the weakness of the ASP.NET cache in that it is single-server only. You might want to look into using a distributed cache like AppFabric, which would give you a single logical cache that underlies all three web servers.

AppFabric can be used with NHibernate as a second-level cache - see http://sourceforge.net/projects/nhcontrib/files/NHibernate.Caches/ (although be aware that this question suggests the current implementation may not be up-to-date with the AppFabric v1 release).


You have a few options:

1) Use a distributed cache (such as distcache, velocity, or ncache)

2) Use a shared cache instance (something like memcached, for instance) that all of your web servers make use of.

NHibernate has second-level cache providers for all of these, which can be found here.

0

精彩评论

暂无评论...
验证码 换一张
取 消