Introduction
Boost Performance, Reduce Server Load & Scale Your Applications
High-traffic web applications demand fast response times and low latency. One proven way to achieve this is caching — temporarily storing data or output so it can be reused instead of recalculated on every request.
In ASP.NET Core, we can use the built-in Response Caching Middleware. When scalability and multi-server deployments are required, NCache is one of the best distributed caching solutions available.
This blog explains what response caching is, why to use NCache, and how to integrate NCache into an ASP.NET Core MVC application step-by-step.
Caching
Caching is a performance optimization technique that stores frequently accessed application data or pre-generated output in a temporary, high-speed memory location. This allows subsequent requests for the same information to be served immediately without re-processing logic or querying the database.
By eliminating repetitive computation and reducing database or API calls, caching significantly lowers CPU usage, minimizes network traffic, decreases latency, and delivers faster response times.
In simple words, caching stores application data or page output in memory across HTTP requests — making repeated requests load instantly, reducing CPU and database usage, and improving user experience.
Response Caching
Response caching is a technique where the final HTTP response (the output sent back to the browser or client) is temporarily stored so that identical future requests can be served directly from the cache, without re-running controller code, database queries, or application logic.
In a typical request–response model, every time a user requests a resource — such as a webpage, API result, or image — the server executes a full pipeline:
Routing
Authentication
Business logic execution
Data access
View rendering
Response generation
This entire process consumes time and server resources.
With response caching, once a specific request is processed for the first time, the server saves the generated response in memory or a distributed cache. If another request for the same resource is made within a valid time window, the server instantly returns the cached response instead of executing the pipeline again.
Why Response Caching Matters
Avoids executing heavy controller logic repeatedly
Reduces database or API calls
Speeds up page or API response times
Saves CPU, memory, and network resources
Helps handle high user load and peak traffic
Simple Example
Without Response Caching
User requests: GET /product-list
Server fetches product data from the database
Applies business logic and formats output
Sends response to browser
This process repeats for every request.
With Response Caching
When to Use Response Caching
Response caching is ideal for:
Static content (HTML, CSS, JavaScript, banners, images)
Pages or APIs that rarely change (FAQs, product catalogues, configuration data)
Public content requested repeatedly by many users
Example header:
Cache-Control: max-age=90
This means the browser can reuse the response for 90 seconds before requesting fresh data from the server.
Response caching is best suited for static or rarely updated content such as CSS files, JavaScript bundles, banners, and static HTML pages.
NCache for Distributed Response Caching
ASP.NET Core applications often run across multiple servers — behind a load balancer, in a web farm, or across cloud containers. In such environments, traditional in-memory caching becomes ineffective because:
Each server maintains its own memory
Cached data on Server A is not available on Server B
A user's next request may reach a different server, causing a cache miss
This results in inconsistency, duplicate processing, and wasted resources.
NCache is a high-performance, open-source distributed caching solution designed for high-transaction .NET applications. It delivers fast and linearly scalable in-memory data caching, significantly reducing database calls and eliminating performance bottlenecks.
NCache acts as a shared distributed caching layer. Instead of each server storing its own cache, all servers connect to a central NCache cluster and share cached items in real time.
This ensures:
Data cached by one server becomes instantly available to all servers
Response caching remains consistent and synchronized
The application scales efficiently to handle millions of requests
Setting Up NCache in ASP.NET Core (Step-by-Step)
Step 1: Create a New MVC Project
Open Visual Studio
Click "Create a New Project"
Select "ASP.NET Core Web Application"
Choose "Web Application (Model-View-Controller)"
Target .NET Core 3.0 or later
Disable Authentication, Docker, and HTTPS (for demo purposes)
Step 2: Install NCache NuGet Package
Open Package Manager Console and run:
Install-Package NCache.Microsoft.Extensions.Caching
Add namespace in Startup.cs:
using Alachisoft.NCache.Caching.Distributed;
Step 3: Enable Response Caching Middleware
In Startup.cs → ConfigureServices():
public void ConfigureServices(IServiceCollection services)
{
services.AddResponseCaching();
services.AddMvc();
}
Add middleware in Configure():
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseResponseCaching();
}
Step 4: Configure NCache as Distributed Cache
You can configure NCache using appsettings.json or via IOptions.
Option A: Configure via appsettings.json
{
"NCacheSettings": {
"CacheName": "MyDistributedCache",
"EnableLogs": "True",
"RequestTimeout": "60"
}
}
Add configuration in Startup:
public void ConfigureServices(IServiceCollection services)
{
services.AddResponseCaching();
services.AddNCacheDistributedCache(Configuration.GetSection("NCacheSettings"));
services.AddMvc();
}
Option B: Configure Using IOptions in Code
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddNCacheDistributedCache(options =>
{
options.CacheName = "MyDistributedCache";
options.EnableLogs = true;
options.ExceptionsEnabled = true;
});
}
Using Response Caching in Action Methods
Apply the ResponseCache attribute to controller actions:
public class HomeController : Controller
{
[ResponseCache(Duration = 60, Location = ResponseCacheLocation.Any, NoStore = false)]
public IActionResult GetData()
{
return Content("Cached Response Example");
}
}
Summary
Caching is essential for building high-performance ASP.NET Core applications.
Response caching reduces latency and improves scalability.
NCache is a powerful distributed caching solution ideal for multi-server environments.
ASP.NET Core integrates seamlessly with NCache using middleware and distributed cache configuration.