Distributed cache in .net - caching

I need to implement distributed cache in my asp.net mvc 4 application. My application is hosted in AWS in web farm environment. I can see the following options are available.
MemCache
RedisCache
NCache
I am not sure which one i should use. I need to configure session state as well in my cache.
Please Advice

Use NCache. Everything is built in and is open source.
Use
Session state
View State
Comparing Redis & Memcached with NCache you could see the following articles
Redis Comparison
Memcached Comparison

Related

Distributed caching with nhibernate orm

I am trying to implement caching in my application.
We are using Oracle database, asp.net web api to serve data to ui.
Api calls take more time, so we are thinking of implementing caching. Our code is deployed on 2 servers with load balancers.
How caching should be implemented.
What i am planning to implement is,
There should be a service API on any server, this api will store all data in memory. Ui will call our existing API, hit can go to any node, this api then will get data from new api(cache) and serve it to ui.
Is this architecture correct for distruted caching.
Can any one share their experience or guidance to implementation?
You might want to check NCache. Being a distributed caching solution, it provides first class support for sharing cache data between multiple clients due to the ache process running autonomously outside the address of any one application address space.
For your case, every web server in your load-balanced web farm will have be the client of NCache and have direct access to the cache servers. All the web servers,being clients to a central caching solution, will see the same cache data through simple-to-use NCache APIs. Any modification through insert, update or delete cache operations will be immediately observable to all the web servers.
The intelligence driving NCache allows for a seamless behind-the-scenes handling of all the tasks of storing and distributing the cache data among multiple cache server nodes on which the cache instance is distributed.
Furthermore, all the caching operations are completely independent of the framework used for database content retrieval and can be applied equally well with NHibernate, EF, EF Core and, of course, ADO.NET.
You can find more information about how to integrate NCache into your web farm environment and much more by using the following link:
http://www.alachisoft.com/resources/docs/ncache/admin-guide/ncache-architecture.html

Caching for both Angular and ASP .NET MVC

Background
In my project app, many small applications has been integrated and they have built-in different technologies such as
Angular JS 1.5
ASP.Net MVC
Angular 5
My app also uses AWS as cloud partner.
Problem
I need to implement Caching mechanism in my app. I am storing some values in S3 bucket and I am using API calls to pull the values. I want to keep those values in Cache. Since, it is implemented in multiple technologies (especially Angular and ASP.Net MVC), Does any caching mechanism can be used in common?
Observations
I have done some work on this and observed the following caching is available
.NET MVC - In Memory Caching
Angular - In Memory Cache with ReactJS
As AWS is my Cloud Partner, it is offering ElastiCache as a Web service, which supports MemCached and Redis. I am not clear whether this will behave like normal In-Memory Cache ( in ASP .NET Core) or this will refer database for caching and retrieve details (cause round-trip!) from there?
Question
Can anyone let me know best caching technique can be handled to my app for both .net mvc and angular?
This is bit tricky (I am assuming that you are using multiple servers of memcache). When you use memcache a lot depends on the clients implementation. Your client decides, on which server a particular key will be stored. And the servers are unaware of the existences of the other servers.
As you are using different languages you will be using different clients so each client will be implementing its own algorithm to decide the server on which the key will be placed. So there will be cases where your client on Angular will store key "K1" on server "S1" and the .Net Client will store the same key on server "S2"
This will add another problem of invalidating these keys, as it will be needed to invalidate the key on both servers.
There is one more problem. You will have to store all objects in a json format if the keys are common. So that the keys is stored on the same memcache server will be read by all programing languages.
I think the best thing is to set a small enough time to invalidate keys on memcache (if it is feasible) and storing keys with different prefix or suffix for each client type. So .net client will store key K1 as 'K1-net' and the one with Angular will store it as "k1-ang".
I would also explore redis which might help (not sure)

Infinispan : Which client API to choose?

If we implement the caching server using Infinispan, what are the possible client APIs to choose? Is Java Hot Rod client a good choice? Any other solutions?
Thank you!
As usually - Depends on your needs.
When you use HotRod you use Infinispan in a fashion similar to using MySQL/Sybase - you have an application that connects to the database backend which means
dedicated servers need to be set up and maintained
need to have multiple (dedicated) boxes to have high-availability and resiliency
but
HotRod client does some load-balancing for you
you can have dedicated data store servers with very specific configuration/separation/etc.
this mode is useful when Infinispan is used as a distributed store with database persistence
You might also use Infinispan in embedded mode, when you data is shared between you applications containing Infinispan instances; this mode is like having a HashMap that is
synchronized across the network with other boxes:
this gives you HA/resiliency by default (if your application is deployed with 2+ instances)
no need to have separate servers (no separate maintenance)
every new instance of your app will also contribute to the Infinispan cluster increasing HA/resiliency
(for testing you'll probably use Infinispan in embedded mode, anyway)
If you have your applications running on the same network segment (no firewall/switches/etc.) it might be easier just to use
Infinispan embedded mode, as it's easy to set up with lot of examples.
My recommendation would be to have a cache layer in your code that separates cache operations w/o the implementation so you can use whatever cache provider you want to use.
For Infininispan you should read the Infinispan User's Guide as #Galder pointed out.
The server modules documentation clarifies this.

MemoryCache object and load balancing

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

Does HttpRuntime.Cache work in Load Cluster enviornment?

Does anyone have knowledge if the HttpRuntime.Cache works in load cluster enviornment? and how to implement them?
What do you mean by Load Cluster Environment? Do you refer to that the content of the cache should be the same on all members of the cluster?
If that is the case this can be done in two ways
Use Sql server to serialize the cache and let all the servers point to the same sql-instance
Use ASPNet state server, which is a windows service asp.net will talk to over tcp. Again, let all your webserver instances point to the same state server.
Another approach is not to use the HttpRuntime.Cache but implement your own cacheprovider and use technologies like memcached.

Resources