How to Optimize ASP.NET Core Performance With Distributed Cache
Check out why a distributed cache could help with you data speed and deployment, and one solution that should provide some assistance.
Join the DZone community and get the full member experience.
Join For Freeasp.net core is starting to become popular for developing web applications because of its cleaner and lighter architecture and cross-platform support. and, many of these asp.net core applications are high traffic and run in a load-balanced multi-server deployment. in fact, it’s very common to see 10-20 server web farms and some are much larger than this.
having a multi-server load-balanced deployment makes your application-tier very scalable because you can add more servers as your transaction load increases. and, this allows your asp.net core application to handle extremely large transaction loads easily. but, there is a performance bottleneck that still exists and that slows down your asp.net core application.
and this asp.net core performance bottleneck is in your database and data storage that cannot handle heavy loads the way your asp.net core application-tier can. this is because although you can add more servers to the application-tier web farm, you cannot do the same with your database tier.
these are the two types of data storage that become a performance bottleneck for asp.net core applications.
- database server (sql server)
- asp.net core sessions
watch on-demand webinar on " how to optimize asp.net core performance with distributed cache "
the solution: distributed cache
to remove these data storage performance bottlenecks, your best bet is to use a distributed cache like ncache . ncache is an open source in-memory distributed cache for .net. ncache is much faster than database because it’s totally in-memory. and, unlike your database, ncache is linearly scalable because it lets you build a cluster of cache servers and add more servers to the cluster as your transaction loads increase.
ncache lets you cache application data so you can reduce those expensive database trips by almost 80%. this reduces load on the database that allows it to perform both reads and writes much faster and not become a performance bottleneck anymore.
ncache is also an extremely fast and scalable in-memory distributed store for your asp.net core sessions. additionally, ncache replicates asp.net core sessions to multiple servers to prevent data loss in case any cache server goes down. for asp.net core sessions, this is very important because you cannot afford to lose any sessions at run-time.
below is a diagram showing how a distributed cache like ncache fits in your application deployment.
app data caching thru asp.net core
idistributedcache
prior to asp.net core, the older asp.net provided a stand-alone asp.net cache that didn’t meet the needs of multi-server environments. now, asp.net core has introduced
idistributedcache
interface as a fairly basic distributed caching standard api that lets you program against it and then plug-in third-party distributed caches seamlessly.
here is an example of how to use
idistributedcache
interface:
idistributedcache _cache;
...
private byte[] loadcustomer(string custid) {
string key = "customers:customerid:"+ custid;
// is the customer in the cache?
byte[] customer = _cache.get(key);
if(customer == null) {
// the cache doesn't have it. so load from db
customer = loadfromdb(key);
// and, cache it for next time
_cache.set(key, customer);
}
return customer;
}
ncache has also implemented a provider for
idistributedcache
that you can plug into your asp.net core applications. this way, you don’t have to change any code specific to ncache.
here is what the
idistributedcache
interface looks like (please note that each of these methods also has an async overload).
namespace microsoft.extensions.caching.distributed
{
public interface idistributedcache
{
// each of these methods also has an “async” overload
byte[] get(string key);
void refresh(string key);
void remove(string key);
// specify absolute & sliding expiration thru options
void set(string key, byte[] value,
distributedcacheentryoptions options);
}
}
configuring ncache as
idistributedcache
provider
here is how you configure ncache as your
idistributedcache
provider in your asp.net core
startup
class.
public class startup
{
...
public void configureservices (iservicecollection services)
{
...
services.addncachedistributedcache();
...
}
...
}
why choose ncache api over
idistributedcache
?
if your caching needs are fairly basic and you desperately want the flexibility of changing the distributed caching vendor seamlessly, then you should go ahead and use
idistributedcache
interface. it allows you to change your caching vendor seamlessly. but, please weigh that against the cost of not having a lot of advanced caching features.
another option is for you to use ncache api directly from within your asp.net core application. ncache api is very similar to the legacy asp.net cache api. but, it contains tons of more features free of cost that enable you to take full advantage of an enterprise-grade distributed cache.
remember, the more data you can cache, the greater is the performance and scalability benefit to your application. and, without the advanced caching features, you’re often restricted to caching read-only or very simple data. read more about all the different
ncache caching features
that you miss by using
idistributedcache
provider.
storing asp.net core sessions in distributed cache
prior to asp.net core, the older asp.net provided an asp.net session state provider framework that allowed third-party session storage providers to plug-in. asp.net core sessions provides a similar mechanism for plugging in third-party storage providers.
there are two ways to use ncache as asp.net core session storage. they are:
1. use ncache for asp.net core sessions thru
idistributedcache
as soon as you configure ncache as
idistributedcache
provider for asp.net core, ncache automatically becomes the default storage option for asp.net core sessions and you don’t have to do anything else. but please note that this implementation is limited in features as compared to the older (before asp.net core) asp.net session state.
here are some of the things the default asp.net core sessions implementation lacks:
- no session locking is provided in asp.net core. this is something even older asp.net session state had provided.
-
byte[]
array for custom objects: asp.net core forces you to convert all your custom objects into byte array before you can store is in the session. even older asp.net session state support custom objects.
2. use ncache as asp.net core sessions provider
to work around the default asp.net core sessions implementation through
idistributedcache
provider, ncache has implemented its own asp.net core sessions provider. this implementation has a lot more features than the default one.
here is how you configure it in your startup class.
public class startup
{
...
public void configure(iapplicationbuilder app, ihostingenvironment env)
{
...
app.usencachesession();
...
}
...
}
you can specify asp.net core session configurations for the above in appsettings.json file as following:
{
...
"ncachesessions": {
...
"cachename": "democache",
"enablelogs": "true",
"requesttimeout": "90",
"enabledetaillogs": "false",
"exceptionsenabled": "true",
"writeexceptionstoeventlog": "false"
}
...
}
read more about how to configure ncache idistributedcache provider for asp.net core sessions storage.
reasons ncache is a better choice than redis
microsoft provides two options as
idistributedcache
providers. one is sql server and second is redis. ncache is better than both options. in comparison to sql server, ncache is much faster and more scalable.
and, ncache is also a better than redis for the following reasons
- native .net: ncache is 100% native .net and therefore fits into your .net application stack very nicely. on the other hand, redis comes from a linux background and is not a native .net cache.
- faster than redis: ncache is actually faster than redis. ncache client cache feature gives ncache a significant performance boost.
- more features: ncache offers a number of very important distributed cache features that redis does not. see more details in this redis vs ncache .
for more details about ncache, please click on one of the following links:
Published at DZone with permission of Iqbal Khan. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments