Over a million developers have joined DZone.

How to Optimize ASP.NET Core Performance With Distributed Cache

DZone 's Guide to

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.

· Performance Zone ·
Free Resource

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

  1. Database Server (SQL Server)
  2. 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.

ASP.NET Core Web Farm

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)

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 IDistributedCacheinterface. 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:

  1. No session locking is provided in ASP.NET Core. This is something even older ASP.NET Session State had provided.
  2. 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 IDistributedCacheprovider, 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)

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

  1. 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.
  2. Faster than Redis: NCache is actually faster than Redis. NCache Client Cache feature gives NCache a significant performance boost.
  3. 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:

asp.net core ,distributed cache ,.net ,redis ,big data ,sql server ,.net core ,performance

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}