MemoryCache object and load balancing
Asked Answered
S

2

6

I'm writing a web application using ASP .NET MVC 3. I want to use the MemoryCache object but I'm worried about it causing issues with load balanced web servers. When I google for it looks like that problem is solved on the server ie using AppFabric. If a company has load balanced servers is it on them to make sure they have AppFabric or something similar running? or is there anything I can or should do as a developer for this?

Spancel answered 5/6, 2012 at 13:39 Comment(0)
D
4

First of all, for ASP.NET you should look at the ASP.NET Cache instead of MemoryCache. MemoryCache is a generic caching API that was introduced in .NET 4.0 to provide an equivalent of the ASP.NET Cache in non-web applications.

You're correct to say that AppFabric resolves the issue of multiple servers having their own instances of cached data, in that it provides a single logical cache accessible from all your web servers. Before you leap on it as the solution to your problem, there's a couple of things to consider:

  • It does not ship as part of Windows Server - it is, as you say, on you to install it on your servers if you want to use it. When AppFabric was released, there was a suggestion that it would ship as part of the next release of Windows Server, but I haven't seen anything about Windows Server 2012 that confirms that to be the case.

  • You need extra servers for it, or at least you're advised to have them. Microsoft's recommendation for AppFabric is that you run it on dedicated servers. Which means that whilst AppFabric itself is a free download, you may be incurring additional Windows Server licence costs. Speaking of which...

  • You might need Enterprise Edition licences. If you want to use the High Availability features of AppFabric, you can only do this with servers running Enterprise Edition, which is a more expensive licence than Standard Edition.

  • You might not need it after all. Some of this will depend on your application and why you want to use a shared caching layer. If your concern is that caches on multiple servers could get out of sync with the database (or indeed each other), some judicious use of SqlCacheDependency objects might get you past the issue.

Doby answered 6/6, 2012 at 15:39 Comment(1)
What I was thinking about using cache for was small attachments. An attachment is part of a larger object to be saved. That object is saved when the user clicks save. If the object isn't saved yet and the user uploads a file it needs to go somewhere. Putting that data in the database before the larger object gets there isn't the right answer. When the user cancels before saving, the application would have to delete that file. IMO cache is the correct place to store the file because the item can be removed in a more efficient manner. Is this logic flawed?Spancel
S
1

This CodeProject article Implementing Local MemoryCache Invalidation with Redis suggests an approach for handling the scenario you describe.

You didn't mention the flavor of load balancing that you are using: "sticky" or "stateless". By far the easiest solution is to use sticky sessions.

If you want to use local memory caches and stateless load balancing, you can end up with race conditions the cross-server invalidation messages arrive late. This can be particularly problematic if you use the Post-Redirect-Get pattern so common in ASP.Net MVC. This can be overcome by using cookies to supplement the cache invalidation broadcasts. I detail this in a blog post here.

Skidproof answered 14/11, 2014 at 21:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.